• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

varhall / utilino / 10449764476

18 Aug 2024 06:16AM UTC coverage: 89.977% (-0.3%) from 90.233%
10449764476

push

github

feropeterko
xml element get collection

1 of 1 new or added line in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

386 of 429 relevant lines covered (89.98%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.29
/src/Collections/ArrayCollection.php
1
<?php
2

3
namespace Varhall\Utilino\Collections;
4

5
use Traversable;
6
use Varhall\Utilino\ISerializable;
7

8
class ArrayCollection implements ICollection, \IteratorAggregate
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $data = [];
14

15
    protected $_searchFunc = null;
16

17
    /// Creation methods
18

19
    public function __construct(...$values)
1✔
20
    {
21
        if (empty($values))
1✔
22
            $values = [[]];
1✔
23

24
        $this->data = call_user_func_array('array_merge', array_map(function($item) {
1✔
25
            if ($item instanceof ICollection)
1✔
26
                return $item->asArray();
1✔
27

28
            if ($item instanceof ISerializable)
1✔
29
                return $item->toArray();
30

31
            return (array)$item;
1✔
32
        }, $values));
1✔
33
    }
1✔
34

35
    public static function create(...$values)
1✔
36
    {
37
        return new static(...$values);
1✔
38
    }
39

40
    public static function range($first, $last, $step = 1)
1✔
41
    {
42
        $array = [];
1✔
43

44
        for ($i = $first; $i <= $last; $i += $step) {
1✔
45
            $array[] = $i;
1✔
46
        }
47

48
        return new static($array);
1✔
49
    }
50

51

52

53
    /// Array Methods
54

55
    /**
56
     * Retrieve an external iterator
57
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
58
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
59
     * <b>Traversable</b>
60
     * @since 5.0.0
61
     */
62
    public function getIterator(): \Traversable
63
    {
64
        return new \ArrayIterator($this->data);
1✔
65
    }
66

67
    /**
68
     * Whether a offset exists
69
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
70
     * @param mixed $offset <p>
71
     * An offset to check for.
72
     * </p>
73
     * @return boolean true on success or false on failure.
74
     * </p>
75
     * <p>
76
     * The return value will be casted to boolean if non-boolean was returned.
77
     * @since 5.0.0
78
     */
79
    public function offsetExists($offset): bool
1✔
80
    {
81
        return isset($this->data[$offset]);
1✔
82
    }
83

84
    /**
85
     * Offset to retrieve
86
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
87
     * @param mixed $offset <p>
88
     * The offset to retrieve.
89
     * </p>
90
     * @return mixed Can return all value types.
91
     * @since 5.0.0
92
     */
93
    public function offsetGet($offset): mixed
1✔
94
    {
95
        return $this->data[$offset];
1✔
96
    }
97

98
    /**
99
     * Offset to set
100
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
101
     * @param mixed $offset <p>
102
     * The offset to assign the value to.
103
     * </p>
104
     * @param mixed $value <p>
105
     * The value to set.
106
     * </p>
107
     * @return void
108
     * @since 5.0.0
109
     */
110
    public function offsetSet($offset, $value): void
1✔
111
    {
112
        $this->data[$offset] = $value;
1✔
113
    }
1✔
114

115
    /**
116
     * Offset to unset
117
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
118
     * @param mixed $offset <p>
119
     * The offset to unset.
120
     * </p>
121
     * @return void
122
     * @since 5.0.0
123
     */
124
    public function offsetUnset($offset): void
1✔
125
    {
126
        unset($this->data[$offset]);
1✔
127
    }
1✔
128

129
    /**
130
     * Specify data which should be serialized to JSON
131
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
132
     * @return mixed data which can be serialized by <b>json_encode</b>,
133
     * which is a value of any type other than a resource.
134
     * @since 5.4.0
135
     */
136
    public function jsonSerialize(): mixed
137
    {
UNCOV
138
        return $this->data;
×
139
    }
140

141

142

143
    public function searchFunc(callable $func)
1✔
144
    {
145
        $this->_searchFunc = $func;
1✔
146

147
        return $this;
1✔
148
    }
149

150

151
    /// ICollection interface
152

153
    public function count(): int
154
    {
155
        return count($this->data);
1✔
156
    }
157

158
    public function limit(?int $limit, ?int $offset = null)
1✔
159
    {
160
        return new static(array_slice($this->data, $offset !== null ? $offset : 0, $limit));
1✔
161
    }
162

163
    public function each(callable $func)
1✔
164
    {
165
        $index = 0;
1✔
166

167
        foreach ($this->data as $key => $value) {
1✔
168
            if (call_user_func($func, $value, $key, $index++) === false)
1✔
169
                break;
×
170
        }
171

172
        return $this;
1✔
173
    }
174

175
    public function every(callable $func)
1✔
176
    {
177
        foreach ($this->data as $item) {
1✔
178
            if (call_user_func($func, $item) === false)
1✔
179
                return false;
1✔
180
        }
181

182
        return true;
1✔
183
    }
184
    
185
    public function any(callable $func)
1✔
186
    {
187
        foreach ($this->data as $item) {
1✔
188
            if (call_user_func($func, $item) === true)
1✔
189
                return true;
1✔
190
        }
191
        
192
        return false;
1✔
193
    }
194

195
    public function filter(callable $func)
1✔
196
    {
197
        return new static(array_filter($this->data, function($item, $key) use ($func) {
1✔
198
            return call_user_func_array($func, [$item, $key]);
1✔
199
        }, ARRAY_FILTER_USE_BOTH));
1✔
200
    }
201

202
    public function filterKeys($keys)
203
    {
204
        return new static(array_filter($this->data, function($key) use ($keys) {
1✔
205
            if (is_array($keys))
1✔
206
                return in_array($key, $keys);
1✔
207

208
            else if (is_callable($keys))
209
                return call_user_func($keys, $key);
210

211
            return true;
212
        }, ARRAY_FILTER_USE_KEY));
1✔
213
    }
214

215
    public function first(callable $func = null)
1✔
216
    {
217
        $collection = !!$func ? $this->filter($func) : $this;
1✔
218

219
        if (!$collection->count())
1✔
220
            return null;
1✔
221

222
        return $collection[0];
1✔
223
    }
224

225
    public function flatten()
226
    {
227
        return $this->reduce(function($carry, $item) {
1✔
228
            return $carry->merge($item);
1✔
229
        }, ArrayCollection::create());
1✔
230
    }
231

232
    public function chunk(int $size, callable $func)
1✔
233
    {
234
        $chunks = array_chunk($this->data, $size);
1✔
235

236
        foreach ($chunks as $index => $chunk) {
1✔
237
            call_user_func($func, new static($chunk), $index);
1✔
238
        }
239

240
        return $this;
1✔
241
    }
242

243
    public function isEmpty()
244
    {
245
        return $this->count() === 0;
1✔
246
    }
247

248
    public function keys()
249
    {
250
        return new static(array_keys($this->data));
1✔
251
    }
252

253
    public function last(callable $func = null)
1✔
254
    {
255
        $collection = !!$func ? $this->filter($func) : $this;
1✔
256

257
        if (!$collection->count())
1✔
258
            return null;
1✔
259

260
        return $collection[$collection->count() - 1];
1✔
261
    }
262

263
    public function map(callable $func)
1✔
264
    {
265
        return new static(array_map($func, $this->data));
1✔
266
    }
267

268
    public function merge($collection)
269
    {
270
        $array = ($collection instanceof ICollection) ? $collection->asArray() : $collection;
1✔
271

272
        return new static(array_merge($this->asArray(), $array));
1✔
273
    }
274

275
    public function pad($size, $value)
276
    {
277
        return new static(array_pad($this->data, $size, $value));
1✔
278
    }
279

280
    public function pipe(callable $func)
1✔
281
    {
282
        return call_user_func_array($func, [ $this ]);
1✔
283
    }
284

285
    public function pop()
286
    {
287
        return array_pop($this->data);
1✔
288
    }
289

290
    public function prepend($value)
291
    {
292
        array_unshift($this->data, $value);
1✔
293

294
        return $this;
1✔
295
    }
296

297
    public function push($value)
298
    {
299
        $this->data[] = $value;
1✔
300

301
        return $this;
1✔
302
    }
303

304
    public function reduce(callable $func, $initial = null)
1✔
305
    {
306
        return array_reduce($this->data, $func, $initial);
1✔
307
    }
308

309
    public function reverse()
310
    {
311
        return new static(array_reverse($this->asArray()));
1✔
312
    }
313

314
    public function search($value, callable $func = null)
1✔
315
    {
316
        if ($func === null && $this->_searchFunc !== null)
1✔
317
            $func = $this->_searchFunc;
1✔
318

319
        return new static($this->filter(function($item) use($func, $value) {
1✔
320
            return !!$func ? call_user_func($func, $item, $value) : true;
1✔
321
        }));
1✔
322
    }
323

324
    public function shift()
325
    {
326
        return array_shift($this->data);
1✔
327
    }
328

329
    public function sort(callable $func)
1✔
330
    {
331
        usort($this->data, $func);
1✔
332
        return $this;
1✔
333
    }
334

335
    public function values()
336
    {
337
        return new static(array_values($this->data));
1✔
338
    }
339

340
    public function asArray()
341
    {
342
        return $this->data;
1✔
343
    }
344

345
    public function toArray()
346
    {
347
        return $this->map(function($item) {
1✔
348
            if (is_array($item) || is_scalar($item) || is_null($item))
1✔
349
                return $item;
1✔
350

351
            else if ($item instanceof \Varhall\Utilino\ISerializable)
1✔
352
                return $item->toArray();
1✔
353

354
            else if ($item instanceof \Nette\Database\Table\ActiveRow)
1✔
355
                return $item->toArray();
1✔
356

357
            else if (is_object($item))
1✔
358
                return json_decode(json_encode($item), true);
1✔
359

360
            return null;
361
        })->data;
1✔
362
    }
363
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc