forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEloquentRepository.php
More file actions
239 lines (212 loc) · 6.46 KB
/
Copy pathEloquentRepository.php
File metadata and controls
239 lines (212 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* Ushahidi Repository
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
namespace Ushahidi\App\Repository;
use Ushahidi\Core\Entity;
use Ushahidi\Core\Usecase;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Collection;
abstract class EloquentRepository implements
Usecase\CreateRepository,
Usecase\ReadRepository,
Usecase\UpdateRepository,
Usecase\DeleteRepository,
Usecase\ImportRepository
{
protected $resolver;
public function __construct(ConnectionResolverInterface $resolver)
{
$this->resolver = $resolver;
}
/**
* Get current connection
*
* @return Illuminate\Database\Connection;
*/
protected function connection()
{
return $this->resolver->connection();
}
/**
* Get the entity for this repository.
* @param Array $data
* @return Ushahidi\Core\Entity
*/
abstract public function getEntity(array $data = null);
/**
* Converts an array/collection of results into an collection
* of entities, indexed by the entity id.
*
* Included directly instead of using Ushahidi\Core\Traits\CollectionLoader
* because this implementation returns an Illuminate\Support\Collection
*
* @param Array|Iterable $results
* @return \Illuminate\Support\Collection
*/
protected function getCollection($results)
{
return Collection::wrap($results)->mapWithKeys(function ($item, $key) {
$entity = $this->getEntity((array) $item);
return [$entity->getId() => $entity];
});
}
/**
* Get the table name for this repository.
* @return String
*/
abstract protected function getTable();
// CreateRepository
// ReadRepository
// UpdateRepository
// DeleteRepository
public function get($id)
{
return $this->getEntity((array) $this->selectOne([
$this->getTable().'.id' => $id
]));
}
// CreateRepository
public function create(Entity $entity)
{
return $this->executeInsert($this->removeNullValues($entity->asArray()));
}
// UpdateRepository
public function update(Entity $entity)
{
return $this->executeUpdate(['id' => $entity->id], $entity->getChanged());
}
// DeleteRepository
public function delete(Entity $entity)
{
return $this->executeDelete(['id' => $entity->id]);
}
/**
* Remove all `null` values, to allow the database to set defaults.
*
* @param Array $data
* @return Array
*/
protected function removeNullValues(array $data)
{
return array_filter($data, function ($val) {
return isset($val);
});
}
/**
* Get a single record meeting some conditions.
* @param Array $where hash of conditions
* @return Array
*/
protected function selectOne(array $where = [])
{
return collect(
$this->selectQuery($where)->first()
)->toArray();
}
/**
* Get a count of records meeting some conditions.
* @param Array $where hash of conditions
* @return Integer
*/
protected function selectCount(array $where = [])
{
return $this->connection()->table($this->getTable())
->select($this->getTable() . '.*') // @todo do we need this?
->where($where) // @todo do we need to handle whereIn here too?
->count();
}
/**
* Return a SELECT query, optionally with preconditions.
* @param Array $where optional hash of conditions
* @return \Illuminate\Database\Query\Builder
*/
protected function selectQuery(array $where = [])
{
$query = $this->connection()->table($this->getTable())
->select($this->getTable() . '.*') // @todo do we need this?
->where($where) // @todo do we need to handle whereIn here too?
;
return $query;
}
/**
* Create a single record from input and return the created ID.
* @param Array $input hash of input
* @return Integer
*/
protected function executeInsert(array $input)
{
if (!$input) {
throw new RuntimeException(sprintf(
'Cannot create an empty record in table "%s"',
$this->getTable()
));
}
return $this->connection()
->table($this->getTable())
->insertGetId($input);
}
/**
* Update records from input with conditions and return the number affected.
* @param Array $where hash of conditions
* @param Array $input hash of input
* @return Integer
*/
protected function executeUpdate(array $where, array $input)
{
if (!$where) {
throw new RuntimeException(sprintf(
'Cannot update every record in table "%s"',
$this->getTable()
));
}
// Prevent overwriting created timestamp
// Probably not needed if `created` is set immutable in Entity
if (array_key_exists('created', $input)) {
unset($input['created']);
}
if (!$input) {
return 0; // nothing would be updated, just ignore
}
return $this->connection()
->table($this->getTable())
->where($where)
->update($input);
}
/**
* Delete records with conditions and return the number affected.
* @param Array $where hash of conditions
* @return Integer
*/
protected function executeDelete(array $where)
{
if (!$where) {
throw new RuntimeException(sprintf(
'Cannot delete every record in table "%s"',
$this->getTable()
));
}
return $this->connection()
->table($this->getTable())
->where($where)
->delete();
}
/**
* Check if an entity with the given id exists
* @param int $id
* @return bool
*/
public function exists($id)
{
return $this->connection()->table($this->getTable())
->select($this->getTable() . '.*') // @todo do we need this?
->where($where) // @todo do we need to handle whereIn here too?
->exists();
}
}