Passed
Push — master ( 2f97ae...9b2be2 )
by Бабичев
01:39
created

Slice::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bavix\Slice;
4
5
use Bavix\Exceptions;
6
use Bavix\Helpers\Arr;
7
use Bavix\Helpers\Str;
8
use Bavix\Iterator\Iterator;
9
10
/**
11
 * Class Slice
12
 *
13
 * @package Bavix\Slice
14
 *
15
 * @method int getInt($offset)
16
 * @method float getFloat($offset)
17
 * @method bool getBool($offset)
18
 * @method string getEmail($offset)
19
 * @method string getIP($offset)
20
 * @method string getURL($offset)
21
 *
22
 * @method int getIntRequired($offset)
23
 * @method float getFloatRequired($offset)
24
 * @method bool getBoolRequired($offset)
25
 * @method string getEmailRequired($offset)
26
 * @method string getIPRequired($offset)
27
 * @method string getURLRequired($offset)
28
 */
29
class Slice extends Iterator
30
{
31
32
    /**
33
     * Slice constructor.
34
     *
35
     * @param array       $data
36
     * @param array|Slice $parameters
37
     */
38 12
    public function __construct(array $data, $parameters = null)
39
    {
40 12
        parent::__construct($data);
41
42 12
        if (null !== $parameters)
43
        {
44 12
            $this->walk($parameters);
45
        }
46 12
    }
47
48
    /**
49
     * @param array|\Traversable $data
50
     *
51
     * @return self
52
     */
53 1
    public static function from($data): self
54
    {
55 1
        if ($data instanceof self)
56
        {
57 1
            return $data;
58
        }
59
60 1
        return new static(
61 1
            Arr::iterator($data)
62
        );
63
    }
64
65
    /**
66
     * @param int|bool $depth
67
     *
68
     * @return array
69
     */
70 5
    public function asArray($depth = INF)
71
    {
72 5
        if (!$depth || $depth <= 0)
73
        {
74 1
            return $this->data;
75
        }
76
77 5
        $results = [];
78
79 5
        foreach (parent::asArray() as $key => $data)
80
        {
81 5
            $results[$key] =
82 5
                $data instanceof self ?
83 1
                    $data->asArray(\is_bool($depth) ? INF : --$depth) :
84 5
                    $data;
85
        }
86
87 5
        return $results;
88
    }
89
90
    /**
91
     * @param Slice|array $slice
92
     */
93 12
    protected function walk($slice)
94
    {
95 12
        if (\is_array($slice))
96
        {
97 12
            $slice = $this->make($slice);
98
        }
99
100 12
        Arr::walkRecursive($this->data, function (&$value) use ($slice) {
101
102 12
            if (\is_object($value) && $value instanceof Raw)
103
            {
104 12
                $value = $value->getData();
105
106 12
                return;
107
            }
108
109 12
            if (empty($value) || !\is_string($value))
110
            {
111
                return;
112
            }
113
114 12
            if (Str::first($value) === '%' &&
115 12
                Str::last($value) === '%' &&
116 12
                \substr_count($value, '%') === 2)
117
            {
118 12
                $path  = Str::sub($value, 1, -1);
119 12
                $value = $slice->getRequired($path);
120
            }
121
122 12
        });
123 12
    }
124
125
    /**
126
     * @param string $name
127
     * @param array  $arguments
128
     *
129
     * @return mixed
130
     */
131
    public function __call($name, $arguments)
132
    {
133
        list($offset) = $arguments;
134
135
        if (\strpos($name, 'Required') !== false)
136
        {
137
            return Filter::$name($this->getRequired($offset));
138
        }
139
140
        return Filter::$name($this->getData($offset));
141
    }
142
143
    /**
144
     * @return \Generator|Slice[]
145
     */
146 1
    public function asGenerator()
147
    {
148 1
        foreach ($this->data as $key => $object)
149
        {
150 1
            yield $key => $this->make($object);
151
        }
152 1
    }
153
154
    /**
155
     * @return array
156
     */
157 1
    public function keys()
158
    {
159 1
        return Arr::getKeys($this->data);
160
    }
161
162
    /**
163
     * @return Slice[]
164
     */
165 1
    public function asObject()
166
    {
167 1
        return Arr::iterator($this->asGenerator());
168
    }
169
170
    /**
171
     * @param array $data
172
     *
173
     * @return static
174
     */
175 12
    public function setData(array $data)
176
    {
177 12
        $this->data = $data;
178
179 12
        return $this;
180
    }
181
182
    /**
183
     * @param string $offset
184
     * @param mixed  $default
185
     *
186
     * @return mixed
187
     */
188 2
    public function getData($offset, $default = null)
189
    {
190 2
        return Arr::get($this->data, $offset, $default);
191
    }
192
193
    /**
194
     * @param string $offset
195
     *
196
     * @return Slice
197
     */
198 1
    public function getSlice($offset)
199
    {
200 1
        return $this->make($this->getRequired($offset));
201
    }
202
203
    /**
204
     * @param array $data
205
     *
206
     * @return Slice
207
     */
208 12
    public function make(array $data)
209
    {
210 12
        return (clone $this)->setData($data);
211
    }
212
213
    /**
214
     * @param string $offset
215
     *
216
     * @return array|mixed
217
     */
218 12
    public function getRequired($offset)
219
    {
220 12
        return Arr::getRequired($this->data, $offset);
221
    }
222
223
    /**
224
     * @inheritdoc
225
     */
226 1
    public function offsetExists($offset)
227
    {
228 1
        return Arr::get($this->data, $offset) !== null;
229
    }
230
231
    /**
232
     * @inheritdoc
233
     */
234 4
    public function offsetGet($offset)
235
    {
236 4
        return $this->getRequired($offset);
237
    }
238
239
    /**
240
     * @inheritdoc
241
     */
242 3
    public function offsetSet($offset, $value)
243
    {
244 3
        if (null === $offset)
245
        {
246 1
            throw new Exceptions\Invalid('Slice does not support NULL');
247
        }
248
249 2
        Arr::set($this->data, $offset, $value);
250 2
    }
251
252
    /**
253
     * @inheritdoc
254
     */
255 2
    public function offsetUnset($offset)
256
    {
257 2
        Arr::remove($this->data, $offset);
258 2
    }
259
260
}
261