|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Metadata\Driver; |
|
6
|
|
|
|
|
7
|
|
|
use JMS\Serializer\Metadata\ClassMetadata as SerializerClassMetadata; |
|
8
|
|
|
use JMS\Serializer\Metadata\ExpressionPropertyMetadata; |
|
9
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
|
10
|
|
|
use JMS\Serializer\Metadata\StaticPropertyMetadata; |
|
11
|
|
|
use JMS\Serializer\Metadata\VirtualPropertyMetadata; |
|
12
|
|
|
use Metadata\ClassMetadata; |
|
13
|
|
|
use Metadata\Driver\DriverInterface; |
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
use ReflectionException; |
|
16
|
|
|
|
|
17
|
|
|
class EnumPropertiesDriver implements DriverInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var DriverInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $delegate; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(DriverInterface $delegate) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->delegate = $delegate; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata |
|
30
|
|
|
{ |
|
31
|
|
|
$classMetadata = $this->delegate->loadMetadataForClass($class); |
|
32
|
|
|
\assert($classMetadata instanceof SerializerClassMetadata); |
|
33
|
|
|
|
|
34
|
|
|
// We base our scan on the internal driver's property list so that we |
|
35
|
|
|
// respect any internal allow/blocklist like in the AnnotationDriver |
|
36
|
|
|
foreach ($classMetadata->propertyMetadata as $propertyMetadata) { |
|
37
|
|
|
// If the inner driver provides a type, don't guess anymore. |
|
38
|
|
|
if ($this->isVirtualProperty($propertyMetadata)) { |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (!isset($propertyMetadata->type['name']) || !enum_exists($propertyMetadata->type['name'])) { |
|
43
|
|
|
continue; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
try { |
|
47
|
|
|
$enumReflection = new \ReflectionEnum($propertyMetadata->type['name']); |
|
48
|
|
|
$serializerType = ['name' => 'enum', 'params' => [$enumReflection->getName(), $enumReflection->isBacked() ? 'value' : 'name']]; |
|
|
|
|
|
|
49
|
|
|
$propertyMetadata->setType($serializerType); |
|
|
|
|
|
|
50
|
|
|
} catch (ReflectionException $e) { |
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $classMetadata; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function isVirtualProperty(PropertyMetadata $propertyMetadata): bool |
|
59
|
|
|
{ |
|
60
|
|
|
return $propertyMetadata instanceof VirtualPropertyMetadata |
|
61
|
|
|
|| $propertyMetadata instanceof StaticPropertyMetadata |
|
62
|
|
|
|| $propertyMetadata instanceof ExpressionPropertyMetadata; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.