1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Annotations\Constructor; |
6
|
|
|
|
7
|
|
|
use Doctrine\Annotations\Assembler\Validator\Exception\InvalidAnnotationValue; |
8
|
|
|
use Doctrine\Annotations\Assembler\Validator\Exception\InvalidPropertyValue; |
9
|
|
|
use Doctrine\Annotations\Assembler\Validator\TargetValidator; |
10
|
|
|
use Doctrine\Annotations\Assembler\Validator\ValueValidator; |
11
|
|
|
use Doctrine\Annotations\Constructor\Instantiator\Instantiator; |
12
|
|
|
use Doctrine\Annotations\Metadata\AnnotationMetadata; |
13
|
|
|
use Doctrine\Annotations\Metadata\PropertyMetadata; |
14
|
|
|
use Doctrine\Annotations\Parser\Scope; |
15
|
|
|
|
16
|
|
|
final class Constructor |
17
|
|
|
{ |
18
|
|
|
/** @var Instantiator */ |
19
|
|
|
private $instantiator; |
20
|
|
|
|
21
|
11 |
|
public function __construct(Instantiator $instantiator) |
22
|
|
|
{ |
23
|
11 |
|
$this->instantiator = $instantiator; |
24
|
11 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param mixed[] $parameters iterable<string, mixed> |
28
|
|
|
*/ |
29
|
11 |
|
public function construct(AnnotationMetadata $annotationMetadata, Scope $scope, iterable $parameters) : object |
30
|
|
|
{ |
31
|
11 |
|
(new TargetValidator())->validate($annotationMetadata, $scope); |
32
|
|
|
|
33
|
11 |
|
if (! $annotationMetadata->hasConstructor()) { |
34
|
10 |
|
$this->validateProperties($annotationMetadata, $parameters); |
35
|
|
|
} |
36
|
|
|
|
37
|
11 |
|
return $this->instantiator->instantiate($annotationMetadata, $parameters); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed[] $parameters |
42
|
|
|
*/ |
43
|
10 |
|
private function validateProperties(AnnotationMetadata $annotationMetadata, iterable $parameters) : void |
44
|
|
|
{ |
45
|
10 |
|
foreach ($parameters as $propertyName => $propertyValue) { |
46
|
4 |
|
$propertyMetadata = $this->getPropertyMetadata($annotationMetadata, $propertyName); |
47
|
|
|
try { |
48
|
4 |
|
(new ValueValidator())->validate($propertyMetadata, $propertyValue); |
49
|
|
|
} catch (InvalidPropertyValue $exception) { |
50
|
4 |
|
throw InvalidAnnotationValue::new($annotationMetadata, $exception); |
51
|
|
|
} |
52
|
|
|
} |
53
|
10 |
|
} |
54
|
|
|
|
55
|
4 |
|
private function getPropertyMetadata(AnnotationMetadata $annotationMetadata, string $propertyName) : PropertyMetadata |
56
|
|
|
{ |
57
|
4 |
|
if ($propertyName === '') { |
58
|
|
|
/** @var PropertyMetadata $defaultProperty */ |
59
|
|
|
$defaultProperty = $annotationMetadata->getDefaultProperty(); |
60
|
|
|
|
61
|
|
|
$propertyName = $defaultProperty->getName(); |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
return $annotationMetadata->getProperties()[$propertyName]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|