1 | <?php |
||
30 | abstract class Model implements ClientInterface |
||
31 | { |
||
32 | protected $session; |
||
33 | protected $flexible_entity_class; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * @var RowStructure |
||
38 | */ |
||
39 | protected $structure; |
||
40 | |||
41 | /** |
||
42 | * getSession |
||
43 | * |
||
44 | * Return the current session. If session is not set, a ModelException is |
||
45 | * thrown. |
||
46 | * |
||
47 | * @access public |
||
48 | * @return Session |
||
49 | * @throws ModelException |
||
50 | */ |
||
51 | public function getSession() |
||
59 | |||
60 | /** |
||
61 | * getClientType |
||
62 | * |
||
63 | * @see ClientInterface |
||
64 | */ |
||
65 | public function getClientType() |
||
69 | |||
70 | /** |
||
71 | * getClientIdentifier |
||
72 | * |
||
73 | * @see ClientInterface |
||
74 | */ |
||
75 | public function getClientIdentifier() |
||
79 | |||
80 | /** |
||
81 | * initialize |
||
82 | * |
||
83 | * @see ClientInterface |
||
84 | */ |
||
85 | public function initialize(Session $session) |
||
115 | |||
116 | /** |
||
117 | * shutdown |
||
118 | * |
||
119 | * @see ClientInterface |
||
120 | */ |
||
121 | public function shutdown() |
||
124 | |||
125 | /** |
||
126 | * createEntity |
||
127 | * |
||
128 | * Create a new entity. |
||
129 | * |
||
130 | * @access public |
||
131 | * @param array $values |
||
132 | * @return FlexibleEntityInterface |
||
133 | */ |
||
134 | public function createEntity(array $values = []) |
||
142 | |||
143 | /** |
||
144 | * query |
||
145 | * |
||
146 | * Execute the given query and return a Collection iterator on results. If |
||
147 | * no projections are passed, it will use the default projection using |
||
148 | * createProjection() method. |
||
149 | * |
||
150 | * @access protected |
||
151 | * @param string $sql |
||
152 | * @param array $values |
||
153 | * @param Projection $projection |
||
154 | * @return CollectionIterator |
||
155 | */ |
||
156 | protected function query($sql, array $values = [], Projection $projection = null) |
||
176 | |||
177 | /** |
||
178 | * createDefaultProjection |
||
179 | * |
||
180 | * This method creates a projection based on the structure definition of |
||
181 | * the underlying relation. It may be used to shunt parent createProjection |
||
182 | * call in inherited classes. |
||
183 | * This method can be used where a projection that sticks to table |
||
184 | * definition is needed like recursive CTEs. For normal projections, use |
||
185 | * createProjection instead. |
||
186 | * |
||
187 | * @access public |
||
188 | * @return Projection |
||
189 | */ |
||
190 | final public function createDefaultProjection() |
||
194 | |||
195 | /** |
||
196 | * createProjection |
||
197 | * |
||
198 | * This is a helper to create a new projection according to the current |
||
199 | * structure.Overriding this method will change projection for all models. |
||
200 | * |
||
201 | * @access public |
||
202 | * @return Projection |
||
203 | */ |
||
204 | public function createProjection() |
||
208 | |||
209 | /** |
||
210 | * checkFlexibleEntity |
||
211 | * |
||
212 | * Check if the given entity is an instance of this model's flexible class. |
||
213 | * If not an exception is thrown. |
||
214 | * |
||
215 | * @access protected |
||
216 | * @param FlexibleEntityInterface $entity |
||
217 | * @throws \InvalidArgumentException |
||
218 | * @return Model $this |
||
219 | */ |
||
220 | protected function checkFlexibleEntity(FlexibleEntityInterface $entity) |
||
232 | |||
233 | /** |
||
234 | * getStructure |
||
235 | * |
||
236 | * Return the structure. |
||
237 | * |
||
238 | * @access public |
||
239 | * @return RowStructure |
||
240 | */ |
||
241 | public function getStructure() |
||
245 | |||
246 | /** |
||
247 | * getModel |
||
248 | * |
||
249 | * Proxy to Session::getModel(); |
||
250 | * |
||
251 | * @param string model identifier |
||
252 | * @return Model |
||
253 | */ |
||
254 | protected function getModel($identifier) |
||
260 | |||
261 | |||
262 | /** |
||
263 | * getFlexibleEntityClass |
||
264 | * |
||
265 | * Return the according flexible entity class associate with this Model |
||
266 | * instance. |
||
267 | * |
||
268 | * @access public |
||
269 | * @return string |
||
270 | */ |
||
271 | public function getFlexibleEntityClass() |
||
275 | |||
276 | /** |
||
277 | * escapeLiteral |
||
278 | * |
||
279 | * Handy method to escape strings. |
||
280 | * |
||
281 | * @access protected |
||
282 | * @param string $string |
||
283 | * @return string |
||
284 | */ |
||
285 | protected function escapeLiteral($string) |
||
292 | |||
293 | /** |
||
294 | * escapeLiteral |
||
295 | * |
||
296 | * Handy method to escape strings. |
||
297 | * |
||
298 | * @access protected |
||
299 | * @param string $string |
||
300 | * @return string |
||
301 | */ |
||
302 | protected function escapeIdentifier($string) |
||
309 | |||
310 | /** |
||
311 | * executeAnonymousQuery |
||
312 | * |
||
313 | * Handy method for DDL statements. |
||
314 | * |
||
315 | * @access protected |
||
316 | * @param string $sql |
||
317 | * @return Model $this |
||
318 | */ |
||
319 | protected function executeAnonymousQuery($sql) |
||
328 | } |
||
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: