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
namespace MaxBeckers\AmazonAlexa\Intent;
4
5
/**
6
 * @author Maximilian Beckers <[email protected]>
7
 */
8
class Resolution implements \JsonSerializable
9
{
10
    /**
11
     * @var string|null
12
     */
13
    public $authority;
14
15
    /**
16
     * @var IntentStatus|null
17
     */
18
    public $status;
19
20
    /**
21
     * @var IntentValue[]
22
     */
23
    public $values = [];
24
25
    /**
26
     * @param array $amazonRequest
27
     *
28
     * @return Resolution
29
     */
30 9
    public static function fromAmazonRequest(array $amazonRequest): self
31
    {
32 9
        $resolution = new self();
33
34 9
        $resolution->authority = isset($amazonRequest['authority']) ? $amazonRequest['authority'] : null;
35 9
        $resolution->status    = isset($amazonRequest['status']) ? IntentStatus::fromAmazonRequest($amazonRequest['status']) : null;
36
37 9
        if (isset($amazonRequest['values'])) {
38 9
            foreach ($amazonRequest['values'] as $value) {
39 9
                if (isset($value['value'])) {
40 9
                    $resolution->values[] = IntentValue::fromAmazonRequest($value['value']);
41
                }
42
            }
43
        }
44
45 9
        return $resolution;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 3
    public function jsonSerialize()
52
    {
53 3
        $data = [];
54 3
        if ($this->authority) {
55 3
            $data['authority'] = $this->authority;
56
        }
57 3
        if ($this->status) {
58 3
            $data['status'] = $this->status;
59
        }
60 3
        if (!empty($this->values)) {
61 3
            $data['values'] = [];
62 3
            foreach ($this->values as $value) {
63 3
                $data['values'][] = [
64 3
                    'value' => $value,
65 3
                ];
66
            }
67
        }
68
69 3
        return $data;
70
    }
71
}
72