Completed
Pull Request — master (#20)
by Gallice
02:59
created

Context::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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