From 8658c605a21f48d4d29d3407db6a487d84e38223 Mon Sep 17 00:00:00 2001 From: Daniil Gentili <daniil@daniil.it> Date: Wed, 19 Feb 2025 16:17:31 +0100 Subject: [PATCH 1/2] Add naive generic array implementation --- src/JsonMapper.php | 17 +++++++++++------ tests/support/JsonMapperTest/Array.php | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/JsonMapper.php b/src/JsonMapper.php index 25534d0a6..a306e2e87 100644 --- a/src/JsonMapper.php +++ b/src/JsonMapper.php @@ -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) == ']') { list($proptype, $subtype) = explode('[', substr($type, 0, -1)); if ($proptype == 'array') { @@ -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) { - 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); + } + return null; } /** diff --git a/tests/support/JsonMapperTest/Array.php b/tests/support/JsonMapperTest/Array.php index 445c20090..cc70a0428 100644 --- a/tests/support/JsonMapperTest/Array.php +++ b/tests/support/JsonMapperTest/Array.php @@ -15,7 +15,7 @@ class JsonMapperTest_Array public $flArray; /** - * @var string[] + * @var array<string> */ public $strArray; From 74dc4631268ee3a7a92fb5e44f32b21d8427e678 Mon Sep 17 00:00:00 2001 From: Daniil Gentili <daniil@daniil.it> Date: Mon, 17 Mar 2025 21:51:44 +0100 Subject: [PATCH 2/2] Fix --- src/JsonMapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonMapper.php b/src/JsonMapper.php index a306e2e87..fb2e831dc 100644 --- a/src/JsonMapper.php +++ b/src/JsonMapper.php @@ -369,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);