Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ElementsList
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 filter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 map
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 all
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIterator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Epic64\Elem;
6
7use ArrayIterator;
8use IteratorAggregate;
9use Traversable;
10
11/**
12 * A simple collection class for fluent array transformations.
13 * If you already have a collection library, please use that one instead.
14 *
15 * @template T
16 * @implements IteratorAggregate<int, T>
17 */
18class ElementsList implements IteratorAggregate
19{
20    /**
21     * @param array<T> $items
22     */
23    public function __construct(
24        private array $items
25    ) {}
26
27    /**
28     * Filter items using a callback.
29     *
30     * @param callable(T): bool $callback
31     * @return self<T>
32     */
33    public function filter(callable $callback): self
34    {
35        return new self(array_values(array_filter($this->items, $callback)));
36    }
37
38    /**
39     * Map items using a callback.
40     *
41     * @template U
42     * @param callable(T): U $callback
43     * @return self<U>
44     */
45    public function map(callable $callback): self
46    {
47        return new self(array_map($callback, $this->items));
48    }
49
50    /**
51     * Get the underlying array.
52     *
53     * @return array<T>
54     */
55    public function all(): array
56    {
57        return $this->items;
58    }
59
60    /**
61     * Get the underlying array (alias for all()).
62     *
63     * @return array<T>
64     */
65    public function toArray(): array
66    {
67        return $this->items;
68    }
69
70    /**
71     * @return Traversable<int, T>
72     */
73    public function getIterator(): Traversable
74    {
75        return new ArrayIterator($this->items);
76    }
77}