1 | <?php |
||
20 | class HashType extends Type |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * extref converter |
||
25 | * |
||
26 | * @var ExtReferenceConverterInterface |
||
27 | */ |
||
28 | private static $extRefConverter; |
||
29 | |||
30 | /** |
||
31 | * sets the converter |
||
32 | * |
||
33 | * @param ExtReferenceConverterInterface $converter converter |
||
34 | * |
||
35 | * @return void |
||
36 | */ |
||
37 | 18 | public function setExtRefConverter(ExtReferenceConverterInterface $converter) |
|
41 | |||
42 | /** |
||
43 | * Convert DB value to PHP representation |
||
44 | * |
||
45 | * @param mixed $value Value to convert |
||
46 | * @return Hash|null |
||
47 | */ |
||
48 | 8 | public static function convertToPhp($value) |
|
52 | |||
53 | /** |
||
54 | * Convert PHP value to MongoDb representation |
||
55 | * |
||
56 | * @param mixed $value Value to convert |
||
57 | * @return object|null |
||
58 | */ |
||
59 | 8 | public static function convertToDb($value) |
|
77 | |||
78 | /** |
||
79 | * loops our structure recursively to |
||
80 | * - find all $ref objects that need to be converted either from that or to that.. |
||
81 | * - empty objects that need to be marked accordingly |
||
82 | * |
||
83 | * @param mixed $input input structure |
||
84 | * |
||
85 | * @return array altered structure with replaced $ref objects |
||
86 | */ |
||
87 | 16 | public static function processDynamicParts($input) |
|
88 | { |
||
89 | 16 | if ($input instanceof \stdClass) { |
|
90 | 8 | if (!empty(get_object_vars($input))) { |
|
91 | 8 | $input = self::processDynamicParts(get_object_vars($input)); |
|
92 | 4 | } |
|
93 | 8 | return $input; |
|
94 | } |
||
95 | |||
96 | // extrefs |
||
97 | 16 | $externalRefFieldName = '$ref'; |
|
98 | 16 | $internalRefFieldName = 'ref'; |
|
99 | |||
100 | // empty objects |
||
101 | 16 | $emptyObjectToPhpValue = '_____EMPTY_PHP_OBJECT_____'; |
|
102 | |||
103 | 16 | if (is_array($input)) { |
|
104 | 16 | foreach ($input as $key => $value) { |
|
105 | 16 | if ($key === $internalRefFieldName) { |
|
106 | if (is_array($value) && isset($value['$ref']) && isset($value['$id'])) { |
||
107 | $extRef = ExtReference::create($value['$ref'], $value['$id']); |
||
108 | $input[$externalRefFieldName] = self::$extRefConverter->getUrl($extRef); |
||
109 | unset($input[$internalRefFieldName]); |
||
110 | } |
||
111 | 16 | } elseif ($key === $externalRefFieldName) { |
|
112 | $extRef = self::$extRefConverter->getExtReference($value); |
||
113 | $input[$internalRefFieldName] = $extRef->jsonSerialize(); |
||
114 | unset($input[$externalRefFieldName]); |
||
115 | 16 | } elseif ($value === $emptyObjectToPhpValue) { |
|
116 | $input[$key] = new \stdClass(); |
||
117 | 16 | } elseif (is_object($value) && empty((array) $value)) { |
|
118 | $input[$key] = $emptyObjectToPhpValue; |
||
119 | } else { |
||
120 | 16 | if (is_array($value) || is_object($value)) { |
|
121 | $value = self::processDynamicParts($value); |
||
122 | } |
||
123 | 16 | $input[$key] = $value; |
|
124 | } |
||
125 | 8 | } |
|
126 | 8 | } |
|
127 | |||
128 | 16 | return $input; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Convert to PHP value |
||
133 | * |
||
134 | * @param mixed $value Db value |
||
135 | * @return Hash|null |
||
136 | */ |
||
137 | 2 | public function convertToPHPValue($value) |
|
141 | |||
142 | /** |
||
143 | * Closure to convert to PHP value |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 4 | public function closureToPHP() |
|
151 | |||
152 | /** |
||
153 | * Convert to DB value |
||
154 | * |
||
155 | * @param mixed $value PHP value |
||
156 | * @return object|null |
||
157 | */ |
||
158 | 2 | public function convertToDatabaseValue($value) |
|
162 | |||
163 | /** |
||
164 | * Closure to convert to DB value |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | 2 | public function closureToMongo() |
|
172 | } |
||
173 |