2
2
3
3
namespace BenTools \IterableFunctions ;
4
4
5
- use Closure ;
6
5
use EmptyIterator ;
7
- use InvalidArgumentException ;
8
6
use IteratorAggregate ;
9
7
use Traversable ;
10
8
9
+ /**
10
+ * @internal
11
+ */
11
12
final class IterableObject implements IteratorAggregate
12
13
{
13
14
/**
@@ -18,113 +19,50 @@ final class IterableObject implements IteratorAggregate
18
19
/**
19
20
* @var callable
20
21
*/
21
- private $ filter ;
22
+ private $ filterFn ;
22
23
23
24
/**
24
25
* @var callable
25
26
*/
26
- private $ map ;
27
+ private $ mapFn ;
27
28
28
- /**
29
- * IterableObject constructor.
30
- * @param iterable|array|Traversable $iterable
31
- * @param callable|null $filter
32
- * @param callable|null $map
33
- * @throws InvalidArgumentException
34
- */
35
- public function __construct ($ iterable , $ filter = null , $ map = null )
29
+ public function __construct (?iterable $ iterable = null , ?callable $ filter = null , ?callable $ map = null )
36
30
{
37
- if (null === $ iterable ) {
38
- $ iterable = new EmptyIterator ();
39
- }
40
- if (!is_iterable ($ iterable )) {
41
- throw new InvalidArgumentException (
42
- sprintf ('Expected array or Traversable, got %s ' , is_object ($ iterable ) ? get_class ($ iterable ) : gettype ($ iterable ))
43
- );
44
- }
45
-
46
- // Cannot rely on callable type-hint on PHP 5.3
47
- if (null !== $ filter && !is_callable ($ filter ) && !$ filter instanceof Closure) {
48
- throw new InvalidArgumentException (
49
- sprintf ('Expected callable, got %s ' , is_object ($ filter ) ? get_class ($ filter ) : gettype ($ filter ))
50
- );
51
- }
52
-
53
- if (null !== $ map && !is_callable ($ map ) && !$ map instanceof Closure) {
54
- throw new InvalidArgumentException (
55
- sprintf ('Expected callable, got %s ' , is_object ($ map ) ? get_class ($ map ) : gettype ($ map ))
56
- );
57
- }
58
-
59
- $ this ->iterable = $ iterable ;
60
- $ this ->filter = $ filter ;
61
- $ this ->map = $ map ;
31
+ $ this ->iterable = $ iterable ?? new EmptyIterator ();
32
+ $ this ->filterFn = $ filter ;
33
+ $ this ->mapFn = $ map ;
62
34
}
63
35
64
- /**
65
- * @param callable $filter
66
- * @return self
67
- */
68
- public function filter ($ filter )
36
+ public function filter (callable $ filter ): self
69
37
{
70
- return new self ($ this ->iterable , $ filter , $ this ->map );
38
+ return new self ($ this ->iterable , $ filter , $ this ->mapFn );
71
39
}
72
40
73
- /**
74
- * @param callable $map
75
- * @return self
76
- */
77
- public function map ($ map )
41
+ public function map (callable $ map ): self
78
42
{
79
- return new self ($ this ->iterable , $ this ->filter , $ map );
43
+ return new self ($ this ->iterable , $ this ->filterFn , $ map );
80
44
}
81
45
82
- /**
83
- * @param callable $filter
84
- * @return self
85
- * @deprecated Use IterableObject::filter instead.
86
- */
87
- public function withFilter ($ filter )
88
- {
89
- return $ this ->filter ($ filter );
90
- }
91
-
92
- /**
93
- * @param callable $map
94
- * @return self
95
- * @deprecated Use IterableObject::map instead.
96
- */
97
- public function withMap ($ map )
98
- {
99
- return $ this ->map ($ map );
100
- }
101
-
102
- /**
103
- * @inheritdoc
104
- */
105
- public function getIterator ()
46
+ public function getIterator (): Traversable
106
47
{
107
48
$ iterable = $ this ->iterable ;
108
- if (null !== $ this ->filter ) {
109
- $ iterable = iterable_filter ($ iterable , $ this ->filter );
49
+ if (null !== $ this ->filterFn ) {
50
+ $ iterable = iterable_filter ($ iterable , $ this ->filterFn );
110
51
}
111
- if (null !== $ this ->map ) {
112
- $ iterable = iterable_map ($ iterable , $ this ->map );
52
+ if (null !== $ this ->mapFn ) {
53
+ $ iterable = iterable_map ($ iterable , $ this ->mapFn );
113
54
}
114
55
return iterable_to_traversable ($ iterable );
115
56
}
116
57
117
- /**
118
- * @return array
119
- */
120
- public function asArray ()
58
+ public function asArray (): array
121
59
{
122
60
$ iterable = $ this ->iterable ;
123
- if (null !== $ this ->filter ) {
124
- $ iterable = iterable_filter ($ iterable , $ this ->filter );
61
+ if (null !== $ this ->filterFn ) {
62
+ $ iterable = iterable_filter ($ iterable , $ this ->filterFn );
125
63
}
126
- if (null !== $ this ->map ) {
127
- $ iterable = iterable_map ($ iterable , $ this ->map );
64
+ if (null !== $ this ->mapFn ) {
65
+ $ iterable = iterable_map ($ iterable , $ this ->mapFn );
128
66
}
129
67
return iterable_to_array ($ iterable );
130
68
}
0 commit comments