-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathTypeConverter.php
161 lines (148 loc) · 5.19 KB
/
TypeConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Alcaeus\MongoDbAdapter;
use MongoDB\BSON;
use MongoDB\Model;
/**
* @internal
*/
class TypeConverter
{
/**
* Converts a legacy type to the new BSON type
*
* This method handles type conversion from ext-mongo to ext-mongodb:
* - For all types (MongoId, MongoDate, etc.) it returns the correct BSON
* object instance
* - For arrays and objects it iterates over properties and converts each
* item individually
* - For other types it returns the value unconverted
*
* @param mixed $value
* @return mixed
*/
public static function fromLegacy($value)
{
switch (true) {
case $value instanceof TypeInterface:
return $value->toBSONType();
case is_array($value):
case is_object($value);
$result = [];
foreach ($value as $key => $item) {
$result[$key] = self::fromLegacy($item);
}
return self::ensureCorrectType($result, is_object($value));
default:
return $value;
}
}
/**
* Converts a BSON type to the legacy types
*
* This method handles type conversion from ext-mongodb to ext-mongo:
* - For all instances of BSON\Type it returns an object of the
* corresponding legacy type (MongoId, MongoDate, etc.)
* - For arrays and objects it iterates over properties and converts each
* item individually
* - For other types it returns the value unconverted
*
* @param mixed $value
* @return mixed
*/
public static function toLegacy($value)
{
switch (true) {
case $value instanceof BSON\Type:
return self::convertBSONObjectToLegacy($value);
case is_array($value):
case is_object($value):
$result = [];
foreach ($value as $key => $item) {
$result[$key] = self::toLegacy($item);
}
return $result;
default:
return $value;
}
}
/**
* Helper method to find out if an array has numerical indexes
*
* For performance reason, this method checks the first array index only.
* More thorough inspection of the array might be needed.
* Note: Returns true for empty arrays to preserve compatibility with empty
* lists.
*
* @param array $array
* @return bool
*/
public static function isNumericArray(array $array)
{
return $array === [] || is_numeric(array_keys($array)[0]);
}
/**
* Converter method to convert a BSON object to its legacy type
*
* @param BSON\Type $value
* @return mixed
*/
private static function convertBSONObjectToLegacy(BSON\Type $value)
{
switch (true) {
case $value instanceof BSON\ObjectID:
return new \MongoId($value);
case $value instanceof BSON\Binary:
return new \MongoBinData($value);
case $value instanceof BSON\Javascript:
return new \MongoCode($value);
case $value instanceof BSON\MaxKey:
return new \MongoMaxKey();
case $value instanceof BSON\MinKey:
return new \MongoMinKey();
case $value instanceof BSON\Regex:
return new \MongoRegex($value);
case $value instanceof BSON\Timestamp:
return new \MongoTimestamp($value);
case $value instanceof BSON\UTCDatetime:
return new \MongoDate($value);
case $value instanceof Model\BSONDocument:
case $value instanceof Model\BSONArray:
return array_map(
['self', 'toLegacy'],
$value->getArrayCopy()
);
default:
return $value;
}
}
/**
* Converts all arrays with non-numeric keys to stdClass
*
* @param array $array
* @param bool $wasObject
* @return array|Model\BSONArray|Model\BSONDocument
*/
private static function ensureCorrectType(array $array, $wasObject = false)
{
if ($array === [] && $wasObject) {
return (object) $array;
}
if (static::isNumericArray($array)) {
return $array;
}
return new Model\BSONDocument($array);
}
}