-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add naive generic array implementation #244
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -282,10 +282,9 @@ public function map($json, $object) | |
|
||
$array = null; | ||
$subtype = null; | ||
if ($this->isArrayOfType($type)) { | ||
if (null !== $subtype = $this->isArrayOfType($type)) { | ||
//array | ||
$array = array(); | ||
$subtype = substr($type, 0, -2); | ||
} else if (substr($type, -1) == ']') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't feel we should keep these two codepaths that deal with the contents of arrays separated. But this is risky to change. |
||
list($proptype, $subtype) = explode('[', substr($type, 0, -1)); | ||
if ($proptype == 'array') { | ||
|
@@ -370,7 +369,7 @@ public function map($json, $object) | |
*/ | ||
protected function getFullNamespace($type, $strNs) | ||
{ | ||
if ($type === null || $type === '' || $type[0] === '\\' || $strNs === '') { | ||
if ($type === null || $type === '' || $type[0] === '\\' || $strNs === '' || str_starts_with($type, 'array<')) { | ||
return $type; | ||
} | ||
list($first) = explode('[', $type, 2); | ||
|
@@ -464,11 +463,11 @@ public function mapArray($json, $array, $class = null, $parent_key = '') | |
$class = $this->getMappedType($originalClass, $jvalue); | ||
if ($class === null) { | ||
$array[$key] = $jvalue; | ||
} else if ($this->isArrayOfType($class)) { | ||
} else if (null !== $sub = $this->isArrayOfType($class)) { | ||
$array[$key] = $this->mapArray( | ||
$jvalue, | ||
array(), | ||
substr($class, 0, -2) | ||
$sub | ||
); | ||
} else if ($this->isFlatType(gettype($jvalue))) { | ||
//use constructor parameter if we have a class | ||
|
@@ -835,11 +834,17 @@ protected function isFlatType($type) | |
* | ||
* @param string $strType type to be matched | ||
* | ||
* @return bool | ||
* @return ?string | ||
*/ | ||
protected function isArrayOfType($strType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename the method accordingly. I strongly believe a function with |
||
{ | ||
return substr($strType, -2) === '[]'; | ||
if (substr($strType, -2) === '[]') { | ||
return substr($strType, 0, -2); | ||
} | ||
if (strpos($strType, 'array<') === 0 && $strType[strlen($strType)-1] === '>') { | ||
return substr($strType, 6, strlen($strType)-7); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's this weird situation now that two of three cases where contents of arrays are being detected are covered here, the remaining one (examples are in the tests) with Also, there are too many magic numbers happening here. I know we still support ancient PHP 7, so there is no way to utilize I see there is heavy complexity accumulating in this method now, and I know there's barely a way around it, but the responsible thing I ask for is to explain it better. And by explaining, I don't necessarily mean adding comments. |
||
return null; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ class JsonMapperTest_Array | |
public $flArray; | ||
|
||
/** | ||
* @var string[] | ||
* @var array<string> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing the only string array with the square bracket style into something different does not sound like the best idea. I don't know if the |
||
*/ | ||
public $strArray; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it's called
$subtype
, later down it is called$sub
. I'd prefer a single name for a single thing.