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