1 | <?php |
||
19 | final class Persister implements PersisterInterface |
||
20 | { |
||
21 | const IDENTIFIER_KEY = '_id'; |
||
22 | const POLYMORPHIC_KEY = '_type'; |
||
23 | const PERSISTER_KEY = 'mongodb'; |
||
24 | |||
25 | /** |
||
26 | * Provides a map of changeset methods. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | private $changeSetMethods = [ |
||
31 | 'attributes' => ['getAttribute', 'getAttributeDbValue'], |
||
32 | 'hasOne' => ['getRelationship', 'getHasOneDbValue'], |
||
33 | 'hasMany' => ['getRelationship', 'getHasManyDbValue'], |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * The raw result hydrator. |
||
38 | * |
||
39 | * @var Hydrator |
||
40 | */ |
||
41 | private $hydrator; |
||
42 | |||
43 | /** |
||
44 | * @var StorageMetadataFactory |
||
45 | */ |
||
46 | private $smf; |
||
47 | |||
48 | /** |
||
49 | * The query service. |
||
50 | * |
||
51 | * @var Query |
||
52 | */ |
||
53 | private $query; |
||
54 | |||
55 | /** |
||
56 | * Constructor. |
||
57 | * |
||
58 | * @param Query $query |
||
59 | * @param StorageMetadataFactory $smf |
||
60 | */ |
||
61 | public function __construct(Query $query, StorageMetadataFactory $smf) |
||
67 | |||
68 | /** |
||
69 | * {@inheritDoc} |
||
70 | */ |
||
71 | public function all(EntityMetadata $metadata, Store $store, array $identifiers = []) |
||
77 | |||
78 | /** |
||
79 | * {@inheritDoc} |
||
80 | */ |
||
81 | public function convertId($identifier, $strategy = null) |
||
85 | |||
86 | /** |
||
87 | * {@inheritDoc} |
||
88 | * @todo Optimize the changeset to query generation. |
||
89 | */ |
||
90 | public function create(Model $model) |
||
97 | |||
98 | /** |
||
99 | * {@inheritDoc} |
||
100 | */ |
||
101 | public function delete(Model $model) |
||
108 | |||
109 | /** |
||
110 | * {@inheritDoc} |
||
111 | */ |
||
112 | public function extractType(EntityMetadata $metadata, array $data) |
||
116 | |||
117 | /** |
||
118 | * {@inheritDoc} |
||
119 | */ |
||
120 | public function generateId($strategy = null) |
||
127 | |||
128 | /** |
||
129 | * @return Formatter |
||
130 | */ |
||
131 | public function getFormatter() |
||
135 | |||
136 | /** |
||
137 | * @return Hydrator |
||
138 | */ |
||
139 | public function getHydrator() |
||
143 | |||
144 | /** |
||
145 | * {@inheritDoc} |
||
146 | */ |
||
147 | public function getIdentifierKey() |
||
151 | |||
152 | /** |
||
153 | * {@inheritDoc} |
||
154 | */ |
||
155 | public function getPersistenceMetadataFactory() |
||
159 | |||
160 | /** |
||
161 | * {@inheritDoc} |
||
162 | */ |
||
163 | public function getPersisterKey() |
||
167 | |||
168 | /** |
||
169 | * {@inheritDoc} |
||
170 | */ |
||
171 | public function getPolymorphicKey() |
||
175 | |||
176 | /** |
||
177 | * @return Query |
||
178 | */ |
||
179 | public function getQuery() |
||
183 | |||
184 | /** |
||
185 | * {@inheritDoc} |
||
186 | */ |
||
187 | public function inverse(EntityMetadata $owner, EntityMetadata $rel, Store $store, array $identifiers, $inverseField) |
||
193 | |||
194 | /** |
||
195 | * {@inheritDoc} |
||
196 | */ |
||
197 | public function query(EntityMetadata $metadata, Store $store, array $criteria, array $fields = [], array $sort = [], $offset = 0, $limit = 0) |
||
202 | |||
203 | /** |
||
204 | * {@inheritDoc} |
||
205 | */ |
||
206 | public function retrieve(EntityMetadata $metadata, $identifier, Store $store) |
||
215 | |||
216 | /** |
||
217 | * {@inheritDoc} |
||
218 | * @todo Optimize the changeset to query generation. |
||
219 | */ |
||
220 | public function update(Model $model) |
||
233 | |||
234 | /** |
||
235 | * Appends the change set values to a database object based on the provided handler. |
||
236 | * |
||
237 | * @param Model $model |
||
238 | * @param array $obj |
||
239 | * @param Closure $handler |
||
240 | * @return array |
||
241 | */ |
||
242 | private function appendChangeSet(Model $model, array $obj, Closure $handler) |
||
257 | |||
258 | /** |
||
259 | * Creates the database insert object for a Model. |
||
260 | * |
||
261 | * @param Model $model |
||
262 | * @return array |
||
263 | */ |
||
264 | private function createInsertObj(Model $model) |
||
275 | |||
276 | /** |
||
277 | * Creates the database update object for a Model. |
||
278 | * |
||
279 | * @param Model $model |
||
280 | * @return array |
||
281 | */ |
||
282 | private function createUpdateObj(Model $model) |
||
286 | |||
287 | /** |
||
288 | * Gets the change set handler Closure for create. |
||
289 | * |
||
290 | * @return Closure |
||
291 | */ |
||
292 | private function getCreateChangeSetHandler() |
||
301 | |||
302 | /** |
||
303 | * Gets the change set handler Closure for update. |
||
304 | * |
||
305 | * @return Closure |
||
306 | */ |
||
307 | private function getUpdateChangeSetHandler() |
||
319 | } |
||
320 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.