Skip to content

Commit

Permalink
reference ok
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamk committed Jul 7, 2022
1 parent f31890c commit 96909bb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 19 deletions.
13 changes: 12 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
{
"phpSniffer.standard": "./tests/phpsniff_ruleset.xml",
"phpSniffer.autoDetect": true,
"phpcs.standard": "./tests/phpsniff_ruleset.xml"
"phpcs.standard": "./tests/phpsniff_ruleset.xml",
"phpcs.autoConfigSearch": true,
"saveAndRun": {
"commands": [
{
"match": "^(?!.*\\\/[Vv]endor\\\/).*\\.php$",
"cmd": "phpcbf -q --standard=${workspaceFolder}/tests/phpsniff_ruleset.xml ${fileDirname}/${fileBasename}",
"useShortcut": false,
"silent": true
}
]
}
}
32 changes: 18 additions & 14 deletions src/AbstractJsonSerializeObjData.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,25 @@ final protected static function jsonDataToValue($value, $map = null)
{
if ($map !== null) {
$current = $map->getCurrent();
}

if (!is_null($map) && $map->isMapped()) {
$mappedVal = $map->getMappedValue($value);
switch (gettype($mappedVal)) {
case 'array':
$result = [];
foreach ($mappedVal as $key => $arrayVal) {
$map->setCurrent($key, $current);
$result[$key] = self::jsonDataToValue($arrayVal, $map);
};
return $result;
case 'object':
return self::fillObjFromValue($value, $mappedVal, $map);
default:
if ($map->isMapped()) {
$mappedVal = $map->getMappedValue($value, $isReference);
if ($isReference) {
return $mappedVal;
}
switch (gettype($mappedVal)) {
case 'array':
$result = [];
foreach ($mappedVal as $key => $arrayVal) {
$map->setCurrent($key, $current);
$result[$key] = self::jsonDataToValue($arrayVal, $map);
};
return $result;
case 'object':
return self::fillObjFromValue($value, $mappedVal, $map);
default:
return $mappedVal;
}
}
}

Expand Down Expand Up @@ -186,6 +189,7 @@ final protected static function fillObjFromValue($value, $obj, $map = null)
{
if ($map !== null) {
$current = $map->getCurrent();
$map->addReferenceObjOfCurrent($obj);
}

if ($obj instanceof \stdClass) {
Expand Down
32 changes: 28 additions & 4 deletions src/JsonUnserializeMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class JsonUnserializeMapping
private $isCurrentMapped = false;
/** @var mixed */
private $currentType = null;
/** @var object[] */
private $objReferences = [];

/**
* Class constructor
Expand Down Expand Up @@ -69,6 +71,19 @@ public function resetCurrent()
$this->currentProp = '';
$this->isCurrentMapped = false;
$this->currentType = null;
$this->objReferences = [];
}

/**
* Add reference object
*
* @param object $obj object
*
* @return void
*/
public function addReferenceObjOfCurrent($obj)
{
$this->objReferences[$this->currentProp] = $obj;
}

/**
Expand Down Expand Up @@ -139,11 +154,12 @@ public function setCurrent($prop, $parent = '')
/**
* Return mapped value
*
* @param mixed $value input value
* @param mixed $value input value
* @param bool $isReference Set to true if is reference object
*
* @return mixed
*/
public function getMappedValue($value)
public function getMappedValue($value, &$isReference = false)
{
if (!$this->isCurrentMapped) {
return $value;
Expand All @@ -168,8 +184,13 @@ public function getMappedValue($value)
}
return $newObj;
case 'rf:':
/** @todo prop reference */
break;
$reference = substr($type, 3);
$isReference = true;
if (isset($this->objReferences[$reference])) {
return $this->objReferences[$reference];
} else {
return null;
}
}

switch ($type) {
Expand All @@ -192,6 +213,9 @@ public function getMappedValue($value)
default:
break;
}

/** @todo create flag not strict to return false */
throw new Exception('Invalid mapping');
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,46 @@ public function testWildCardProp()

$this->assertEquals($value, $unserVal);
}

/**
* Test reference object
*
* @return void
*/
public function testReference()
{
$value = new stdClass();

$value->list = [
'a' => new stdClass(),
'b' => 2,
'c' => new stdClass()
];

$value->list['c']->value = $value;
$value->list['c']->item = 'item';
$value->list['c']->array = [
$value->list['a'],
$value->list['a'],
$value->list['a']
];


$map = new JsonUnserializeMapping(
[
'' => 'object',
'list/a' => 'object',
'list/c' => 'object',
'list/c/value' => 'rf:',
'list/c/array/*' => 'rf:list/a'
]
);

$serializedValue = JsonSerialize::serialize($value, JsonSerialize::JSON_SERIALIZE_SKIP_CLASS_NAME);
$unserVal = JsonSerialize::unserializeWithMapping($serializedValue, $map);

var_dump($unserVal);

$this->assertEquals($value, $unserVal);
}
}

0 comments on commit 96909bb

Please sign in to comment.