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

SendTokenListDataDirective::jsonSerialize()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 11
nc 8
nop 0
dl 0
loc 21
ccs 0
cts 13
cp 0
crap 20
rs 9.9
c 1
b 0
f 1
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 SendTokenListDataDirective extends Directive implements \JsonSerializable
10
{
11
    public const TYPE = 'Alexa.Presentation.APL.SendTokenListData';
12
13
    /**
14
     * @param string $listId The identifier of the list whose items are contained in this response
15
     * @param string $pageToken Opaque token for the array of items which are contained in this response
16
     * @param string|null $correlationToken The correlation token supplied in the LoadTokenListData request
17
     * @param string|null $nextPageToken Opaque token to retrieve the next page of list items data
18
     * @param array $items Array of objects to be added to the list
19
     */
20
    public function __construct(
21
        public string $listId,
22
        public string $pageToken,
23
        public ?string $correlationToken = null,
24
        public ?string $nextPageToken = null,
25
        public array $items = [],
26
    ) {
27
    }
28
29
    public function addItem(array $item): void
30
    {
31
        $this->items[] = $item;
32
    }
33
34
    public function jsonSerialize(): array
35
    {
36
        $data = [
37
            'type' => self::TYPE,
38
            'listId' => $this->listId,
39
            'pageToken' => $this->pageToken,
40
        ];
41
42
        if ($this->correlationToken !== null) {
43
            $data['correlationToken'] = $this->correlationToken;
44
        }
45
46
        if ($this->nextPageToken !== null) {
47
            $data['nextPageToken'] = $this->nextPageToken;
48
        }
49
50
        if (!empty($this->items)) {
51
            $data['items'] = $this->items;
52
        }
53
54
        return $data;
55
    }
56
}
57