1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Schema Validator |
4
|
|
|
* |
5
|
|
|
* @author Vlad Shashkov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2021, The Myaza Software |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace SchemaValidator\Metadata; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
14
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
15
|
|
|
|
16
|
|
|
final class ClassMetadataFactoryWrapper implements ClassMetadataFactoryWrapperInterface |
17
|
|
|
{ |
18
|
6 |
|
public function __construct( |
19
|
|
|
private PropertyAccessorInterface $propertyAccessor, |
20
|
|
|
private ClassMetadataFactoryInterface $classMetadataFactory, |
21
|
|
|
) { |
22
|
6 |
|
} |
23
|
|
|
|
24
|
6 |
|
public function getMetadataFor(string $type, array $values): ClassMetadata |
25
|
|
|
{ |
26
|
6 |
|
$metadata = $this->classMetadataFactory->getMetadataFor($type); |
27
|
6 |
|
$reflectionClass = $metadata->getReflectionClass(); |
28
|
|
|
|
29
|
6 |
|
if ($reflectionClass->hasMethod('__construct')) { |
30
|
1 |
|
return new ClassMetadata( |
31
|
1 |
|
$metadata->getAttributesMetadata(), |
32
|
1 |
|
$reflectionClass->getMethod('__construct')->getParameters() |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
5 |
|
$mapping = $metadata->getClassDiscriminatorMapping(); |
37
|
|
|
|
38
|
5 |
|
if (null === $mapping) { |
39
|
1 |
|
throw new \RuntimeException('Not found constructor class:' . $type); |
40
|
|
|
} |
41
|
|
|
|
42
|
4 |
|
$propertyPath = $mapping->getTypeProperty(); |
43
|
|
|
/** @var string|null $value */ |
44
|
4 |
|
$value = $this->propertyAccessor->getValue($values, sprintf('[%s]', $propertyPath)); |
45
|
|
|
|
46
|
4 |
|
if (null === $value) { |
47
|
|
|
/** @var array<string> $mapValue */ |
48
|
1 |
|
$mapValue = array_keys($mapping->getTypesMapping()); |
49
|
1 |
|
$property = new Property($propertyPath, null); |
50
|
1 |
|
$mapping = new ClassDiscriminatorMapping($mapValue, $property); |
51
|
|
|
|
52
|
1 |
|
return new ClassMetadata([], [], $mapping); |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
$type = $mapping->getClassForType($value); |
56
|
|
|
|
57
|
3 |
|
if (null === $type) { |
58
|
|
|
/** @var array<string> $mapValue */ |
59
|
1 |
|
$mapValue = array_keys($mapping->getTypesMapping()); |
60
|
1 |
|
$property = new Property($propertyPath, $value, true); |
61
|
1 |
|
$mapping = new ClassDiscriminatorMapping($mapValue, $property); |
62
|
|
|
|
63
|
1 |
|
return new ClassMetadata([], [], $mapping); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
$metadata = $this->classMetadataFactory->getMetadataFor($type); |
67
|
2 |
|
$reflectionClass = $metadata->getReflectionClass(); |
68
|
|
|
|
69
|
2 |
|
if (!$reflectionClass->hasMethod('__construct')) { |
70
|
1 |
|
throw new \RuntimeException('Not found constructor class:' . $type); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return new ClassMetadata( |
74
|
1 |
|
$metadata->getAttributesMetadata(), |
75
|
1 |
|
$reflectionClass->getMethod('__construct')->getParameters() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|