|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Percy\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Percy\Entity\Collection; |
|
8
|
|
|
use Percy\Entity\CollectionBuilderTrait; |
|
9
|
|
|
use Percy\Entity\EntityInterface; |
|
10
|
|
|
use Percy\Http\QueryStringParserTrait; |
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractSqlRepository implements RepositoryInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use CollectionBuilderTrait; |
|
17
|
|
|
use QueryStringParserTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \Aura\Sql\ExtendedPdoInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $dbal; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* @var mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $relationships = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Construct. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Aura\Sql\ExtendedPdoInterface $dbal |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function __construct(ExtendedPdoInterface $dbal) |
|
36
|
|
|
{ |
|
37
|
2 |
|
$this->dbal = $dbal; |
|
38
|
2 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function countFromRequest(ServerRequestInterface $request) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$rules = $this->parseQueryString($request->getUri()->getQuery()); |
|
46
|
1 |
|
list($query, $params) = $this->buildQueryFromRules($rules, 'SELECT COUNT(*) as total FROM '); |
|
47
|
|
|
|
|
48
|
1 |
|
return (int) $this->dbal->fetchOne($query, $params)['total']; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function getFromRequest(ServerRequestInterface $request) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$rules = $this->parseQueryString($request->getUri()->getQuery()); |
|
57
|
|
|
|
|
58
|
1 |
|
list($query, $params) = $this->buildQueryFromRules($rules); |
|
59
|
|
|
|
|
60
|
1 |
|
if (array_key_exists('sort', $rules)) { |
|
61
|
1 |
|
$query .= sprintf(' ORDER BY %s ', $rules['sort']); |
|
62
|
1 |
|
$query .= (array_key_exists('sort_direction', $rules)) ? $rules['sort_direction'] : 'ASC'; |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
if (array_key_exists('limit', $rules)) { |
|
66
|
1 |
|
$query .= ' LIMIT '; |
|
67
|
1 |
|
$query .= (array_key_exists('offset', $rules)) ? sprintf('%d,', $rules['offset']) : ''; |
|
68
|
1 |
|
$query .= $rules['limit']; |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
return $this->buildCollection($this->dbal->fetchAll($query, $params)) |
|
72
|
1 |
|
->setTotal($this->countFromRequest($request)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Build a base query without sorting and limits from filter rules. |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $rules |
|
79
|
|
|
* @param string $start |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
1 |
|
protected function buildQueryFromRules(array $rules, $start = 'SELECT * FROM ') |
|
84
|
|
|
{ |
|
85
|
1 |
|
$query = $start . $this->getTable(); |
|
86
|
|
|
|
|
87
|
1 |
|
$params = []; |
|
88
|
|
|
|
|
89
|
1 |
|
if (array_key_exists('filter', $rules)) { |
|
90
|
1 |
|
foreach ($rules['filter'] as $key => $where) { |
|
91
|
1 |
|
$keyword = ($key === 0) ? ' WHERE' : ' AND'; |
|
92
|
1 |
|
$query .= sprintf('%s %s %s :%s', $keyword, $where['field'], $where['delimiter'], $where['field']); |
|
93
|
|
|
|
|
94
|
1 |
|
$params[$where['field']] = $where['value']; |
|
95
|
1 |
|
} |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
return [$query, $params]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function countByField($field, $value) |
|
105
|
|
|
{ |
|
106
|
1 |
|
$query = sprintf('SELECT COUNT(*) as total FROM %s WHERE %s IN (:%s)', $this->getTable(), $field, $field); |
|
107
|
|
|
|
|
108
|
|
|
$params = [ |
|
109
|
1 |
|
$field => implode(',', (array) $value) |
|
110
|
1 |
|
]; |
|
111
|
|
|
|
|
112
|
1 |
|
return (int) $this->dbal->fetchOne($query, $params)['total']; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function getByField($field, $value) |
|
119
|
|
|
{ |
|
120
|
1 |
|
$query = sprintf('SELECT * FROM %s WHERE %s IN (:%s)', $this->getTable(), $field, $field); |
|
121
|
|
|
|
|
122
|
|
|
$params = [ |
|
123
|
1 |
|
$field => implode(',', (array) $value) |
|
124
|
1 |
|
]; |
|
125
|
|
|
|
|
126
|
1 |
|
return $this->buildCollection($this->dbal->fetchAll($query, $params)) |
|
127
|
1 |
|
->setTotal($this->countByField($field, $value)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getRelationshipsFor(Collection $collection, array $relationships = []) |
|
134
|
|
|
{ |
|
135
|
|
|
$relCollection = new Collection; |
|
136
|
|
|
$entities = []; |
|
137
|
|
|
|
|
138
|
|
|
foreach ($collection->getIterator() as $entity) { |
|
139
|
|
|
$rels = $entity->getRelationships(); |
|
140
|
|
|
array_walk($rels, [$this, 'getEntityRelationships'], $entity, $entities); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
foreach ($entities as $entity) { |
|
144
|
|
|
$relCollection->addEntity($entity); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $relCollection; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Attach relationships to a specific entity. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $entityType |
|
154
|
|
|
* @param string $relationship |
|
155
|
|
|
* @param \Percy\Entity\EntityInterface $entity |
|
156
|
|
|
* @param array $entities |
|
157
|
|
|
* |
|
158
|
|
|
* @return void |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getEntityRelationships($entityType, $relationship, EntityInterface $entity, array &$entities) |
|
161
|
|
|
{ |
|
162
|
|
|
$map = $this->getRelationshipMap($relationship); |
|
163
|
|
|
|
|
164
|
|
|
$query = sprintf( |
|
165
|
|
|
'SELECT * FROM %s LEFT JOIN %s ON %s.%s = %s.%s WHERE %s = :%s', |
|
166
|
|
|
$map['defined_in']['table'], |
|
167
|
|
|
$map['target']['table'], |
|
168
|
|
|
$map['target']['table'], |
|
169
|
|
|
$map['target']['primary'], |
|
170
|
|
|
$map['defined_in']['table'], |
|
171
|
|
|
$map['target']['relationship'], |
|
172
|
|
|
$map['defined_in']['primary'], |
|
173
|
|
|
$map['defined_in']['entity'] |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
|
|
$result = $this->dbal->fetchAll($query, [ |
|
177
|
|
|
$map['defined_in']['entity'] => $entity[$map['defined_in']['entity']] |
|
178
|
|
|
]); |
|
179
|
|
|
|
|
180
|
|
|
$remove = [$map['defined_in']['primary'], $map['target']['relationship']]; |
|
181
|
|
|
|
|
182
|
|
|
foreach ($result as $resource) { |
|
183
|
|
|
$resource = array_filter($resource, function ($key) use ($remove) { |
|
184
|
|
|
return (! in_array($key, $remove)); |
|
185
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
186
|
|
|
|
|
187
|
|
|
$entities[] = (new $entityType)->hydrate($resource); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Get possible relationships and the properties attached to them. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $relationship |
|
195
|
|
|
* |
|
196
|
|
|
* @throws \InvalidArgumentException when requested relationship is not defined |
|
197
|
|
|
* @throws \RuntimeException when map structure is defined incorrectly |
|
198
|
|
|
* |
|
199
|
|
|
* @return array |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function getRelationshipMap($relationship) |
|
202
|
|
|
{ |
|
203
|
|
|
if (! array_key_exists($relationship, $this->relationships)) { |
|
204
|
|
|
throw new InvalidArgumentException( |
|
205
|
|
|
sprintf('(%s) is not defined in the relationship map on (%s)', $relationship, get_class($this)) |
|
206
|
|
|
); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$map = $this->relationships[$relationship]; |
|
210
|
|
|
|
|
211
|
|
|
foreach ([ |
|
212
|
|
|
'defined_in' => ['table', 'primary', 'entity'], |
|
213
|
|
|
'target' => ['table', 'primary', 'relationship'] |
|
214
|
|
|
] as $key => $value) { |
|
215
|
|
|
if (! array_key_exists($key, $map) || ! is_array($map[$key])) { |
|
216
|
|
|
throw new RuntimeException( |
|
217
|
|
|
sprintf( |
|
218
|
|
|
'Relationship (%s) should contain the (%s) key and should be of type array on (%s)', |
|
219
|
|
|
$relationship, $key, get_class($this) |
|
220
|
|
|
) |
|
221
|
|
|
); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
if (! empty(array_diff($value, array_keys($map[$key])))) { |
|
225
|
|
|
throw new RuntimeException( |
|
226
|
|
|
sprintf( |
|
227
|
|
|
'(%s) for relationship (%s) should contain keys (%s) on (%s)', |
|
228
|
|
|
$key, $relationship, implode(', ', $value), get_class($this) |
|
229
|
|
|
) |
|
230
|
|
|
); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return $map; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Returns table that repository is reading from. |
|
239
|
|
|
* |
|
240
|
|
|
* @return string |
|
241
|
|
|
*/ |
|
242
|
|
|
abstract protected function getTable(); |
|
243
|
|
|
} |
|
244
|
|
|
|