Completed
Push — master ( 63d999...3eb304 )
by Gallice
02:54
created

Context::setReferenceTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 31
        $this->data = $data;
18 31
    }
19
20
    /**
21
     * @return array|Entity[]
22
     */
23 2
    public function getEntities()
24
    {
25 2
        return $this->getContextField('entities', []);
26
    }
27
28
    /**
29
     * @return array|Location
30
     */
31 2
    public function getLocation()
32
    {
33 2
        return $this->getContextField('location', []);
34
    }
35
36
    /**
37
     * Return the reference date in ISO8601 format
38
     *
39
     * @return string|null
40
     */
41 2
    public function getReferenceTime()
42
    {
43 2
        return $this->getContextField('reference_time');
44
    }
45
46
    /**
47
     * Set the reference time in ISO8601 format
48
     *
49
     * @param \DateTimeInterface $dateTime
50
     */
51 1
    public function setReferenceTime(\DateTimeInterface $dateTime)
52
    {
53 1
        $dt = $dateTime->format(DATE_ISO8601);
54 1
        $this->add('reference_time', $dt);
55 1
    }
56
57
    /**
58
     * @return array|string|null
59
     */
60 2
    public function getState()
61
    {
62 2
        return $this->getContextField('state');
63
    }
64
65
    /**
66
     * @return string|null
67
     */
68 2
    public function getTimezone()
69
    {
70 2
        return $this->getContextField('timezone');
71
    }
72
73
    /**
74
     * @param string $timezone Timezone identifier e.g 'Europe/Berlin'
75
     */
76 1
    public function setTimezone($timezone)
77
    {
78 1
        $tz = new \DateTimeZone($timezone);
79 1
        $this->add('timezone', $tz->getName());
80 1
    }
81
82
    /**
83
     * @param string $name
84
     * @param mixed $value
85
     */
86 10
    public function add($name, $value)
87
    {
88 10
        $this->data[$name] = $value;
89 10
    }
90
91
    /**
92
     * @param string $name
93
     *
94
     * @return mixed
95
     */
96 2
    public function get($name)
97
    {
98 2
        return $this->getContextField($name);
99
    }
100
101
    /**
102
     * @param string $name
103
     */
104 1
    public function remove($name)
105
    {
106 1
        unset($this->data[$name]);
107 1
    }
108
109
    /**
110
     * @param string $name
111
     *
112
     * @return bool
113
     */
114 13
    public function has($name)
115
    {
116 13
        return isset($this->data[$name]);
117
    }
118
119
    /**
120
     * @return array
121
     */
122 3
    public function jsonSerialize()
123
    {
124 3
        return $this->data;
125
    }
126
127
    /**
128
     * Check if the context is empty
129
     *
130
     * @return bool
131
     */
132 7
    public function isEmpty()
133
    {
134 7
        return empty($this->data);
135
    }
136
137
    /**
138
     * @param string $name
139
     * @param mixed $default
140
     *
141
     * @return mixed
142
     */
143 12
    private function getContextField($name, $default = null)
144
    {
145 12
        return $this->has($name) ? $this->data[$name] : $default;
146
    }
147
}
148