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 | 163 | 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 | 69 | 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 |
||
101 | * |
||
102 | * @throws \MongoException |
||
103 | */ |
||
104 | 58 | 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 | 167 | 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 | 62 | 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 | 162 | private static function ensureCorrectType(array $array, $wasObject = false) |
|
202 | } |
||
203 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.