Completed
Pull Request — master (#247)
by Michael
02:36
created

AnnotationMetadata::getDefaultProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Metadata;
6
7
use Doctrine\Annotations\Metadata\Exception\TooManyDefaultProperties;
8
use function array_combine;
9
use function array_filter;
10
use function array_map;
11
use function array_values;
12
use function count;
13
14
/**
15
 * @internal
16
 */
17
final class AnnotationMetadata
18
{
19
    /** @var string */
20
    private $name;
21
22
    /** @var AnnotationTarget */
23
    private $target;
24
25
    /** @var bool */
26
    private $usesConstructor;
27
28
    /** @var array<string, PropertyMetadata> */
29
    private $properties;
30
31
    /** @var PropertyMetadata|null */
32
    private $defaultProperty;
33
34
    /**
35
     * @param PropertyMetadata[] $properties
36
     */
37
    public function __construct(
38
        string $name,
39
        AnnotationTarget $target,
40
        bool $hasConstructor,
41
        PropertyMetadata ...$properties
42
    ) {
43
        $this->name            = $name;
44
        $this->target          = $target;
45
        $this->usesConstructor = $hasConstructor;
46
        $this->properties      = array_combine(
47
            array_map(
48
                static function (PropertyMetadata $property) : string {
49
                    return $property->getName();
50
                },
51
                $properties
52
            ),
53
            $properties
54
        );
55
56
        $defaultProperties = array_filter(
57
            $properties,
58
            static function (PropertyMetadata $property) : bool {
59
                return $property->isDefault();
60
            }
61
        );
62
63
        if (count($defaultProperties) > 1) {
64
            throw TooManyDefaultProperties::new();
65
        }
66
67
        $this->defaultProperty = array_values($defaultProperties)[0] ?? null;
68
    }
69
70
    public function getName() : string
71
    {
72
        return $this->name;
73
    }
74
75
    public function getTarget() : AnnotationTarget
76
    {
77
        return $this->target;
78
    }
79
80
    public function usesConstructor() : bool
81
    {
82
        return $this->usesConstructor;
83
    }
84
85
    /**
86
     * @return array<string, PropertyMetadata>
87
     */
88
    public function getProperties() : array
89
    {
90
        return $this->properties;
91
    }
92
93
    public function getDefaultProperty() : ?PropertyMetadata
94
    {
95
        return $this->defaultProperty;
96
    }
97
}
98