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

ScrollViewComponent::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\Component;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\APLComponentType;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
9
10
class ScrollViewComponent extends ActionableComponent implements \JsonSerializable
11
{
12
    public const TYPE = APLComponentType::SCROLL_VIEW;
13
14
    /**
15
     * @param APLBaseComponent|null $item Single component displayed in the ScrollView
16
     * @param APLBaseComponent[]|null $items Array of components displayed in the ScrollView
17
     * @param string[]|null $preserve Properties to save when reinflating the document
18
     * @param AbstractStandardCommand[]|null $onScroll Commands to run during scrolling
19
     */
20 9
    public function __construct(
21
        public ?APLBaseComponent $item = null,
22
        public ?array $items = null,
23
        ?array $preserve = null,
24
        public ?array $onScroll = null,
25
    ) {
26 9
        parent::__construct(type: self::TYPE, preserve: $preserve);
27
    }
28
29 5
    public function jsonSerialize(): array
30
    {
31 5
        $data = parent::jsonSerialize();
32
33 5
        if ($this->item !== null) {
34 2
            $data['item'] = $this->item;
35
        }
36
37 5
        if ($this->items !== null && !empty($this->items)) {
38 2
            $data['items'] = $this->items;
39
        }
40
41 5
        if ($this->preserve !== null && !empty($this->preserve)) {
42 1
            $data['preserve'] = $this->preserve;
43
        }
44
45 5
        if ($this->onScroll !== null && !empty($this->onScroll)) {
46 1
            $data['onScroll'] = $this->onScroll;
47
        }
48
49 5
        return $data;
50
    }
51
}
52