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

SelectCommand::jsonSerialize()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 10
nc 16
nop 0
dl 0
loc 21
ccs 11
cts 11
cp 1
crap 8
rs 8.4444
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand;
6
7
class SelectCommand extends AbstractStandardCommand implements \JsonSerializable
8
{
9
    public const TYPE = 'Select';
10
11
    /**
12
     * @param string|null $componentId ID of the component to select
13
     * @param AbstractStandardCommand[]|null $commands Commands to run when condition is true
14
     * @param array|null $data Array of data to iterate over
15
     * @param AbstractStandardCommand[]|null $otherwise Commands to run when condition is false
16
     */
17 6
    public function __construct(
18
        public ?string $componentId = null,
19
        public ?array $commands = null,
20
        public ?array $data = null,
21
        public ?array $otherwise = null,
22
    ) {
23 6
        parent::__construct(self::TYPE);
24
    }
25
26 4
    public function jsonSerialize(): array
27
    {
28 4
        $data = parent::jsonSerialize();
29
30 4
        if ($this->componentId !== null) {
31 3
            $data['componentId'] = $this->componentId;
32
        }
33
34 4
        if ($this->commands !== null && !empty($this->commands)) {
35 2
            $data['commands'] = $this->commands;
36
        }
37
38 4
        if ($this->data !== null && !empty($this->data)) {
39 1
            $data['data'] = $this->data;
40
        }
41
42 4
        if ($this->otherwise !== null && !empty($this->otherwise)) {
43 1
            $data['otherwise'] = $this->otherwise;
44
        }
45
46 4
        return $data;
47
    }
48
}
49