1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tgallice\Wit\Model; |
4
|
|
|
|
5
|
|
|
class Context implements \JsonSerializable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
private $data; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param array $data |
14
|
|
|
*/ |
15
|
29 |
|
public function __construct($data = []) |
16
|
|
|
{ |
17
|
|
|
// Ensure the refenre_date field |
18
|
29 |
|
if (empty($data['reference_time'])) { |
19
|
29 |
|
$timezone = isset($data['timezone']) ? new \DateTimeZone($data['timezone']) : null; |
20
|
29 |
|
$data['reference_time'] = new \DateTimeImmutable('now', $timezone); |
21
|
29 |
|
} |
22
|
|
|
|
23
|
29 |
|
if ($data['reference_time'] instanceof \DateTimeInterface) { |
24
|
29 |
|
$data['reference_time'] = $data['reference_time']->format(DATE_ISO8601); |
25
|
29 |
|
} |
26
|
|
|
|
27
|
29 |
|
$this->data = $data; |
28
|
29 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return array|Entity[] |
32
|
|
|
*/ |
33
|
2 |
|
public function getEntities() |
34
|
|
|
{ |
35
|
2 |
|
return $this->getContextField('entities', []); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return array|Location |
40
|
|
|
*/ |
41
|
2 |
|
public function getLocation() |
42
|
|
|
{ |
43
|
2 |
|
return $this->getContextField('location', []); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return the reference date in the ISO8601 format |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
2 |
|
public function getReferenceTime() |
52
|
|
|
{ |
53
|
2 |
|
return $this->getContextField('reference_time'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array|string|null |
58
|
|
|
*/ |
59
|
2 |
|
public function getState() |
60
|
|
|
{ |
61
|
2 |
|
return $this->getContextField('state'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string|null |
66
|
|
|
*/ |
67
|
2 |
|
public function getTimezone() |
68
|
|
|
{ |
69
|
2 |
|
return $this->getContextField('timezone'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $name |
74
|
|
|
* @param mixed $value |
75
|
|
|
*/ |
76
|
6 |
|
public function add($name, $value) |
77
|
|
|
{ |
78
|
6 |
|
$this->data[$name] = $value; |
79
|
6 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $name |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
2 |
|
public function get($name) |
87
|
|
|
{ |
88
|
2 |
|
return $this->getContextField($name); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $name |
93
|
|
|
*/ |
94
|
1 |
|
public function remove($name) |
95
|
|
|
{ |
96
|
1 |
|
unset($this->data[$name]); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $name |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
13 |
|
public function has($name) |
105
|
|
|
{ |
106
|
13 |
|
return isset($this->data[$name]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
5 |
|
public function jsonSerialize() |
113
|
|
|
{ |
114
|
5 |
|
return $this->data; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $name |
119
|
|
|
* @param mixed $default |
120
|
|
|
* |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
12 |
|
private function getContextField($name, $default = null) |
124
|
|
|
{ |
125
|
12 |
|
return $this->has($name) ? $this->data[$name] : $default; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|