Skip to content

Commit

Permalink
Handle FQN for array Array Binding
Browse files Browse the repository at this point in the history
  • Loading branch information
umotif-gcaldock committed Dec 22, 2020
1 parent 7c3ca2a commit e108224
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/JsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace uMotif\JsonDecoder;

use PhpDocReader\PhpDocReader;
use ReflectionClass;
use uMotif\JsonDecoder\Bindings\ArrayBinding;
use uMotif\JsonDecoder\Bindings\DateTimeBinding;
use uMotif\JsonDecoder\Bindings\FieldBinding;
Expand All @@ -10,8 +12,6 @@
use uMotif\JsonDecoder\Exceptions\InvalidJsonException;
use uMotif\JsonDecoder\Exceptions\JsonValueException;
use uMotif\JsonDecoder\Exceptions\NotExistingRootException;
use PhpDocReader\PhpDocReader;
use ReflectionClass;

class JsonDecoder
{
Expand Down Expand Up @@ -227,8 +227,8 @@ private function scan(string $class)
} else {
$bindings[] = new FieldBinding($propertyName, $propertyName, $propertyType);
}
} else if ($property->getDocComment()) {
$class = self::extractVar($property->getDocComment());
} elseif ($property->getDocComment()) {
$class = self::extractVar($property->getDocComment(), $reflectionClass);
$bindings[] = new ArrayBinding($propertyName, $propertyName, $class);
} else {
$bindings[] = new RawBinding($propertyName);
Expand All @@ -238,20 +238,24 @@ private function scan(string $class)
return $bindings;
}

/**
* @param string $docComment
* @return string
*/
private static function extractVar(string $docComment): string
private static function extractVar(string $docComment, ReflectionClass $class): string
{
$start = '@var ';
$end = '[]';
$end = '[]';

$ini = strpos($docComment, $start);
if ($ini == 0) return '';
if ($ini == 0) {
return '';
}
$ini += strlen($start);
$len = strpos($docComment, $end, $ini) - $ini;
return substr($docComment, $ini, $len);
$var = substr($docComment, $ini, $len);

if (class_exists($class->getNamespaceName() . '\\' . $var)) {
return $class->getNamespaceName() . '\\' . $var;
}

return $var;
}

/**
Expand Down

0 comments on commit e108224

Please sign in to comment.