Passed
Pull Request — master (#97)
by Maximilian
04:02
created

StyleValue::set()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
6
7
class StyleValue implements \JsonSerializable
8
{
9
    /**
10
     * When is an optional APL boolean expression controlling whether this style value block applies.
11
     * If null, the block always applies.
12
     */
13 17
    public function __construct(
14
        public ?string $when = null,
15
        public array $properties = [],
16
    ) {
17 17
    }
18
19 6
    public function set(string $name, mixed $value): self
20
    {
21 6
        if ($name === 'when') {
22 4
            $this->when = is_string($value) ? $value : null;
23
        } else {
24 3
            $this->properties[$name] = $value;
25
        }
26
27 6
        return $this;
28
    }
29
30 7
    public function jsonSerialize(): array
31
    {
32 7
        $out = $this->properties;
33 7
        if ($this->when !== null && $this->when !== '') {
34
            // Ensure 'when' appears first for readability (JSON object order not semantically relevant).
35 3
            $out = ['when' => $this->when] + $out;
36
        }
37
38 7
        return $out;
39
    }
40
}
41