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