|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BenTools\IterableFunctions; |
|
6
|
|
|
|
|
7
|
|
|
use CallbackFilterIterator; |
|
8
|
|
|
use IteratorAggregate; |
|
9
|
|
|
use IteratorIterator; |
|
10
|
|
|
use Traversable; |
|
11
|
|
|
|
|
12
|
|
|
use function array_filter; |
|
13
|
|
|
use function array_map; |
|
14
|
|
|
use function iterator_to_array; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @internal |
|
18
|
|
|
* |
|
19
|
|
|
* @template TKey |
|
20
|
|
|
* @template TValue |
|
21
|
|
|
* |
|
22
|
|
|
* @implements IteratorAggregate<TKey, TValue> |
|
23
|
|
|
*/ |
|
24
|
|
|
final class IterableObject implements IteratorAggregate |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var iterable<TKey, TValue> */ |
|
27
|
|
|
private $iterable; |
|
28
|
|
|
|
|
29
|
|
|
/** @param iterable<TKey, TValue> $iterable */ |
|
30
|
|
|
public function __construct(iterable $iterable) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->iterable = $iterable; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param (callable(TValue):bool)|null $filter |
|
|
|
|
|
|
37
|
|
|
* |
|
38
|
|
|
* @return self<TKey, TValue> |
|
39
|
|
|
*/ |
|
40
|
|
|
public function filter(?callable $filter = null): self |
|
41
|
|
|
{ |
|
42
|
|
|
if ($this->iterable instanceof Traversable) { |
|
43
|
|
|
$filter ??= |
|
44
|
|
|
/** @param mixed $value */ |
|
45
|
|
|
static function ($value): bool { |
|
46
|
|
|
return (bool) $value; |
|
47
|
|
|
}; |
|
48
|
|
|
|
|
49
|
|
|
return new self(new CallbackFilterIterator(new IteratorIterator($this->iterable), $filter)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$filtered = $filter === null ? array_filter($this->iterable) : array_filter($this->iterable, $filter); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
return new self($filtered); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param callable(TValue):TResult $mapper |
|
59
|
|
|
* |
|
60
|
|
|
* @return self<TKey, TResult> |
|
61
|
|
|
* |
|
62
|
|
|
* @template TResult |
|
63
|
|
|
*/ |
|
64
|
|
|
public function map(callable $mapper): self |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->iterable instanceof Traversable) { |
|
67
|
|
|
return new self(new MappedTraversable($this->iterable, $mapper)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return new self(array_map($mapper, $this->iterable)); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return self<int, TValue> |
|
75
|
|
|
*/ |
|
76
|
|
|
public function values(): self |
|
77
|
|
|
{ |
|
78
|
|
|
return new self(new WithoutKeysTraversable($this->iterable)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** @return Traversable<TKey, TValue> */ |
|
82
|
|
|
public function getIterator(): Traversable |
|
83
|
|
|
{ |
|
84
|
|
|
yield from $this->iterable; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** @return array<array-key, TValue> */ |
|
|
|
|
|
|
88
|
|
|
public function asArray(): array |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->iterable instanceof Traversable ? iterator_to_array($this->iterable) : $this->iterable; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|