1 | <?php |
||
31 | class PkManager |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * Prepare multi pk criteria |
||
36 | * @param AnnotatedInterface $model |
||
37 | * @param mixed[] $pkValues |
||
38 | * @param CriteriaInterface|null $criteria |
||
39 | */ |
||
40 | 9 | public static function prepareAll($model, $pkValues, CriteriaInterface $criteria = null) |
|
62 | |||
63 | /** |
||
64 | * Prepare pk criteria from user provided data |
||
65 | * @param AnnotatedInterface $model |
||
66 | * @param mixed|mixed[] $pkValue |
||
67 | * @return Criteria |
||
68 | * @throws CriteriaException |
||
69 | */ |
||
70 | 93 | public static function prepare(AnnotatedInterface $model, $pkValue) |
|
92 | |||
93 | /** |
||
94 | * Create pk criteria from model data |
||
95 | * @param AnnotatedInterface $model |
||
96 | * @return Criteria |
||
97 | */ |
||
98 | 81 | public static function prepareFromModel(AnnotatedInterface $model) |
|
102 | |||
103 | /** |
||
104 | * Get primary key from model |
||
105 | * @param AnnotatedInterface $model |
||
106 | * @return MongoId|mixed|mixed[] |
||
107 | */ |
||
108 | 82 | public static function getFromModel(AnnotatedInterface $model) |
|
126 | |||
127 | /** |
||
128 | * Get primary key(s). |
||
129 | * |
||
130 | * Might return single string value for one primary key, or array |
||
131 | * for composite keys. |
||
132 | * |
||
133 | * |
||
134 | * @param AnnotatedInterface $model |
||
135 | * @return string|string[] |
||
136 | */ |
||
137 | 130 | public static function getPkKeys(AnnotatedInterface $model) |
|
141 | |||
142 | /** |
||
143 | * Get pk criteria from raw array |
||
144 | * @param mixed[] $data |
||
145 | * @param AnnotatedInterface $model |
||
146 | * @return mixed[] |
||
147 | */ |
||
148 | 5 | public static function getFromArray($data, AnnotatedInterface $model) |
|
166 | |||
167 | /** |
||
168 | * Apply pk value to model |
||
169 | * @param AnnotatedInterface $model |
||
170 | * @param MongoId|mixed|mixed[] $pkValue |
||
171 | * @return mixed Primary key value |
||
172 | * @throws CriteriaException |
||
173 | */ |
||
174 | 12 | public static function applyToModel(AnnotatedInterface $model, $pkValue) |
|
196 | |||
197 | /** |
||
198 | * Compare primary keys. For both params primary keys values or models can be used. |
||
199 | * Example use: |
||
200 | * <pre> |
||
201 | * <code> |
||
202 | * $model = new Model(); |
||
203 | * $pk = ['_id' => new MongoId()]; |
||
204 | * PkManager::compare($model, $pk); |
||
205 | * |
||
206 | * $pk1 = ['keyOne' => 1, 'keyTwo' => 2]; |
||
207 | * $pk2 = ['keyOne' => 1, 'keyTwo' => 2];; |
||
208 | * PkManager::compare($pk1, $pk2); |
||
209 | * |
||
210 | * $model1 = new Model(); |
||
211 | * $model2 = new Model(); |
||
212 | * PkManager::compare($model1, $model2); |
||
213 | * |
||
214 | * </code> |
||
215 | * </pre> |
||
216 | * @param AnnotatedInterface|mixed[] $source |
||
217 | * @param AnnotatedInterface|mixed[] $target |
||
218 | * @return boolean true if pk's points to same document |
||
219 | */ |
||
220 | 8 | public static function compare($source, $target) |
|
272 | |||
273 | 8 | private static function _compareNormalize($value) |
|
289 | |||
290 | /** |
||
291 | * Create pk criteria for single field |
||
292 | * @param AnnotatedInterface $model Model instance |
||
293 | * @param string $name |
||
294 | * @param mixed $value |
||
295 | * @param Criteria $criteria |
||
296 | */ |
||
297 | 93 | private static function _prepareField(AnnotatedInterface $model, $name, $value, Criteria &$criteria) |
|
302 | |||
303 | } |
||
304 |