1 | <?php |
||
30 | class EntityMapper implements EntityMapperInterface |
||
31 | { |
||
32 | /** |
||
33 | * @var EntityDescriptorInterface |
||
34 | */ |
||
35 | protected $descriptor; |
||
36 | |||
37 | /** |
||
38 | * @var EntityInterface |
||
39 | */ |
||
40 | protected $entity; |
||
41 | |||
42 | /** |
||
43 | * @var AdapterInterface |
||
44 | */ |
||
45 | protected $adapter; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $entityClassName; |
||
51 | |||
52 | /** |
||
53 | * Saves current entity object to database |
||
54 | * |
||
55 | * Optionally saves only the partial data if $data argument is passed. If |
||
56 | * no data is given al the field properties will be updated. |
||
57 | * |
||
58 | * @param array $data Partial data to save |
||
59 | * @param EntityInterface $entity |
||
60 | * |
||
61 | * @return self|$this|EntityMapperInterface |
||
62 | */ |
||
63 | 4 | public function save(EntityInterface $entity, array $data = []) |
|
94 | |||
95 | /** |
||
96 | * Deletes current entity from database |
||
97 | * |
||
98 | * @param EntityInterface $entity |
||
99 | * |
||
100 | * @return self|$this|EntityInterface |
||
101 | */ |
||
102 | 1 | public function delete(EntityInterface $entity) |
|
116 | |||
117 | /** |
||
118 | * Get entity descriptor |
||
119 | * |
||
120 | * @return EntityDescriptorInterface |
||
121 | */ |
||
122 | 10 | public function getDescriptor() |
|
132 | |||
133 | /** |
||
134 | * Set entity descriptor |
||
135 | * |
||
136 | * @param EntityDescriptorInterface $descriptor |
||
137 | * |
||
138 | * @return $this|self|EntityMapper |
||
139 | */ |
||
140 | 10 | public function setDescriptor($descriptor) |
|
145 | |||
146 | /** |
||
147 | * Sets the adapter for this mapper |
||
148 | * |
||
149 | * @param AdapterInterface $adapter |
||
150 | * |
||
151 | * @return self|$this|EntityMapper |
||
152 | */ |
||
153 | 8 | public function setAdapter(AdapterInterface $adapter) |
|
158 | |||
159 | /** |
||
160 | * Retrieves the current adapter |
||
161 | * |
||
162 | * @return AdapterInterface |
||
163 | */ |
||
164 | 4 | public function getAdapter() |
|
168 | |||
169 | /** |
||
170 | * Gets entity class name for this mapper |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 6 | public function getEntityClassName() |
|
181 | |||
182 | /** |
||
183 | * Sets entity class name for this mapper |
||
184 | * |
||
185 | * @param string $entityClassName |
||
186 | * |
||
187 | * @return self|$this|EntityMapper |
||
188 | */ |
||
189 | 8 | public function setEntityClassName($entityClassName) |
|
194 | |||
195 | /** |
||
196 | * Creates the insert/update query for current entity state |
||
197 | * |
||
198 | * @return Sql\Insert|Sql\Update |
||
199 | */ |
||
200 | 4 | protected function getUpdateQuery() |
|
214 | |||
215 | /** |
||
216 | * Adds the update criteria for an update query |
||
217 | * |
||
218 | * @param Sql\SqlInterface|Sql\Update|Sql\delete $query |
||
219 | * @param string $primaryKey |
||
220 | * @param string $table |
||
221 | * |
||
222 | * @return Sql\SqlInterface|Sql\Update|Sql\delete |
||
223 | */ |
||
224 | 2 | protected function setUpdateCriteria( |
|
231 | |||
232 | /** |
||
233 | * Gets data to be used in queries |
||
234 | * |
||
235 | * @return array |
||
236 | */ |
||
237 | 4 | protected function getData() |
|
247 | |||
248 | /** |
||
249 | * Creates an entity object from provided data |
||
250 | * |
||
251 | * Data can be an array with single row fields or a RecordList from |
||
252 | * a query. |
||
253 | * |
||
254 | * @param array|RecordList $data |
||
255 | * |
||
256 | * @return EntityInterface|EntityMapperInterface[]|EntityCollection |
||
257 | */ |
||
258 | 6 | public function createFrom($data) |
|
265 | |||
266 | /** |
||
267 | * Creates an entity for provided row array |
||
268 | * |
||
269 | * @param array $source |
||
270 | * @return EntityInterface |
||
271 | */ |
||
272 | 6 | protected function createSingle(array $source) |
|
284 | |||
285 | /** |
||
286 | * Creates an entity collection for provided record list |
||
287 | * |
||
288 | * @param RecordList $source |
||
289 | * @return EntityCollection |
||
290 | */ |
||
291 | 2 | protected function createMultiple(RecordList $source) |
|
299 | |||
300 | /** |
||
301 | * Sets the entity in the identity map of its repository. |
||
302 | * |
||
303 | * This avoids a select when one client creates an entity and |
||
304 | * other client gets it from the repository. |
||
305 | * |
||
306 | * @param EntityInterface $entity |
||
307 | * @return $this|self|EntityMapper |
||
308 | */ |
||
309 | 4 | protected function registerEntity(EntityInterface $entity) |
|
316 | |||
317 | /** |
||
318 | * Removes the entity from the identity map of its repository. |
||
319 | * |
||
320 | * @param EntityInterface $entity |
||
321 | * @return $this|self|EntityMapper |
||
322 | */ |
||
323 | protected function removeEntity(EntityInterface $entity) |
||
329 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: