Passed
Push — attributes ( 57ed6a )
by Peter
04:12
created

Attribute::remove()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Html;
6
7
class Attribute
8
{
9
    protected string $key;
10
11
    /** @var null|array<string,string> */
12
    protected ?array $values = null;
13
14
    /**
15
     * Attribute constructor.
16
     *
17
     * @param string $key
18
     * @param string ...$values
19
     */
20
    public function __construct(string $key, string ...$values)
21
    {
22
        $this->key = $key;
23
        $this->append(...$values);
24
    }
25
26
    /**
27
     * @param string ...$values
28
     *
29
     * @return $this
30
     */
31
    public function set(string ...$values): self
32
    {
33
        $this->values = [];
34
35
        return $this->unsafeSet(...$values);
36
    }
37
38
    /**
39
     * @param string ...$values
40
     *
41
     * @return $this
42
     */
43
    public function append(string ...$values): self
44
    {
45
        if (!$values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $values of type array<integer,string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
46
            return $this;
47
        }
48
49
        if ($this->values === null) {
50
            $this->values = [];
51
        }
52
53
        return $this->unsafeSet(...$values);
54
    }
55
56
    /**
57
     * @param string ...$values
58
     *
59
     * @return $this
60
     */
61
    private function unsafeSet(string ...$values): self
62
    {
63
        foreach ($values as $part) {
64
            if (empty($part)) {
65
                continue;
66
            }
67
68
            $this->values[$part] = $part;
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param string ...$value
76
     *
77
     * @return int
78
     */
79
    public function remove(string ...$value): int
80
    {
81
        $count = 0;
82
        foreach ($value as $key) {
83
            if (!array_key_exists($key, $this->values)) {
84
                continue;
85
            }
86
            unset($this->values[$key]);
87
            $count++;
88
        }
89
90
        return $count;
91
    }
92
93
    /**
94
     * @return $this
95
     */
96
    public function reset(): self
97
    {
98
        $this->values = null;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return string[]|null
105
     */
106
    public function getValues(): ?array
107
    {
108
        if (null === $this->values) {
109
            return null;
110
        }
111
112
        return array_values($this->values);
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getKey(): string
119
    {
120
        return $this->key;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function __toString(): string
127
    {
128
        if ($this->values === null) {
129
            return $this->key;
130
        }
131
132
        return sprintf('%s="%s"', $this->key, join(" ", $this->values));
133
    }
134
}
135