Passed
Pull Request — main (#3)
by Peter
03:13
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\Attributes;
8
9
class ParsedTemplate
10
{
11
    protected string $type;
12
13
    protected string $identifier;
14
15
    protected Attributes $attributes;
16
17
    /** @var string[] */
18
    protected array $occurrences;
19
20
    /**
21
     * ParsedData constructor.
22
     *
23
     * @param string          $type
24
     * @param string          $identifier
25
     * @param Attributes|null $attributes
26
     * @param string[]        $occurrences
27
     */
28
    public function __construct(
29
        string $type,
30
        string $identifier,
31
        ?Attributes $attributes = null,
32
        array $occurrences = []
33
    ) {
34
        $this->type        = $type;
35
        $this->identifier  = $identifier;
36
        $this->attributes  = $attributes ?? new Attributes();
37
        $this->occurrences = $occurrences;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getType(): string
44
    {
45
        return $this->type;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getIdentifier(): string
52
    {
53
        return $this->identifier;
54
    }
55
56
    /**
57
     * @return Attributes
58
     */
59
    public function getAttributes(): Attributes
60
    {
61
        return $this->attributes;
62
    }
63
64
    /**
65
     * @param string      $key
66
     * @param string|null $default
67
     *
68
     * @return string|null
69
     */
70
    public function getAttributeValue(string $key, ?string $default = null): ?string
71
    {
72
        $attribute = $this->attributes->getItem($key);
73
        if ($attribute && !$attribute->isNull()) {
74
            return $attribute->getValue();
75
        }
76
77
        return $default;
78
    }
79
80
    /**
81
     * @param string $occurrence
82
     *
83
     * @return ParsedTemplate
84
     */
85
    public function addOccurrence(string $occurrence): ParsedTemplate
86
    {
87
        $this->occurrences[] = $occurrence;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return string[]
94
     */
95
    public function getOccurrences(): array
96
    {
97
        return $this->occurrences;
98
    }
99
}
100