TouchableComponent::jsonSerialize()   B
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.013

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 16
c 1
b 0
f 1
nc 128
nop 0
dl 0
loc 33
ccs 16
cts 17
cp 0.9412
crap 8.013
rs 8.2111
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\Gesture\AbstractGesture;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
10
11
abstract class TouchableComponent extends ActionableComponent implements \JsonSerializable
12
{
13
    /**
14
     * @param APLComponentType $type The type of the component
15
     * @param AbstractGesture|null $gesture Single gesture handler
16
     * @param AbstractGesture[]|null $gestures Array of gesture handlers
17
     * @param AbstractStandardCommand[]|null $onCancel Commands to run when a gesture takes over the pointer
18
     * @param AbstractStandardCommand[]|null $onDown Commands to run when a pointer down event occurs
19
     * @param AbstractStandardCommand[]|null $onMove Commands to run as the pointer moves
20
     * @param AbstractStandardCommand[]|null $onPress Commands to run for a pointer down followed by a pointer up
21
     * @param AbstractStandardCommand[]|null $onUp Commands to run when releasing the pointer
22
     */
23 20
    public function __construct(
24
        APLComponentType $type,
25
        public ?AbstractGesture $gesture = null,
26
        public ?array $gestures = null,
27
        public ?array $onCancel = null,
28
        public ?array $onDown = null,
29
        public ?array $onMove = null,
30
        public ?array $onPress = null,
31
        public ?array $onUp = null,
32
    ) {
33 20
        parent::__construct($type);
34
    }
35
36 12
    public function jsonSerialize(): array
37
    {
38 12
        $data = parent::jsonSerialize();
39
40 12
        if ($this->gesture !== null) {
41
            $data['gesture'] = $this->gesture;
42
        }
43
44 12
        if (!empty($this->gestures)) {
45 1
            $data['gestures'] = $this->gestures;
46
        }
47
48 12
        if (!empty($this->onCancel)) {
49 1
            $data['onCancel'] = $this->onCancel;
50
        }
51
52 12
        if (!empty($this->onDown)) {
53 1
            $data['onDown'] = $this->onDown;
54
        }
55
56 12
        if (!empty($this->onMove)) {
57 1
            $data['onMove'] = $this->onMove;
58
        }
59
60 12
        if (!empty($this->onPress)) {
61 1
            $data['onPress'] = $this->onPress;
62
        }
63
64 12
        if (!empty($this->onUp)) {
65 1
            $data['onUp'] = $this->onUp;
66
        }
67
68 12
        return $data;
69
    }
70
}
71