Skip to content

Commit 874c968

Browse files
authored
Merge pull request #17 from andreamk/20220803_AL_add_skip_magic_methods-to-unserialize
now JSON_SKIP_MAGIC_METHODS work on unserialize
2 parents 0dcb1d8 + d7c9571 commit 874c968

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

src/AbstractJsonSerializeObjData.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ final protected static function valueToJsonData($value, $flags = 0, $objParents
124124
* Return value from json decoded data
125125
*
126126
* @param mixed $value json decoded data
127+
* @param int $flags flags bitmask
127128
* @param ?JsonUnserializeMap $map unserialize map
128129
*
129130
* @return mixed
130131
*/
131-
final protected static function jsonDataToValue($value, $map = null)
132+
final protected static function jsonDataToValue($value, $flags = 0, $map = null)
132133
{
133134
if ($map !== null) {
134135
$current = $map->getCurrent();
@@ -144,15 +145,15 @@ final protected static function jsonDataToValue($value, $map = null)
144145
$result = [];
145146
foreach ($mappedVal as $key => $arrayVal) {
146147
$map->setCurrent($key, $current);
147-
$result[$key] = self::jsonDataToValue($arrayVal, $map);
148+
$result[$key] = self::jsonDataToValue($arrayVal, $flags, $map);
148149
};
149150
return $result;
150151
case 'object':
151152
/** @var object $mappedVal */
152153
if (!is_array($value)) {
153154
$value = [];
154155
}
155-
return self::fillObjFromValue($value, $mappedVal, $map);
156+
return self::fillObjFromValue($value, $mappedVal, $flags, $map);
156157
default:
157158
return $mappedVal;
158159
}
@@ -168,10 +169,10 @@ final protected static function jsonDataToValue($value, $map = null)
168169
if ($map !== null) {
169170
$map->setCurrent($key, $current);
170171
}
171-
$result[$key] = self::jsonDataToValue($arrayVal, $map);
172+
$result[$key] = self::jsonDataToValue($arrayVal, $flags, $map);
172173
}
173174
} else {
174-
$result = self::fillObjFromValue($value, self::getObjFromClass($newClassName), $map);
175+
$result = self::fillObjFromValue($value, self::getObjFromClass($newClassName), $flags, $map);
175176
}
176177
return $result;
177178
case 'boolean':
@@ -208,11 +209,12 @@ final public static function getObjFromClass($class)
208209
*
209210
* @param mixed[] $value value from json data
210211
* @param object $obj object to fill with json data
212+
* @param int $flags flags bitmask
211213
* @param ?JsonUnserializeMap $map unserialize map
212214
*
213215
* @return object
214216
*/
215-
final protected static function fillObjFromValue($value, $obj, $map = null)
217+
final protected static function fillObjFromValue($value, $obj, $flags = 0, $map = null)
216218
{
217219
if ($map !== null) {
218220
$current = $map->getCurrent();
@@ -227,10 +229,11 @@ final protected static function fillObjFromValue($value, $obj, $map = null)
227229
if ($map !== null) {
228230
$map->setCurrent($arrayProp, $current);
229231
}
230-
$obj->{$arrayProp} = self::jsonDataToValue($arrayValue, $map);
232+
$obj->{$arrayProp} = self::jsonDataToValue($arrayValue, $flags, $map);
231233
}
232234
} else {
233-
if (method_exists($obj, '__unserialize')) {
235+
$skipMagicMethods = ($flags & self::JSON_SKIP_MAGIC_METHODS);
236+
if (!$skipMagicMethods && method_exists($obj, '__unserialize')) {
234237
$obj->__unserialize($value);
235238
} else {
236239
$reflect = new ReflectionObject($obj);
@@ -246,10 +249,10 @@ final protected static function fillObjFromValue($value, $obj, $map = null)
246249
if (!array_key_exists($propName, $value) || $prop->isStatic()) {
247250
continue;
248251
}
249-
$prop->setValue($obj, self::jsonDataToValue($value[$propName], $map));
252+
$prop->setValue($obj, self::jsonDataToValue($value[$propName], $flags, $map));
250253
}
251254

252-
if (method_exists($obj, '__wakeup')) {
255+
if (!$skipMagicMethods && method_exists($obj, '__wakeup')) {
253256
$obj->__wakeup();
254257
}
255258
}

src/JsonSerialize.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function serializeToData($value, $flags = 0)
7777
public static function unserialize($json, $depth = 512, $flags = 0)
7878
{
7979
$publicArray = json_decode($json, true, $depth, $flags);
80-
return self::jsonDataToValue($publicArray);
80+
return self::jsonDataToValue($publicArray, $flags);
8181
}
8282

8383
/**
@@ -96,7 +96,7 @@ public static function unserializeWithMap($json, JsonUnserializeMap $map, $depth
9696
{
9797
$publicArray = json_decode($json, true, $depth, $flags);
9898
$map->setCurrent('');
99-
return self::jsonDataToValue($publicArray, $map);
99+
return self::jsonDataToValue($publicArray, $flags, $map);
100100
}
101101

102102
/**
@@ -123,7 +123,7 @@ public static function unserializeToObj($json, $obj, $depth = 512, $flags = 0)
123123
if (!is_array($value)) {
124124
throw new Exception('json value isn\'t an array');
125125
}
126-
return self::fillObjFromValue($value, $obj);
126+
return self::fillObjFromValue($value, $obj, $flags);
127127
}
128128

129129
/**

0 commit comments

Comments
 (0)