1 | <?php |
||
27 | class EntityMapper extends AbstractEntityMapper implements |
||
28 | EntityMapperInterface |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * Add event trigger helpers |
||
33 | */ |
||
34 | use EventTriggers; |
||
35 | |||
36 | /** |
||
37 | * Saves current entity object to database |
||
38 | * |
||
39 | * Optionally saves only the partial data if $data argument is passed. If |
||
40 | * no data is given al the field properties will be updated. |
||
41 | * |
||
42 | * @param array $data Partial data to save |
||
43 | * @param EntityInterface $entity |
||
44 | * |
||
45 | * @return self|$this|EntityMapperInterface |
||
46 | */ |
||
47 | 4 | public function save(EntityInterface $entity, array $data = []) |
|
66 | |||
67 | /** |
||
68 | * Deletes current entity from database |
||
69 | * |
||
70 | * @param EntityInterface $entity |
||
71 | * |
||
72 | * @return self|$this|EntityInterface |
||
73 | */ |
||
74 | 2 | public function delete(EntityInterface $entity) |
|
93 | |||
94 | /** |
||
95 | * Creates the insert/update query for current entity state |
||
96 | * |
||
97 | * @return Sql\Insert|Sql\Update |
||
98 | */ |
||
99 | 4 | protected function getUpdateQuery() |
|
113 | |||
114 | /** |
||
115 | * Adds the update criteria for an update query |
||
116 | * |
||
117 | * @param Sql\SqlInterface|Sql\Update|Sql\delete $query |
||
118 | * @param string $primaryKey |
||
119 | * @param string $table |
||
120 | * |
||
121 | * @return Sql\SqlInterface|Sql\Update|Sql\delete |
||
122 | */ |
||
123 | 6 | protected function setUpdateCriteria( |
|
130 | |||
131 | /** |
||
132 | * Gets data to be used in queries |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 4 | protected function getData() |
|
146 | |||
147 | /** |
||
148 | * Creates an entity object from provided data |
||
149 | * |
||
150 | * Data can be an array with single row fields or a RecordList from |
||
151 | * a query. |
||
152 | * |
||
153 | * @param array|RecordList $data |
||
154 | * |
||
155 | * @return EntityInterface|EntityMapperInterface[]|EntityCollection |
||
156 | */ |
||
157 | 6 | public function createFrom($data) |
|
164 | |||
165 | /** |
||
166 | * Creates an entity for provided row array |
||
167 | * |
||
168 | * @param array $source |
||
169 | * @return EntityInterface |
||
170 | */ |
||
171 | 6 | protected function createSingle(array $source) |
|
183 | |||
184 | /** |
||
185 | * Creates an entity collection for provided record list |
||
186 | * |
||
187 | * @param RecordList $source |
||
188 | * @return EntityCollection |
||
189 | */ |
||
190 | 2 | protected function createMultiple(RecordList $source) |
|
198 | |||
199 | /** |
||
200 | * Sets the entity in the identity map of its repository. |
||
201 | * |
||
202 | * This avoids a select when one client creates an entity and |
||
203 | * other client gets it from the repository. |
||
204 | * |
||
205 | * @param EntityInterface $entity |
||
206 | * @return $this|self|EntityMapper |
||
207 | */ |
||
208 | 4 | protected function registerEntity(EntityInterface $entity) |
|
215 | |||
216 | /** |
||
217 | * Removes the entity from the identity map of its repository. |
||
218 | * |
||
219 | * @param EntityInterface $entity |
||
220 | * @return $this|self|EntityMapper |
||
221 | */ |
||
222 | 2 | protected function removeEntity(EntityInterface $entity) |
|
228 | } |
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: