Completed
Push — master ( c79f7e...463ede )
by Gallice
02:34
created

Context::getContextField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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