1 | <?php |
||
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) |
|
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 1 | public function countFromRequest(ServerRequestInterface $request) |
|
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) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 1 | public function getByField($field, $value) |
|
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function getRelationshipsFor(Collection $collection, array $relationships = []) |
||
148 | |||
149 | /** |
||
150 | * Attach relationships to a specific entity. |
||
151 | * |
||
152 | * @param string $entityType |
||
153 | * @param string $relationship |
||
154 | * @param array $userData |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | protected function getEntityRelationships($entityType, $relationship, array $userData) |
||
195 | |||
196 | /** |
||
197 | * Get possible relationships and the properties attached to them. |
||
198 | * |
||
199 | * @param string $relationship |
||
200 | * |
||
201 | * @throws \InvalidArgumentException when requested relationship is not defined |
||
202 | * @throws \RuntimeException when map structure is defined incorrectly |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | protected function getRelationshipMap($relationship) |
||
241 | |||
242 | /** |
||
243 | * Returns table that repository is reading from. |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | abstract protected function getTable(); |
||
248 | } |
||
249 |