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