Completed
Push — master ( 3e297a...bce447 )
by David
15s
created

AbstractAnnotation::isFromType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TheCodingMachine\Funky\Annotations;
5
6
use Doctrine\Common\Annotations\AnnotationException;
7
8
abstract class AbstractAnnotation
9
{
10
    /**
11
     * @var bool
12
     */
13
    private $nameFromType = false;
14
    /**
15
     * @var bool
16
     */
17
    private $nameFromMethodName = false;
18
    /**
19
     * @var string|null
20
     */
21
    private $name;
22
    /**
23
     * @var Tag[]
24
     */
25
    private $tags = [];
26
27
    /**
28
     * @param mixed[] $attributes
29
     */
30
    public function __construct(array $attributes = [])
31
    {
32
        $count = 0;
33
        if (isset($attributes['nameFromType'])) {
34
            $this->nameFromType = $attributes['nameFromType'];
35
            if ($this->nameFromType) {
36
                $count++;
37
            }
38
        }
39
        if (isset($attributes['nameFromMethodName'])) {
40
            $this->nameFromMethodName = $attributes['nameFromMethodName'];
41
            if ($this->nameFromMethodName) {
42
                $count++;
43
            }
44
        }
45
        if (isset($attributes['name'])) {
46
            $this->name = $attributes['name'];
47
            $count++;
48
        }
49
50
        if ($count === 0) {
51
            $this->nameFromType = true;
52
        }
53
        if ($count > 1) {
54
            throw new AnnotationException('There should be only one property in the list "name", "nameFromType", '
55
                .'"nameFromMethodName". You can add aliases if you need several names.');
56
        }
57
58
        if (isset($attributes['tags']) && \is_array($attributes['tags'])) {
59
            $this->setTags(...$attributes['tags']);
60
        }
61
    }
62
63
    /**
64
     * Helper method to ensure that the passed tags are of Tag type.
65
     *
66
     * @param Tag[] $tags
67
     */
68
    private function setTags(Tag ...$tags): void
69
    {
70
        $this->tags = $tags;
71
    }
72
    /**
73
     * Returns the list of tags for the bean instance. Returns an empty array when no tag was set.
74
     *
75
     * @return Tag[]
76
     */
77
    public function getTags(): array
78
    {
79
        return $this->tags;
80
    }
81
    public function getName(): ?string
82
    {
83
        return $this->name;
84
    }
85
    public function isFromType(): bool
86
    {
87
        return $this->nameFromType;
88
    }
89
    public function isFromMethodName(): bool
90
    {
91
        return $this->nameFromMethodName;
92
    }
93
}
94