Test Failed
Push — master ( 690100...f55788 )
by Kirill
09:19
created

ValueObject   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 230
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 8 3
A set() 0 10 3
A setAttributes() 0 8 2
A getAttributes() 0 4 1
A toArray() 0 4 1
A jsonSerialize() 0 12 3
A toJson() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A __call() 0 6 2
A __get() 0 4 1
A __set() 0 4 1
A __isset() 0 4 1
A __unset() 0 4 1
A __debugInfo() 0 4 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\IR;
11
12
/**
13
 * Class ValueObject
14
 */
15
class ValueObject implements \ArrayAccess, \JsonSerializable
16
{
17
    /**
18
     * All of the attributes set on the container.
19
     *
20
     * @var array
21
     */
22
    protected $attributes = [];
23
24
    /**
25
     * Create a new ValueObject container instance.
26
     *
27
     * @param iterable $attributes
28
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
29
     */
30
    public function __construct(iterable $attributes = [])
31
    {
32
        $this->setAttributes($attributes);
33
    }
34
35
    /**
36
     * Get an attribute from the container.
37
     *
38
     * @param string $key
39
     * @param mixed $default
40
     * @return mixed
41
     */
42
    public function get(string $key, $default = null)
43
    {
44
        if (\array_key_exists($key, $this->attributes)) {
45
            return $this->attributes[$key];
46
        }
47
48
        return \is_callable($default) ? $default() : $default;
49
    }
50
51
    /**
52
     * Set an attribute to the container.
53
     *
54
     * @param string|int $key
55
     * @param mixed $value
56
     * @return $this
57
     */
58
    public function set($key, $value): self
59
    {
60
        if ($key === null) {
61
            $key = \count($this->attributes);
62
        }
63
64
        $this->attributes[$key] = \is_iterable($value) ? new ValueObject($value) : $value;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Set the attributes to the container.
71
     *
72
     * @param iterable $attributes
73
     * @return $this
74
     */
75
    public function setAttributes(iterable $attributes = []): self
76
    {
77
        foreach ($attributes as $key => $value) {
78
            $this->set($key, $value);
79
        }
80
81
        return $this;
82
    }
83
84
    /**
85
     * Get the attributes from the container.
86
     *
87
     * @return array
88
     */
89
    public function getAttributes(): array
90
    {
91
        return $this->attributes;
92
    }
93
94
    /**
95
     * Convert the ValueObject instance to an array.
96
     *
97
     * @return array
98
     */
99
    public function toArray(): array
100
    {
101
        return $this->attributes;
102
    }
103
104
    /**
105
     * Convert the object into something JSON serializable.
106
     *
107
     * @return array
108
     */
109
    public function jsonSerialize(): array
110
    {
111
        $applicator = function ($value) {
112
            if (\is_object($value) && \method_exists($value, '__toString')) {
113
                return (string)$value;
114
            }
115
116
            return $value;
117
        };
118
119
        return \array_map($applicator, $this->toArray());
120
    }
121
122
    /**
123
     * Convert the ValueObject instance to JSON.
124
     *
125
     * @param int $options
126
     * @return string
127
     */
128
    public function toJson(int $options = 0): string
129
    {
130
        return (string)\json_encode($this->jsonSerialize(), $options);
131
    }
132
133
    /**
134
     * Determine if the given offset exists.
135
     *
136
     * @param string $offset
137
     * @return bool
138
     */
139
    public function offsetExists($offset): bool
140
    {
141
        return isset($this->attributes[$offset]);
142
    }
143
144
    /**
145
     * Get the value for a given offset.
146
     *
147
     * @param string|int $offset
148
     * @return mixed
149
     */
150
    public function offsetGet($offset)
151
    {
152
        return $this->get($offset);
153
    }
154
155
    /**
156
     * Set the value at the given offset.
157
     *
158
     * @param string|int $offset
159
     * @param mixed $value
160
     * @return void
161
     */
162
    public function offsetSet($offset, $value): void
163
    {
164
        $this->set($offset, $value);
165
    }
166
167
    /**
168
     * Unset the value at the given offset.
169
     *
170
     * @param string $offset
171
     * @return void
172
     */
173
    public function offsetUnset($offset): void
174
    {
175
        unset($this->attributes[$offset]);
176
    }
177
178
    /**
179
     * Handle dynamic calls to the container to set attributes.
180
     *
181
     * @param string $method
182
     * @param array $parameters
183
     * @return $this
184
     */
185
    public function __call(string $method, array $parameters = []): self
186
    {
187
        $this->attributes[$method] = \count($parameters) > 0 ? $parameters[0] : true;
188
189
        return $this;
190
    }
191
192
    /**
193
     * Dynamically retrieve the value of an attribute.
194
     *
195
     * @param string $key
196
     * @return mixed
197
     */
198
    public function __get(string $key)
199
    {
200
        return $this->get($key);
201
    }
202
203
    /**
204
     * Dynamically set the value of an attribute.
205
     *
206
     * @param string $key
207
     * @param mixed $value
208
     * @return void
209
     */
210
    public function __set(string $key, $value): void
211
    {
212
        $this->offsetSet($key, $value);
213
    }
214
215
    /**
216
     * Dynamically check if an attribute is set.
217
     *
218
     * @param string $key
219
     * @return bool
220
     */
221
    public function __isset(string $key): bool
222
    {
223
        return $this->offsetExists($key);
224
    }
225
226
    /**
227
     * Dynamically unset an attribute.
228
     *
229
     * @param string $key
230
     * @return void
231
     */
232
    public function __unset(string $key): void
233
    {
234
        $this->offsetUnset($key);
235
    }
236
237
    /**
238
     * @return array
239
     */
240
    public function __debugInfo(): array
241
    {
242
        return $this->attributes;
243
    }
244
}
245