Passed
Push — master ( 09c644...07947a )
by Бабичев
03:38
created

Slice::from()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Bavix\Slice;
4
5
use Bavix\Helpers\Arr;
6
use Bavix\Iterator\Iterator;
7
use Bavix\Exceptions;
8
9
class Slice extends Iterator
10
{
11
12
    /**
13
     * Slice constructor.
14
     *
15
     * @param array       $data
16
     * @param array|Slice $parameters
17
     */
18 12
    public function __construct(array $data, $parameters = null)
19
    {
20 12
        parent::__construct($data);
21
22 12
        if (null !== $parameters)
23
        {
24 12
            $this->walk($parameters);
25
        }
26 12
    }
27
28
    /**
29
     * @param array|\Traversable $data
30
     *
31
     * @return self
32
     */
33 1
    public static function from($data): self
34
    {
35 1
        if ($data instanceof self)
36
        {
37 1
            return $data;
38
        }
39
40 1
        return new static(
41 1
            Arr::iterator($data)
42
        );
43
    }
44
45
    /**
46
     * @param int  $depth
47
     *
48
     * @return array
49
     */
50 5
    public function asArray($depth = INF)
51
    {
52 5
        if (!$depth || $depth <= 0)
53
        {
54 1
            return $this->data;
55
        }
56
57 5
        $results = [];
58
59 5
        foreach (parent::asArray() as $key => $data)
60
        {
61 5
            $results[$key] =
62 5
                $data instanceof self ?
63 1
                    $data->asArray(is_bool($data) ? INF : --$depth) :
64 5
                    $data;
65
        }
66
67 5
        return $results;
68
    }
69
70
    /**
71
     * @param Slice|array $slice
72
     */
73 12
    protected function walk($slice)
74
    {
75 12
        if (is_array($slice))
76
        {
77 12
            $slice = $this->make($slice);
78
        }
79
80 12
        Arr::walkRecursive($this->data, function (&$value) use ($slice)
81
        {
82 12
            if (is_string($value) && $value{0} === '%' && $value{\strlen($value) - 1} === '%')
83
            {
84 12
                $path  = \substr($value, 1, -1);
85 12
                $value = $slice->getData($path);
86
            }
87 12
        });
88 12
    }
89
90
    /**
91
     * @return \Generator|Slice[]
92
     */
93 1
    public function asGenerator()
94
    {
95 1
        foreach ($this->data as $key => $object)
96
        {
97 1
            yield $key => $this->make($object);
98
        }
99 1
    }
100
101
    /**
102
     * @return array
103
     */
104 1
    public function keys()
105
    {
106 1
        return Arr::getKeys($this->data);
107
    }
108
109
    /**
110
     * @return Slice[]
111
     */
112 1
    public function asObject()
113
    {
114 1
        return Arr::iterator($this->asGenerator());
115
    }
116
117
    /**
118
     * @param array $data
119
     *
120
     * @return static
121
     */
122 12
    public function setData(array $data)
123
    {
124 12
        $this->data = $data;
125
126 12
        return $this;
127
    }
128
129
    /**
130
     * @param string $offset
131
     * @param mixed  $default
132
     *
133
     * @return mixed
134
     */
135 12
    public function getData($offset, $default = null)
136
    {
137 12
        return Arr::get($this->data, $offset, $default);
138
    }
139
140
    /**
141
     * @param string $offset
142
     *
143
     * @return Slice
144
     */
145 1
    public function getSlice($offset)
146
    {
147 1
        return $this->make($this->getRequired($offset));
148
    }
149
150
    /**
151
     * @param array $data
152
     *
153
     * @return Slice
154
     */
155 12
    public function make(array $data)
156
    {
157 12
        return (clone $this)->setData($data);
158
    }
159
160
    /**
161
     * @param string $offset
162
     *
163
     * @return array|mixed
164
     */
165 3
    public function getRequired($offset)
166
    {
167 3
        return Arr::getRequired($this->data, $offset);
168
    }
169
170
    /**
171
     * @inheritdoc
172
     */
173 1
    public function offsetExists($offset)
174
    {
175 1
        return Arr::get($this->data, $offset) !== null;
176
    }
177
178
    /**
179
     * @inheritdoc
180
     */
181 3
    public function offsetGet($offset)
182
    {
183 3
        return $this->getRequired($offset);
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189 3
    public function offsetSet($offset, $value)
190
    {
191 3
        if (null === $offset)
192
        {
193 1
            throw new Exceptions\Invalid('Slice does not support NULL');
194
        }
195
196 2
        Arr::set($this->data, $offset, $value);
197 2
    }
198
199
    /**
200
     * @inheritdoc
201
     */
202 1
    public function offsetUnset($offset)
203
    {
204 1
        Arr::remove($this->data, $offset);
205 1
    }
206
207
}
208