Passed
Push — master ( 0e2863...12b99e )
by Alexander
02:19
created

StateTrait::setLanguage()   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 Yiisoft\View\Theme;
9
10
use function array_key_exists;
11
use function func_get_args;
12
use function is_array;
13
14
/**
15
 * @internal
16
 */
17
trait StateTrait
18
{
19
    private ?Theme $theme = null;
20
    private string $language = 'en';
21
22
    /**
23
     * @var array Parameters that are common for all view templates.
24
     * @psalm-var array<string, mixed>
25
     */
26
    private array $parameters = [];
27
28
    /**
29
     * @var array Named content blocks that are common for all view templates.
30
     * @psalm-var array<string, string>
31
     */
32
    private array $blocks = [];
33
34
    /**
35
     * Set the specified theme instance.
36
     *
37
     * @param Theme|null $theme The theme instance or `null` for reset theme.
38
     *
39
     * @return static
40
     */
41 1
    public function setTheme(?Theme $theme): self
42
    {
43 1
        $this->theme = $theme;
44 1
        return $this;
45
    }
46
47
    /**
48
     * Gets the theme instance, or `null` if no theme has been set.
49
     *
50
     * @return Theme|null The theme instance, or `null` if no theme has been set.
51
     */
52 57
    public function getTheme(): ?Theme
53
    {
54 57
        return $this->theme;
55
    }
56
57
    /**
58
     * Set the specified language code.
59
     *
60
     * @param string $language The language code.
61
     *
62
     * @return static
63
     */
64 1
    public function setLanguage(string $language): self
65
    {
66 1
        $this->language = $language;
67 1
        return $this;
68
    }
69
70
    /**
71
     * Gets the language code.
72
     *
73
     * @return string The language code.
74
     */
75 58
    public function getLanguage(): string
76
    {
77 58
        return $this->language;
78
    }
79
80
    /**
81
     * Sets a common parameters that is accessible in all view templates.
82
     *
83
     * @param array $parameters Parameters that are common for all view templates.
84
     *
85
     * @psalm-param array<string, mixed> $parameters
86
     *
87
     * @return static
88
     *
89
     * @see setParameter()
90
     */
91 3
    public function setParameters(array $parameters): self
92
    {
93
        /** @var mixed $value */
94 3
        foreach ($parameters as $id => $value) {
95 1
            $this->setParameter($id, $value);
96
        }
97 3
        return $this;
98
    }
99
100
    /**
101
     * Sets a common parameter that is accessible in all view templates.
102
     *
103
     * @param string $id The unique identifier of the parameter.
104
     * @param mixed $value The value of the parameter.
105
     *
106
     * @return static
107
     */
108 15
    public function setParameter(string $id, $value): self
109
    {
110 15
        $this->parameters[$id] = $value;
111 15
        return $this;
112
    }
113
114
    /**
115
     * Add values to end of common array parameter. If specified parameter does not exist or him is not array,
116
     * then parameter will be added as empty array.
117
     *
118
     * @param string $id The unique identifier of the parameter.
119
     * @param mixed ...$value Value(s) for add to end of array parameter.
120
     *
121
     * @throws InvalidArgumentException When specified parameter already exists and is not an array.
122
     *
123
     * @return static
124
     */
125 6
    public function addToParameter(string $id, ...$value): self
126
    {
127
        /** @var mixed $array */
128 6
        $array = $this->parameters[$id] ?? [];
129 6
        if (!is_array($array)) {
130 1
            throw new InvalidArgumentException(
131 1
                sprintf('The "%s" parameter already exists and is not an array.', $id)
132
            );
133
        }
134
135 5
        $this->setParameter($id, array_merge($array, $value));
136
137 5
        return $this;
138
    }
139
140
    /**
141
     * Removes a common parameter.
142
     *
143
     * @param string $id The unique identifier of the parameter.
144
     *
145
     * @return static
146
     */
147 3
    public function removeParameter(string $id): self
148
    {
149 3
        unset($this->parameters[$id]);
150 3
        return $this;
151
    }
152
153
    /**
154
     * Gets a common parameter value by ID.
155
     *
156
     * @param string $id The unique identifier of the parameter.
157
     * @param mixed $default The default value to be returned if the specified parameter does not exist.
158
     *
159
     * @throws InvalidArgumentException If specified parameter does not exist and not passed default value.
160
     *
161
     * @return mixed The value of the parameter.
162
     */
163 8
    public function getParameter(string $id)
164
    {
165 8
        if (isset($this->parameters[$id])) {
166 6
            return $this->parameters[$id];
167
        }
168
169 3
        $args = func_get_args();
170 3
        if (array_key_exists(1, $args)) {
171 1
            return $args[1];
172
        }
173
174 2
        throw new InvalidArgumentException('Parameter "' . $id . '" not found.');
175
    }
176
177 57
    public function getParameters(): array
178
    {
179 57
        return $this->parameters;
180
    }
181
182
    /**
183
     * Checks the existence of a common parameter by ID.
184
     *
185
     * @param string $id The unique identifier of the parameter.
186
     *
187
     * @return bool Whether a custom parameter that is common for all view templates exists.
188
     */
189 5
    public function hasParameter(string $id): bool
190
    {
191 5
        return isset($this->parameters[$id]);
192
    }
193
194
    /**
195
     * Sets a content block.
196
     *
197
     * @param string $id The unique identifier of the block.
198
     * @param string $content The content of the block.
199
     *
200
     * @return static
201
     */
202 7
    public function setBlock(string $id, string $content): self
203
    {
204 7
        $this->blocks[$id] = $content;
205 7
        return $this;
206
    }
207
208
    /**
209
     * Removes a content block.
210
     *
211
     * @param string $id The unique identifier of the block.
212
     *
213
     * @return static
214
     */
215 3
    public function removeBlock(string $id): self
216
    {
217 3
        unset($this->blocks[$id]);
218 3
        return $this;
219
    }
220
221
    /**
222
     * Gets content of the block by ID.
223
     *
224
     * @param string $id The unique identifier of the block.
225
     *
226
     * @return string The content of the block.
227
     */
228 2
    public function getBlock(string $id): string
229
    {
230 2
        if (isset($this->blocks[$id])) {
231 1
            return $this->blocks[$id];
232
        }
233
234 2
        throw new InvalidArgumentException('Block "' . $id . '" not found.');
235
    }
236
237
    /**
238
     * Checks the existence of a content block by ID.
239
     *
240
     * @param string $id The unique identifier of the block.
241
     *
242
     * @return bool Whether a content block exists.
243
     */
244 5
    public function hasBlock(string $id): bool
245
    {
246 5
        return isset($this->blocks[$id]);
247
    }
248
}
249