1 | <?php declare(strict_types=1); |
||
14 | class Repository implements RepositoryInterface |
||
15 | { |
||
16 | protected $tableGateway; |
||
17 | |||
18 | protected $hydrator; |
||
19 | |||
20 | protected $modelClass; |
||
21 | |||
22 | protected $relationCollection; |
||
23 | |||
24 | 14 | public function __construct( |
|
35 | |||
36 | protected function getRelation($name): Relation |
||
47 | |||
48 | protected function normalizeResultSet(array $data, array $relations): array |
||
66 | |||
67 | protected function applyRelationStrategies(array $relations) |
||
77 | |||
78 | 2 | public function findOne(array $conditions = [], array $with = []): object |
|
79 | { |
||
80 | 2 | $relations = []; |
|
81 | 2 | foreach ($with as $relationName) { |
|
82 | $relations[$relationName] = $this->getRelation($relationName); |
||
83 | } |
||
84 | 2 | $data = $this->tableGateway->queryOne($conditions, $relations); |
|
85 | 2 | if (!\is_array($data)) { |
|
86 | 1 | throw new NotFoundException(); |
|
87 | } |
||
88 | 1 | if (!empty($relations)) { |
|
89 | $data = $this->normalizeResultSet($data, $relations); |
||
90 | $this->applyRelationStrategies($relations); |
||
91 | } |
||
92 | 1 | $result = $this->hydrator->hydrate($this->modelClass, $data); |
|
93 | 1 | return $result; |
|
94 | } |
||
95 | |||
96 | public function findAll(array $conditions = [], array $order = [], int $limit = 0, int $offset = 0, array $with = []): array |
||
115 | |||
116 | 1 | public function aggregate(string $expression, array $conditions): string |
|
120 | |||
121 | 1 | public function aggregateCount(string $field = '', array $conditions = []): string |
|
122 | { |
||
123 | 1 | return $this->tableGateway->aggregateCount($field, $conditions); |
|
124 | } |
||
125 | |||
126 | 1 | public function aggregateSum(string $field, array $conditions): string |
|
127 | { |
||
128 | 1 | return $this->tableGateway->aggregateSum($field, $conditions); |
|
129 | } |
||
130 | |||
131 | 1 | public function aggregateAverage(string $field, array $conditions): string |
|
132 | { |
||
133 | 1 | return $this->tableGateway->aggregateAverage($field, $conditions); |
|
134 | } |
||
135 | |||
136 | 1 | public function aggregateMin(string $field, array $conditions): string |
|
137 | { |
||
138 | 1 | return $this->tableGateway->aggregateMin($field, $conditions); |
|
139 | } |
||
140 | |||
141 | public function aggregateveMax(string $field, array $conditions): string |
||
145 | |||
146 | 3 | protected function validateModelClass(object $model) |
|
147 | { |
||
148 | 3 | if (!($model instanceof $this->modelClass)) { |
|
149 | throw new InvalidModelClassException('Invalid model class: ' . get_class($model) . '. Expected ' . $this->modelClass); |
||
150 | } |
||
151 | 3 | } |
|
152 | |||
153 | 1 | public function insert(object $model): void |
|
154 | { |
||
155 | 1 | $this->validateModelClass($model); |
|
156 | 1 | $data = $this->hydrator->extract($model); |
|
157 | 1 | $primaryKeys = $this->tableGateway->insert($data); |
|
158 | 1 | if (!\is_array($primaryKeys)) { |
|
159 | throw new InsertException($data); |
||
160 | } |
||
161 | 1 | $this->hydrator->hydrate($model, $primaryKeys); |
|
162 | 1 | } |
|
163 | |||
164 | 1 | public function update(object $model): void |
|
165 | { |
||
166 | 1 | $this->validateModelClass($model); |
|
167 | 1 | $data = $this->hydrator->extract($model); |
|
168 | 1 | $this->tableGateway->updateOne($data); |
|
169 | 1 | } |
|
170 | |||
171 | 1 | public function delete(object $model): void |
|
172 | { |
||
173 | 1 | $this->validateModelClass($model); |
|
174 | 1 | $data = $this->hydrator->extract($model); |
|
175 | 1 | $this->tableGateway->deleteOne($data); |
|
176 | 1 | } |
|
177 | |||
178 | 1 | public function updateAll(array $data, array $conditions): int |
|
179 | { |
||
180 | 1 | return $this->tableGateway->updateAll($data, $conditions); |
|
181 | } |
||
182 | |||
183 | 1 | public function deleteAll(array $conditions): int |
|
184 | { |
||
185 | 1 | return $this->tableGateway->deleteAll($conditions); |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param array $data |
||
190 | * @return object |
||
191 | */ |
||
192 | 1 | public function create(array $data = []): object |
|
196 | |||
197 | /** |
||
198 | * @param object $entity |
||
199 | * @param array $data |
||
200 | */ |
||
201 | 1 | public function populate(object $entity, array $data): void |
|
205 | } |
||
206 |