IteratorDot   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 38
eloc 74
c 7
b 0
f 0
dl 0
loc 213
rs 9.36

10 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 8 4
A __construct() 0 3 1
A delete() 0 18 6
A getArrayItems() 0 8 3
A flatten() 0 17 5
B get() 0 19 7
A clear() 0 9 3
A push() 0 10 4
A pull() 0 10 2
A toJson() 0 7 3
1
<?php
2
3
/**
4
 * Dot - PHP dot notation access to arrays
5
 *
6
 * @author  Riku Särkinen <[email protected]>
7
 * @link    https://github.com/adbario/php-dot-notation
8
 * @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
9
 */
10
11
namespace Ballybran\Core\Collections\Collection;
12
use Ballybran\Core\Collections\Collection\ValidateDot;
13
14
use ArrayIterator;
15
16
/**
17
 * Dot
18
 *
19
 * This class provides a dot notation access and helper functions for
20
 * working with arrays of data. Inspired by Laravel Collection.
21
 */
22
class IteratorDot extends ValidateDot
23
{
24
    /**
25
     * The stored items
26
     *
27
     * @var array
28
     */
29
    protected $items = [];
30
    
31
    /**
32
     *  elements
33
     *
34
     * @var array
35
     */
36
    protected $elements;
37
38
    /**
39
     * Create a new Dot instance
40
     *
41
     * @param mixed $items
42
     */
43
    public function __construct($items = [])
44
    {
45
        $this->elements = $this->getArrayItems($items);
46
    }
47
48
    /**
49
     * Set a given key / value pair or pairs
50
     * if the key doesn't exist already
51
     *
52
     * @param array|int|string $keys
53
     * @param mixed $value
54
     */
55
    public function add($keys, $value = null)
56
    {
57
        if (is_array($keys)) {
58
            foreach ($keys as $key => $value) {
59
                $this->add($key, $value);
60
            }
61
        } elseif (is_null($this->get($keys))) {
62
            $this->set($keys, $value);
63
        }
64
    }
65
66
    /**
67
     * Delete the contents of a given key or keys
68
     *
69
     * @param array|int|string|null $keys
70
     */
71
    public function clear($keys = null)
72
    {
73
        if (is_null($keys)) {
74
            $this->elements = [];
75
            return;
76
        }
77
        $keys = (array)$keys;
78
        foreach ($keys as $key) {
79
            $this->set($key, []);
80
        }
81
    }
82
83
    /**
84
     * Delete the given key or keys
85
     *
86
     * @param array|int|string $keys
87
     */
88
    public function delete($keys)
89
    {
90
        $keys = (array)$keys;
91
        foreach ($keys as $key) {
92
            if ($this->exists($this->elements, $key)) {
93
                unset($this->elements[$key]);
94
                continue;
95
            }
96
            $items = &$this->elements;
97
            $segments = explode('.', $key);
98
            $lastSegment = array_pop($segments);
99
            foreach ($segments as $segment) {
100
                if (!isset($items[$segment]) || !is_array($items[$segment])) {
101
                    continue 2;
102
                }
103
                $items = &$items[$segment];
104
            }
105
            unset($items[$lastSegment]);
106
        }
107
    }
108
109
    /**
110
     * Flatten an array with the given character as a key delimiter
111
     *
112
     * @param  string $delimiter
113
     * @param  array|null $items
114
     * @param  string $prepend
115
     * @return array
116
     */
117
    public function flatten($delimiter = '.', $items = null, $prepend = '')
118
    {
119
        $flatten = [];
120
        if (is_null($items)) {
121
            $items = $this->elements;
122
        }
123
        foreach ($items as $key => $value) {
124
            if (is_array($value) && !empty($value)) {
125
                $flatten = array_merge(
126
                    $flatten,
127
                    $this->flatten($delimiter, $value, $prepend . $key . $delimiter)
128
                );
129
            } else {
130
                $flatten[$prepend . $key] = $value;
131
            }
132
        }
133
        return $flatten;
134
    }
135
136
    /**
137
     * Return the value of a given key
138
     *
139
     * @param  int|string|null $key
140
     * @param  mixed $default
141
     * @return mixed
142
     */
143
    public function get($key = null, $default = null)
144
    {
145
        if (is_null($key)) {
146
            return $this->elements;
147
        }
148
        if ($this->exists($this->elements, $key)) {
149
            return $this->elements[$key];
150
        }
151
        if (false === strpos($key, '.')) {
152
            return $default;
153
        }
154
        $items = $this->elements;
155
        foreach (explode('.', $key) as $segment) {
156
            if (!is_array($items) || !$this->exists($items, $segment)) {
157
                return $default;
158
            }
159
            $items = &$items[$segment];
160
        }
161
        return $items;
162
    }
163
164
    /**
165
     * Return the given items as an array
166
     *
167
     * @param  mixed $items
168
     * @return array
169
     */
170
    public function getArrayItems($items)
171
    {
172
        if (is_array($items)) {
173
            return $items;
174
        } elseif ($items instanceof self) {
175
            return $items->all();
176
        }
177
        return (array)$items;
178
    }
179
180
    /**
181
     * Return the value of a given key and
182
     * delete the key
183
     *
184
     * @param  int|string|null $key
185
     * @param  mixed $default
186
     * @return mixed
187
     */
188
    public function pull($key = null, $default = null)
189
    {
190
        if (is_null($key)) {
191
            $value = $this->all();
192
            $this->clear();
193
            return $value;
194
        }
195
        $value = $this->get($key, $default);
196
        $this->delete($key);
197
        return $value;
198
    }
199
200
    /**
201
     * Push a given value to the end of the array
202
     * in a given key
203
     *
204
     * @param mixed $key
205
     * @param mixed $value
206
     */
207
    public function push($key, $value = null)
208
    {
209
        if (is_null($value)) {
210
            $this->elements[] = $key;
211
            return;
212
        }
213
        $items = $this->get($key);
214
        if (is_array($items) || is_null($items)) {
215
            $items[] = $value;
216
            $this->set($key, $items);
217
        }
218
    }
219
220
   
221
    /**
222
     * Return the value of a given key or all the values as JSON
223
     *
224
     * @param  mixed $key
225
     * @param  int $options
226
     * @return string
227
     */
228
    public function toJson($key = null, $options = 0)
229
    {
230
        if (is_string($key)) {
231
            return json_encode($this->get($key), $options);
232
        }
233
        $options = $key === null ? 0 : $key;
234
        return json_encode($this->elements, $options);
235
    }
236
        
237
}