Resolution::jsonSerialize()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.6111
cc 5
nc 8
nop 0
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Intent;
6
7
class Resolution implements \JsonSerializable
8
{
9
    public ?string $authority = null;
10
    public ?IntentStatus $status = null;
11
12
    /** @var IntentValue[] */
13
    public array $values = [];
14
15 9
    public static function fromAmazonRequest(array $amazonRequest): self
16
    {
17 9
        $resolution = new self();
18
19 9
        $resolution->authority = $amazonRequest['authority'] ?? null;
20 9
        $resolution->status = isset($amazonRequest['status']) ? IntentStatus::fromAmazonRequest($amazonRequest['status']) : null;
21
22 9
        if (isset($amazonRequest['values'])) {
23 9
            foreach ($amazonRequest['values'] as $value) {
24 9
                if (isset($value['value'])) {
25 9
                    $resolution->values[] = IntentValue::fromAmazonRequest($value['value']);
26
                }
27
            }
28
        }
29
30 9
        return $resolution;
31
    }
32
33 3
    public function jsonSerialize(): array
34
    {
35 3
        $data = [];
36 3
        if ($this->authority) {
37 3
            $data['authority'] = $this->authority;
38
        }
39 3
        if ($this->status) {
40 3
            $data['status'] = $this->status;
41
        }
42 3
        if (!empty($this->values)) {
43 3
            $data['values'] = [];
44 3
            foreach ($this->values as $value) {
45 3
                $data['values'][] = [
46 3
                    'value' => $value,
47 3
                ];
48
            }
49
        }
50
51 3
        return $data;
52
    }
53
}
54