1 | <?php |
||
29 | class EntityMapper implements EntityMapperInterface |
||
30 | { |
||
31 | /** |
||
32 | * @var EntityDescriptorInterface |
||
33 | */ |
||
34 | protected $descriptor; |
||
35 | |||
36 | /** |
||
37 | * @var EntityInterface |
||
38 | */ |
||
39 | protected $entity; |
||
40 | |||
41 | /** |
||
42 | * @var AdapterInterface |
||
43 | */ |
||
44 | protected $adapter; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $entityClassName; |
||
50 | |||
51 | /** |
||
52 | * Saves current entity object to database |
||
53 | * |
||
54 | * Optionally saves only the partial data if $data argument is passed. If |
||
55 | * no data is given al the field properties will be updated. |
||
56 | * |
||
57 | * @param array $data Partial data to save |
||
58 | * @param EntityInterface $entity |
||
59 | * |
||
60 | * @return self|$this|EntityMapperInterface |
||
61 | */ |
||
62 | 4 | public function save(EntityInterface $entity, array $data = []) |
|
75 | |||
76 | /** |
||
77 | * Deletes current entity from database |
||
78 | * |
||
79 | * @param EntityInterface $entity |
||
80 | * |
||
81 | * @return self|$this|EntityInterface |
||
82 | */ |
||
83 | 1 | public function delete(EntityInterface $entity) |
|
97 | |||
98 | /** |
||
99 | * Get entity descriptor |
||
100 | * |
||
101 | * @return EntityDescriptorInterface |
||
102 | */ |
||
103 | 10 | public function getDescriptor() |
|
113 | |||
114 | /** |
||
115 | * Set entity descriptor |
||
116 | * |
||
117 | * @param EntityDescriptorInterface $descriptor |
||
118 | * |
||
119 | * @return $this|self|EntityMapper |
||
120 | */ |
||
121 | 10 | public function setDescriptor($descriptor) |
|
126 | |||
127 | /** |
||
128 | * Sets the adapter for this mapper |
||
129 | * |
||
130 | * @param AdapterInterface $adapter |
||
131 | * |
||
132 | * @return self|$this|EntityMapper |
||
133 | */ |
||
134 | 8 | public function setAdapter(AdapterInterface $adapter) |
|
139 | |||
140 | /** |
||
141 | * Retrieves the current adapter |
||
142 | * |
||
143 | * @return AdapterInterface |
||
144 | */ |
||
145 | 4 | public function getAdapter() |
|
149 | |||
150 | /** |
||
151 | * Gets entity class name for this mapper |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | 6 | public function getEntityClassName() |
|
162 | |||
163 | /** |
||
164 | * Sets entity class name for this mapper |
||
165 | * |
||
166 | * @param string $entityClassName |
||
167 | * |
||
168 | * @return self|$this|EntityMapper |
||
169 | */ |
||
170 | 8 | public function setEntityClassName($entityClassName) |
|
175 | |||
176 | /** |
||
177 | * Creates the insert/update query for current entity state |
||
178 | * |
||
179 | * @return Sql\Insert|Sql\Update |
||
180 | */ |
||
181 | 4 | protected function getUpdateQuery() |
|
195 | |||
196 | /** |
||
197 | * Adds the update criteria for an update query |
||
198 | * |
||
199 | * @param Sql\SqlInterface|Sql\Update|Sql\delete $query |
||
200 | * @param string $primaryKey |
||
201 | * @param string $table |
||
202 | * |
||
203 | * @return Sql\SqlInterface|Sql\Update|Sql\delete |
||
204 | */ |
||
205 | 2 | protected function setUpdateCriteria( |
|
212 | |||
213 | /** |
||
214 | * Gets data to be used in queries |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | 4 | protected function getData() |
|
228 | |||
229 | /** |
||
230 | * Creates an entity object from provided data |
||
231 | * |
||
232 | * Data can be an array with single row fields or a RecordList from |
||
233 | * a query. |
||
234 | * |
||
235 | * @param array|RecordList $data |
||
236 | * |
||
237 | * @return EntityInterface|EntityMapperInterface[]|EntityCollection |
||
238 | */ |
||
239 | 6 | public function createFrom($data) |
|
246 | |||
247 | /** |
||
248 | * Creates an entity for provided row array |
||
249 | * |
||
250 | * @param array $source |
||
251 | * @return EntityInterface |
||
252 | */ |
||
253 | 6 | protected function createSingle(array $source) |
|
265 | |||
266 | /** |
||
267 | * Creates an entity collection for provided record list |
||
268 | * |
||
269 | * @param RecordList $source |
||
270 | * @return EntityCollection |
||
271 | */ |
||
272 | 2 | protected function createMultiple(RecordList $source) |
|
280 | |||
281 | /** |
||
282 | * Sets the entity in the identity map of its repository. |
||
283 | * |
||
284 | * This avoids a select when one client creates an entity and |
||
285 | * other client gets it from the repository. |
||
286 | * |
||
287 | * @param EntityInterface $entity |
||
288 | * @return $this|self|EntityMapper |
||
289 | */ |
||
290 | 4 | protected function registerEntity(EntityInterface $entity) |
|
297 | |||
298 | /** |
||
299 | * Removes the entity from the identity map of its repository. |
||
300 | * |
||
301 | * @param EntityInterface $entity |
||
302 | * @return $this|self|EntityMapper |
||
303 | */ |
||
304 | protected function removeEntity(EntityInterface $entity) |
||
310 | } |
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: