Passed
Pull Request — master (#97)
by Maximilian
03:49
created

SendIndexListDataDirective   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 18
dl 0
loc 58
ccs 0
cts 22
cp 0
rs 10
c 1
b 0
f 1
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addItem() 0 3 1
A __construct() 0 9 1
A jsonSerialize() 0 29 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\Directive;
8
9
class SendIndexListDataDirective extends Directive implements \JsonSerializable
10
{
11
    public const TYPE = 'Alexa.Presentation.APL.SendIndexListData';
12
13
    /**
14
     * @param string $listId The identifier for the list to update with this response
15
     * @param int $startIndex Index of the first element in the items array
16
     * @param string|null $correlationToken The correlation token supplied in the LoadIndexListData request
17
     * @param int|null $listVersion The new version number for the list following the update
18
     * @param string|null $minimumInclusiveIndex The index of the first item in the array
19
     * @param string|null $maximumExclusiveIndex The last valid index of the array plus one
20
     * @param array $items Array of objects to add to the list
21
     */
22
    public function __construct(
23
        public string $listId,
24
        public int $startIndex,
25
        public ?string $correlationToken = null,
26
        public ?int $listVersion = null,
27
        public ?string $minimumInclusiveIndex = null,
28
        public ?string $maximumExclusiveIndex = null,
29
        public array $items = [],
30
    ) {
31
    }
32
33
    public function addItem(array $item): void
34
    {
35
        $this->items[] = $item;
36
    }
37
38
    public function jsonSerialize(): array
39
    {
40
        $data = [
41
            'type' => self::TYPE,
42
            'listId' => $this->listId,
43
            'startIndex' => $this->startIndex,
44
        ];
45
46
        if ($this->correlationToken !== null) {
47
            $data['correlationToken'] = $this->correlationToken;
48
        }
49
50
        if ($this->listVersion !== null) {
51
            $data['listVersion'] = $this->listVersion;
52
        }
53
54
        if ($this->minimumInclusiveIndex !== null) {
55
            $data['minimumInclusiveIndex'] = $this->minimumInclusiveIndex;
56
        }
57
58
        if ($this->maximumExclusiveIndex !== null) {
59
            $data['maximumExclusiveIndex'] = $this->maximumExclusiveIndex;
60
        }
61
62
        if (!empty($this->items)) {
63
            $data['items'] = $this->items;
64
        }
65
66
        return $data;
67
    }
68
}
69