Passed
Push — master ( 204e2b...dee98e )
by Gallice
03:34
created

Context   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 122
ccs 0
cts 54
cp 0
rs 10

12 Methods

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