1 | <?php |
||
24 | class TypeConverter |
||
25 | { |
||
26 | /** |
||
27 | * Converts a legacy type to the new BSON type |
||
28 | * |
||
29 | * This method handles type conversion from ext-mongo to ext-mongodb: |
||
30 | * - For all types (MongoId, MongoDate, etc.) it returns the correct BSON |
||
31 | * object instance |
||
32 | * - For arrays and objects it iterates over properties and converts each |
||
33 | * item individually |
||
34 | * - For other types it returns the value unconverted |
||
35 | * |
||
36 | * @param mixed $value |
||
37 | * @return mixed |
||
38 | */ |
||
39 | 211 | public static function fromLegacy($value) |
|
59 | |||
60 | /** |
||
61 | * Converts a BSON type to the legacy types |
||
62 | * |
||
63 | * This method handles type conversion from ext-mongodb to ext-mongo: |
||
64 | * - For all instances of BSON\Type it returns an object of the |
||
65 | * corresponding legacy type (MongoId, MongoDate, etc.) |
||
66 | * - For arrays and objects it iterates over properties and converts each |
||
67 | * item individually |
||
68 | * - For other types it returns the value unconverted |
||
69 | * |
||
70 | * @param mixed $value |
||
71 | * @return mixed |
||
72 | */ |
||
73 | 86 | public static function toLegacy($value) |
|
91 | |||
92 | /** |
||
93 | * Converts a projection used in find queries. |
||
94 | * |
||
95 | * This method handles conversion from the legacy syntax (e.g. ['x', 'y', 'z']) |
||
96 | * to the new syntax (e.g. ['x' => true, 'y' => true, 'z' => true]). While |
||
97 | * this was never documented, the legacy driver applied the same conversion. |
||
98 | * |
||
99 | * @param array $fields |
||
100 | * @return array|null |
||
101 | * |
||
102 | * @throws \MongoException |
||
103 | */ |
||
104 | 64 | public static function convertProjection($fields) |
|
127 | |||
128 | /** |
||
129 | * Helper method to find out if an array has numerical indexes |
||
130 | * |
||
131 | * For performance reason, this method checks the first array index only. |
||
132 | * More thorough inspection of the array might be needed. |
||
133 | * Note: Returns true for empty arrays to preserve compatibility with empty |
||
134 | * lists. |
||
135 | * |
||
136 | * @param array $array |
||
137 | * @return bool |
||
138 | */ |
||
139 | 215 | public static function isNumericArray(array $array) |
|
150 | |||
151 | /** |
||
152 | * Converter method to convert a BSON object to its legacy type |
||
153 | * |
||
154 | * @param BSON\Type $value |
||
155 | * @return mixed |
||
156 | */ |
||
157 | 81 | private static function convertBSONObjectToLegacy(BSON\Type $value) |
|
186 | |||
187 | /** |
||
188 | * Converts all arrays with non-numeric keys to stdClass |
||
189 | * |
||
190 | * @param array $array |
||
191 | * @param bool $wasObject |
||
192 | * @return array|Model\BSONArray|Model\BSONDocument |
||
193 | */ |
||
194 | 210 | private static function ensureCorrectType(array $array, $wasObject = false) |
|
202 | } |
||
203 |