Passed
Push — master ( c0b632...08da57 )
by Stf
02:15
created

Vivid::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * Articulately self-expressive type
5
 *
6
 * PHP version 7.4
7
 *
8
 * @category Support
9
 * @package  Chaospelt\Kernel\Support
10
 *
11
 * @author  Stf Kolev <[email protected]>
12
 * @license BSD-3-Clause https://opensource.org/licenses/BSD-3-Clause
13
 *
14
 * @link https://github.com/stfkolev/chaospelt
15
 */
16
17
namespace Chaospelt\Kernel\Support;
18
19
use Chaospelt\Kernel\Contracts\Arrayable;
20
use Chaospelt\Kernel\Contracts\Jsonable;
21
22
use ArrayAccess;
23
use JsonSerializable;
24
25
/**
26
 * Articulately self-expressive type
27
 * 
28
 * @template TKey of array-key
29
 * @template TValue
30
 *
31
 * @implements \Chaospelt\Kernel\Contracts\Arrayable<TKey, TValue>
32
 * @implements \ArrayAccess<TKey, TValue>
33
 *
34
 * @category Support
35
 * @package  Chaospelt\Kernel\Support
36
 *
37
 * @author  Stf Kolev <[email protected]>
38
 * @license BSD-3-Clause https://opensource.org/licenses/BSD-3-Clause
39
 *
40
 * @link https://github.com/stfkolev/chaospelt
41
 */
42
class Vivid
43
{
44
    /**
45
     * All of the attributes set on the vivid instance.
46
     *
47
     * @var array<TKey, TValue>
48
     */
49
    protected $attributes = [];
50
51
    /**
52
     * Create a new vivid instance.
53
     *
54
     * @param iterable<TKey, TValue> $attributes List of attributes to be passed
55
     * 
56
     * @return void
57
     */
58
    public function __construct($attributes = [])
59
    {
60
        foreach ($attributes as $key => $value) {
61
            $this->attributes[$key] = $value;
62
        }
63
    }
64
65
    /**
66
     * Get an attribute from the vivid instance.
67
     *
68
     * @param TKey                                  $key     Key of the attribute
69
     * @param TGetDefault|(\Closure(): TGetDefault) $default Default key parameter
0 ignored issues
show
Documentation Bug introduced by
The doc comment TGetDefault|(\Closure(): TGetDefault) at position 3 could not be parsed: Expected ')' at position 3, but found '\Closure'.
Loading history...
70
     * 
71
     * @return TValue|TGetDefault
72
     */
73
    public function get($key, $default = null)
74
    {
75
        if (array_key_exists($key, $this->attributes)) {
76
            return $this->attributes[$key];
77
        }
78
79
        return value($default);
0 ignored issues
show
Bug introduced by
The function value was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        return /** @scrutinizer ignore-call */ value($default);
Loading history...
80
    }
81
82
    /**
83
     * Get the attributes from the vivid instance.
84
     *
85
     * @return array<TKey, TValue>
86
     */
87
    public function getAttributes()
88
    {
89
        return $this->attributes;
90
    }
91
92
    /**
93
     * Convert the vivid instance to an array.
94
     *
95
     * @return array<TKey, TValue>
96
     */
97
    public function toArray()
98
    {
99
        return $this->attributes;
100
    }
101
102
    /**
103
     * Convert the object into something JSON serializable.
104
     *
105
     * @return array<TKey, TValue>
106
     */
107
    public function jsonSerialize(): array
108
    {
109
        return $this->toArray();
110
    }
111
112
    /**
113
     * Convert the fluent instance to JSON.
114
     *
115
     * @param int $options Options available to pass onto the json serialization
116
     * 
117
     * @return string
118
     */
119
    public function toJson($options = 0)
120
    {
121
        return json_encode($this->jsonSerialize(), $options);
122
    }
123
124
    /**
125
     * Determine if the given offset exists.
126
     *
127
     * @param TKey $offset Key of the attribute
0 ignored issues
show
Bug introduced by
The type Chaospelt\Kernel\Support\TKey was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
128
     * 
129
     * @return bool
130
     */
131
    public function offsetExists($offset): bool
132
    {
133
        return isset($this->attributes[$offset]);
134
    }
135
136
    /**
137
     * Get the value for a given offset.
138
     *
139
     * @param TKey $offset Key of the attribute
140
     * 
141
     * @return TValue|null
0 ignored issues
show
Bug introduced by
The type Chaospelt\Kernel\Support\TValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
142
     */
143
    public function offsetGet($offset): mixed
144
    {
145
        return $this->get($offset);
146
    }
147
148
    /**
149
     * Set the value at the given offset.
150
     *
151
     * @param TKey   $offset Key of the attribute
152
     * @param TValue $value  Value of the attribute
153
     * 
154
     * @return void
155
     */
156
    public function offsetSet($offset, $value): void
157
    {
158
        $this->attributes[$offset] = $value;
159
    }
160
161
    /**
162
     * Unset the value at the given offset.
163
     *
164
     * @param TKey $offset Key of the attribute
165
     * 
166
     * @return void
167
     */
168
    public function offsetUnset($offset): void
169
    {
170
        unset($this->attributes[$offset]);
171
    }
172
173
    /**
174
     * Handle dynamic calls to the vivid instance to set attributes.
175
     *
176
     * @param TKey              $method     Method instance to be called
177
     * @param array{0: ?TValue} $parameters List of method arguments
178
     * 
179
     * @return $this
180
     */
181
    public function __call($method, $parameters)
182
    {
183
        $this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true;
184
185
        return $this;
186
    }
187
188
    /**
189
     * Dynamically retrieve the value of an attribute.
190
     *
191
     * @param TKey $key Key of the attribute
192
     * 
193
     * @return TValue|null
194
     */
195
    public function __get($key)
196
    {
197
        return $this->get($key);
198
    }
199
200
    /**
201
     * Dynamically set the value of an attribute.
202
     *
203
     * @param TKey   $key   Key of the attribute
204
     * @param TValue $value Value of the attribute
205
     * 
206
     * @return void
207
     */
208
    public function __set($key, $value)
209
    {
210
        $this->offsetSet($key, $value);
211
    }
212
213
    /**
214
     * Dynamically check if an attribute is set.
215
     *
216
     * @param TKey $key Key of the attribute
217
     * 
218
     * @return bool
219
     */
220
    public function __isset($key)
221
    {
222
        return $this->offsetExists($key);
223
    }
224
225
    /**
226
     * Dynamically unset an attribute.
227
     *
228
     * @param TKey $key Key of the attribute
229
     * 
230
     * @return void
231
     */
232
    public function __unset($key)
233
    {
234
        $this->offsetUnset($key);
235
    }
236
}