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

VisibilityChangeHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 1
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
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
8
9
class VisibilityChangeHandler implements \JsonSerializable
10
{
11
    /**
12
     * @param AbstractStandardCommand[] $commands list of commands to run (evaluated in order)
13
     * @param string|null $when APL boolean expression controlling whether this handler is considered. If null, treated as 'true'.
14
     * @param string|null $description optional description of this handler
15
     */
16 19
    public function __construct(
17
        public array $commands = [],
18
        public ?string $when = null,
19
        public ?string $description = null,
20
    ) {
21
        // Filter and ensure valid commands
22 19
        $this->commands = array_values(array_filter(
23 19
            $this->commands,
24 19
            static fn ($c) => $c instanceof AbstractStandardCommand
25 19
        ));
26
    }
27
28 12
    public function jsonSerialize(): array
29
    {
30 12
        $data = [];
31 12
        if ($this->commands !== []) {
32 6
            $data['commands'] = $this->commands;
33
        }
34 12
        if ($this->description !== null && $this->description !== '') {
35 6
            $data['description'] = $this->description;
36
        }
37 12
        if ($this->when !== null && $this->when !== '') {
38 6
            $data['when'] = $this->when;
39
        }
40
41 12
        return $data;
42
    }
43
}
44