Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,11 @@ public function map($json, $object)
// again for subsequent objects of the same type
if (!isset($this->arInspectedClasses[$strClassName][$key])) {
$this->arInspectedClasses[$strClassName][$key]
= $this->inspectProperty($rc, $key);
= $this->inspectBuildProperty($rc, $key);
}

list($hasProperty, $accessor, $type, $isNullable)
/** @var JsonProperty $jsonProperty */
list($hasProperty, $accessor, $type, $isNullable, $jsonProperty)
= $this->arInspectedClasses[$strClassName][$key];

if (!$hasProperty) {
Expand All @@ -195,8 +196,8 @@ public function map($json, $object)
);

if (is_string($undefinedPropertyKey)) {
list($hasProperty, $accessor, $type, $isNullable)
= $this->inspectProperty($rc, $undefinedPropertyKey);
list($hasProperty, $accessor, $type, $isNullable, $jsonProperty)
= $this->inspectBuildProperty($rc, $undefinedPropertyKey);
}
} else {
$this->log(
Expand Down Expand Up @@ -231,28 +232,24 @@ public function map($json, $object)
$this->setProperty($object, $accessor, null);
continue;
}
$type = $this->removeNullable($type);
} else if ($jvalue === null) {
throw new JsonMapper_Exception(
'JSON property "' . $key . '" in class "'
. $strClassName . '" must not be NULL'
);
}

$type = $this->getFullNamespace($type, $strNs);
$type = $this->getMappedType($type, $jvalue);

if ($type === null || $type === 'mixed') {
//no given type - simply set the json data
$this->setProperty($object, $accessor, $jvalue);
continue;
} else if ($this->isObjectOfSameType($type, $jvalue)) {
$this->setProperty($object, $accessor, $jvalue);
continue;
} else if ($this->isSimpleType($type)
&& !(is_array($jvalue) && $this->hasVariadicArrayType($accessor))
} else if ($jsonProperty->isSimpleType
&& !($jsonProperty->hasVariadicArrayType && is_array($jvalue))
) {
if ($this->isFlatType($type)
if ($jsonProperty->isFlatType
&& !$this->isFlatType(gettype($jvalue))
) {
throw new JsonMapper_Exception(
Expand Down Expand Up @@ -282,18 +279,18 @@ public function map($json, $object)

$array = null;
$subtype = null;
if ($this->isArrayOfType($type)) {
if ($jsonProperty->isArrayOfType) {
//array
$array = array();
$subtype = substr($type, 0, -2);
} else if (substr($type, -1) == ']') {
} else if ($jsonProperty->isParenthesesEnd) {
list($proptype, $subtype) = explode('[', substr($type, 0, -1));
if ($proptype == 'array') {
$array = array();
} else {
$array = $this->createInstance($proptype, false, $jvalue);
}
} else if (is_array($jvalue) && $this->hasVariadicArrayType($accessor)) {
} else if ($jsonProperty->hasVariadicArrayType && is_array($jvalue)) {
$array = array();
$subtype = $type;
} else {
Expand Down Expand Up @@ -512,6 +509,26 @@ public function mapArray($json, $array, $class = null, $parent_key = '')
return $array;
}

protected function inspectBuildProperty(ReflectionClass $rc, $name)
{
list($hasProperty, $accessor, $type, $isNullable) = $this->inspectProperty($rc, $name);
$jsonProperty = new JsonProperty();
if ($isNullable || !$this->bStrictNullTypes) {
$type = $this->removeNullable($type);
}
$strNs = $rc->getNamespaceName();
$type = $this->getFullNamespace($type, $strNs);
$jsonProperty->isSimpleType = $this->isSimpleType($type);
$jsonProperty->hasVariadicArrayType = $this->hasVariadicArrayType($accessor);
$jsonProperty->isFlatType = $this->isFlatType($type);

if (is_string($type)) {
$jsonProperty->isArrayOfType = $this->isArrayOfType($type);
$jsonProperty->isParenthesesEnd = substr($type, -1) == ']';
}

return array($hasProperty, $accessor, $type, $isNullable, $jsonProperty);
}
/**
* Try to find out if a property exists in a given class.
* Checks property first, falls back to setter method.
Expand Down
30 changes: 30 additions & 0 deletions src/JsonProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

class JsonProperty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would need a proper name. The project consists of two classes, one of it an exception, as of now, and I believe it isn't ideal if you add random new class names into the root namespace.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea to use arrays or other names.

{
/**
* @var bool
*/
public $isSimpleType = false;

/**
* @var bool
*/
public $hasVariadicArrayType = false;

/**
* @var bool
*/
public $isFlatType = false;

/**
* @var bool
*/
public $isArrayOfType = false;

/**
* @var bool
*/
public $isParenthesesEnd = false;

}