-
-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
211 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Metadata\Driver; | ||
|
||
use JMS\Serializer\Metadata\ClassMetadata as SerializerClassMetadata; | ||
use JMS\Serializer\Metadata\ExpressionPropertyMetadata; | ||
use JMS\Serializer\Metadata\PropertyMetadata; | ||
use JMS\Serializer\Metadata\StaticPropertyMetadata; | ||
use JMS\Serializer\Metadata\VirtualPropertyMetadata; | ||
use JMS\Serializer\Type\Parser; | ||
use JMS\Serializer\Type\ParserInterface; | ||
use Metadata\ClassMetadata; | ||
use Metadata\Driver\DriverInterface; | ||
use ReflectionClass; | ||
use ReflectionException; | ||
use ReflectionProperty; | ||
|
||
class TypedPropertiesDriver implements DriverInterface | ||
{ | ||
/** | ||
* @var DriverInterface | ||
*/ | ||
protected $delegate; | ||
|
||
/** | ||
* @var ParserInterface | ||
*/ | ||
protected $typeParser; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $customTypes; | ||
|
||
/** | ||
* @param string[] $customTypes | ||
*/ | ||
public function __construct(DriverInterface $delegate, ?ParserInterface $typeParser = null, array $customTypes = []) | ||
{ | ||
$this->delegate = $delegate; | ||
$this->typeParser = $typeParser ?: new Parser(); | ||
$this->customTypes = $customTypes; | ||
} | ||
|
||
public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata | ||
{ | ||
/** @var SerializerClassMetadata $classMetadata */ | ||
$classMetadata = $this->delegate->loadMetadataForClass($class); | ||
|
||
if (null === $classMetadata) { | ||
return null; | ||
} | ||
// We base our scan on the internal driver's property list so that we | ||
// respect any internal white/blacklisting like in the AnnotationDriver | ||
foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) { | ||
/** @var $propertyMetadata PropertyMetadata */ | ||
|
||
// If the inner driver provides a type, don't guess anymore. | ||
if ($propertyMetadata->type || $this->isVirtualProperty($propertyMetadata)) { | ||
continue; | ||
} | ||
|
||
try { | ||
$propertyReflection = $this->getReflection($propertyMetadata); | ||
if ($this->shouldTypeHint($propertyReflection)) { | ||
$propertyMetadata->setType($this->typeParser->parse($propertyReflection->getType()->getName())); | ||
} | ||
} catch (ReflectionException $e) { | ||
continue; | ||
} | ||
} | ||
|
||
return $classMetadata; | ||
} | ||
|
||
private function shouldTypeHint(ReflectionProperty $propertyReflection): bool | ||
{ | ||
if (null === $propertyReflection->getType()) { | ||
return false; | ||
} | ||
|
||
if (in_array($propertyReflection->getType()->getName(), $this->customTypes, true)) { | ||
return true; | ||
} | ||
|
||
if (interface_exists($propertyReflection->getType()->getName())) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function getReflection(PropertyMetadata $propertyMetadata): ReflectionProperty | ||
{ | ||
return new ReflectionProperty($propertyMetadata->class, $propertyMetadata->name); | ||
} | ||
|
||
private function isVirtualProperty(PropertyMetadata $propertyMetadata): bool | ||
{ | ||
return $propertyMetadata instanceof VirtualPropertyMetadata | ||
|| $propertyMetadata instanceof StaticPropertyMetadata | ||
|| $propertyMetadata instanceof ExpressionPropertyMetadata; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Tests\Fixtures\TypedProperties; | ||
|
||
class User | ||
{ | ||
private int $id; | ||
private Role $role; | ||
private \DateTime $created; | ||
private \DateTimeInterface $updated; | ||
private iterable $tags; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Tests\Metadata\Driver; | ||
|
||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use JMS\Serializer\Builder\DefaultDriverFactory; | ||
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; | ||
use JMS\Serializer\Tests\Fixtures\TypedProperties\User; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DefaultDriverFactoryTest extends TestCase | ||
{ | ||
public function testDefaultDriverFactoryLoadsTypedPropertiesDriver() | ||
{ | ||
if (PHP_VERSION_ID < 70400) { | ||
$this->markTestSkipped(sprintf('%s requires PHP 7.4', __METHOD__)); | ||
} | ||
|
||
$factory = new DefaultDriverFactory(new IdenticalPropertyNamingStrategy()); | ||
|
||
$driver = $factory->createDriver([], new AnnotationReader()); | ||
|
||
$m = $driver->loadMetadataForClass(new \ReflectionClass(User::class)); | ||
self::assertNotNull($m); | ||
|
||
$expectedPropertyTypes = [ | ||
'id' => 'int', | ||
'role' => 'JMS\Serializer\Tests\Fixtures\TypedProperties\Role', | ||
'created' => 'DateTime', | ||
'tags' => 'iterable', | ||
]; | ||
|
||
foreach ($expectedPropertyTypes as $property => $type) { | ||
self::assertEquals(['name' => $type, 'params' => []], $m->propertyMetadata[$property]->type); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Tests\Metadata\Driver; | ||
|
||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use JMS\Serializer\Metadata\Driver\AnnotationDriver; | ||
use JMS\Serializer\Metadata\Driver\TypedPropertiesDriver; | ||
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; | ||
use JMS\Serializer\Tests\Fixtures\TypedProperties\User; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TypedPropertiesDriverTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (PHP_VERSION_ID < 70400) { | ||
$this->markTestSkipped(sprintf('%s requires PHP 7.4', TypedPropertiesDriver::class)); | ||
} | ||
} | ||
|
||
public function testInferPropertiesFromTypes() | ||
{ | ||
$baseDriver = new AnnotationDriver(new AnnotationReader(), new IdenticalPropertyNamingStrategy()); | ||
$driver = new TypedPropertiesDriver($baseDriver); | ||
|
||
$m = $driver->loadMetadataForClass(new \ReflectionClass(User::class)); | ||
|
||
self::assertNotNull($m); | ||
|
||
$expectedPropertyTypes = [ | ||
'id' => 'int', | ||
'role' => 'JMS\Serializer\Tests\Fixtures\TypedProperties\Role', | ||
'created' => 'DateTime', | ||
'tags' => 'iterable', | ||
]; | ||
|
||
foreach ($expectedPropertyTypes as $property => $type) { | ||
self::assertEquals(['name' => $type, 'params' => []], $m->propertyMetadata[$property]->type); | ||
} | ||
} | ||
} |