Passed
Pull Request — master (#243)
by
unknown
02:58
created

StateTrait::setTheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View\State;
6
7
use InvalidArgumentException;
8
use function array_key_exists;
9
use function func_get_args;
10
use function is_array;
11
12
/**
13
 * @internal
14
 */
15
trait StateTrait
16
{
17
    /**
18
     * @var array Parameters that are common for all view templates.
19
     * @psalm-var array<string, mixed>
20
     */
21
    private array $parameters = [];
22
23
    /**
24
     * @var array Named content blocks that are common for all view templates.
25
     * @psalm-var array<string, string>
26
     */
27
    private array $blocks = [];
28
29
    /**
30
     * Sets a common parameters that is accessible in all view templates.
31
     *
32
     * @param array $parameters Parameters that are common for all view templates.
33
     *
34
     * @psalm-param array<string, mixed> $parameters
35
     *
36
     * @see setParameter()
37
     */
38 5
    public function setParameters(array $parameters): static
39
    {
40
        /** @var mixed $value */
41 5
        foreach ($parameters as $id => $value) {
42 1
            $this->setParameter($id, $value);
43
        }
44 5
        return $this;
45
    }
46
47
    /**
48
     * Sets a common parameter that is accessible in all view templates.
49
     *
50
     * @param string $id The unique identifier of the parameter.
51
     * @param mixed $value The value of the parameter.
52
     */
53 15
    public function setParameter(string $id, mixed $value): static
54
    {
55 15
        $this->parameters[$id] = $value;
56 15
        return $this;
57
    }
58
59
    /**
60
     * Add values to end of common array parameter. If specified parameter does not exist or him is not array,
61
     * then parameter will be added as empty array.
62
     *
63
     * @param string $id The unique identifier of the parameter.
64
     * @param mixed ...$value Value(s) for add to end of array parameter.
65
     *
66
     * @throws InvalidArgumentException When specified parameter already exists and is not an array.
67
     */
68 6
    public function addToParameter(string $id, mixed ...$value): static
69
    {
70
        /** @var mixed $array */
71 6
        $array = $this->parameters[$id] ?? [];
72 6
        if (!is_array($array)) {
73 1
            throw new InvalidArgumentException(
74 1
                sprintf('The "%s" parameter already exists and is not an array.', $id)
75 1
            );
76
        }
77
78 5
        $this->setParameter($id, array_merge($array, $value));
79
80 5
        return $this;
81
    }
82
83
    /**
84
     * Removes a common parameter.
85
     *
86
     * @param string $id The unique identifier of the parameter.
87
     */
88 3
    public function removeParameter(string $id): static
89
    {
90 3
        unset($this->parameters[$id]);
91 3
        return $this;
92
    }
93
94
    /**
95
     * Gets a common parameter value by ID.
96
     *
97
     * @param string $id The unique identifier of the parameter.
98
     * @param mixed $default The default value to be returned if the specified parameter does not exist.
99
     *
100
     * @throws InvalidArgumentException If specified parameter does not exist and not passed default value.
101
     *
102
     * @return mixed The value of the parameter.
103
     */
104 8
    public function getParameter(string $id)
105
    {
106 8
        if (isset($this->parameters[$id])) {
107 6
            return $this->parameters[$id];
108
        }
109
110 3
        $args = func_get_args();
111 3
        if (array_key_exists(1, $args)) {
112 1
            return $args[1];
113
        }
114
115 2
        throw new InvalidArgumentException('Parameter "' . $id . '" not found.');
116
    }
117
118 64
    public function getParameters(): array
119
    {
120 64
        return $this->parameters;
121
    }
122
123
    /**
124
     * Checks the existence of a common parameter by ID.
125
     *
126
     * @param string $id The unique identifier of the parameter.
127
     *
128
     * @return bool Whether a custom parameter that is common for all view templates exists.
129
     */
130 5
    public function hasParameter(string $id): bool
131
    {
132 5
        return isset($this->parameters[$id]);
133
    }
134
135
    /**
136
     * Sets a content block.
137
     *
138
     * @param string $id The unique identifier of the block.
139
     * @param string $content The content of the block.
140
     */
141 7
    public function setBlock(string $id, string $content): static
142
    {
143 7
        $this->blocks[$id] = $content;
144 7
        return $this;
145
    }
146
147
    /**
148
     * Removes a content block.
149
     *
150
     * @param string $id The unique identifier of the block.
151
     */
152 3
    public function removeBlock(string $id): static
153
    {
154 3
        unset($this->blocks[$id]);
155 3
        return $this;
156
    }
157
158
    /**
159
     * Gets content of the block by ID.
160
     *
161
     * @param string $id The unique identifier of the block.
162
     *
163
     * @return string The content of the block.
164
     */
165 2
    public function getBlock(string $id): string
166
    {
167 2
        if (isset($this->blocks[$id])) {
168 1
            return $this->blocks[$id];
169
        }
170
171 2
        throw new InvalidArgumentException('Block "' . $id . '" not found.');
172
    }
173
174
    /**
175
     * Checks the existence of a content block by ID.
176
     *
177
     * @param string $id The unique identifier of the block.
178
     *
179
     * @return bool Whether a content block exists.
180
     */
181 5
    public function hasBlock(string $id): bool
182
    {
183 5
        return isset($this->blocks[$id]);
184
    }
185
}
186