Passed
Push — attributes ( 578d64...364588 )
by Peter
03:15
created

ParsedTemplate::getAttributeValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Template;
6
7
use AbterPhp\Framework\Html\Attribute;
8
use AbterPhp\Framework\Html\Attributes;
9
10
class ParsedTemplate
11
{
12
    protected string $type;
13
14
    protected string $identifier;
15
16
    protected Attributes $attributes;
17
18
    /** @var string[] */
19
    protected array $occurrences;
20
21
    /**
22
     * ParsedData constructor.
23
     *
24
     * @param string          $type
25
     * @param string          $identifier
26
     * @param Attributes|null $attributes
27
     * @param string[]        $occurrences
28
     */
29
    public function __construct(
30
        string $type,
31
        string $identifier,
32
        ?Attributes $attributes = null,
33
        array $occurrences = []
34
    ) {
35
        $this->type        = $type;
36
        $this->identifier  = $identifier;
37
        $this->attributes  = $attributes ?? new Attributes();
38
        $this->occurrences = $occurrences;
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getType(): string
45
    {
46
        return $this->type;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getIdentifier(): string
53
    {
54
        return $this->identifier;
55
    }
56
57
    /**
58
     * @return Attributes
59
     */
60
    public function getAttributes(): Attributes
61
    {
62
        return $this->attributes;
63
    }
64
65
    /**
66
     * @param string      $key
67
     * @param string|null $default
68
     *
69
     * @return string|null
70
     */
71
    public function getAttributeValue(string $key, ?string $default = null): ?string
72
    {
73
        $attribute = $this->attributes->getItem($key);
74
        if ($attribute && !$attribute->isNull()) {
75
            return $attribute->getValue();
76
        }
77
78
        return $default;
79
    }
80
81
    /**
82
     * @param string $occurrence
83
     *
84
     * @return ParsedTemplate
85
     */
86
    public function addOccurrence(string $occurrence): ParsedTemplate
87
    {
88
        $this->occurrences[] = $occurrence;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @return string[]
95
     */
96
    public function getOccurrences(): array
97
    {
98
        return $this->occurrences;
99
    }
100
}
101