1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Zicht\Itertools\filters; |
7
|
|
|
|
8
|
|
|
use Zicht\Itertools\conversions; |
9
|
|
|
use Zicht\Itertools; |
10
|
|
|
use Zicht\Itertools\util\Filters; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Returns a filter closure that only accepts values that are instances of $CLASS. |
14
|
|
|
* |
15
|
|
|
* For example, the following will return a list where all items |
16
|
|
|
* are instances of class Bar: |
17
|
|
|
* > $list = iterable([new Foo(), new Bar(), new Moo()]); |
18
|
|
|
* > $result = $list->filter(type(Bar::class)); |
19
|
|
|
* > // {1: Bar} |
20
|
|
|
* |
21
|
|
|
* For example, the following will return a list where all items |
22
|
|
|
* have a property or array index 'key' that is an instance |
23
|
|
|
* of class Foo: |
24
|
|
|
* > $list = iterable([['key' => new Foo()], ['key' => new Bar()], ['key' => new Moo()]]); |
25
|
|
|
* > $result = $list->filter(type(Bar::class, 'key')); |
26
|
|
|
* > // {1: ['key' => Bar]} |
27
|
|
|
* |
28
|
|
|
* @param string $class |
29
|
|
|
* @param null|string|\Closure $strategy |
30
|
|
|
* @return \Closure |
31
|
|
|
* @deprecated Use \Zicht\Itertools\util\Filters::type($class, $strategy), will be removed in version 3.0 |
32
|
|
|
*/ |
33
|
|
|
function type($class, $strategy = null) |
34
|
|
|
{ |
35
|
|
|
return Filters::type($class, $strategy); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns a filter closure that only accepts values that are in $HAYSTACK. |
40
|
|
|
* |
41
|
|
|
* For example, the following will return a list where all items |
42
|
|
|
* are either 'b' or 'c': |
43
|
|
|
* > $list = iterable(['a', 'b', 'c', 'd', 'e']); |
44
|
|
|
* > $result = $list->filter(in(['b', 'c'])); |
45
|
|
|
* > // {1: 'b', 2: 'c'} |
46
|
|
|
* |
47
|
|
|
* For example, the following will return a list where all items |
48
|
|
|
* have a property or array index 'key' that is either 'b' or 'c': |
49
|
|
|
* > $list = iterable([['key' => 'a'], ['key' => 'b'], ['key' => 'c'], ['key' => 'd'], ['key' => 'e']]); |
50
|
|
|
* > $result = $list->filter(in(['b', 'c'], 'key')); |
51
|
|
|
* > // {1: ['key' => 'b'], 2: ['key' => 'c']} |
52
|
|
|
* |
53
|
|
|
* @param null|array|string|\Iterator $haystack |
54
|
|
|
* @param null|string|\Closure $strategy |
55
|
|
|
* @param boolean $strict |
56
|
|
|
* @return \Closure |
57
|
|
|
* @deprecated Use \Zicht\Itertools\util\Filters::type($heystack, $strategy, $strict), will be removed in version 3.0 |
58
|
|
|
*/ |
59
|
|
|
function in($haystack, $strategy = null, $strict = false) |
60
|
|
|
{ |
61
|
|
|
return Filters::in($haystack, $strategy, $strict); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns a filter closure that only accepts values that are not in $HAYSTACK. |
66
|
|
|
* |
67
|
|
|
* @param array|string|\Iterator $haystack |
68
|
|
|
* @param null|string|\Closure $strategy |
69
|
|
|
* @param boolean $strict |
70
|
|
|
* @return \Closure |
71
|
|
|
* @deprecated Instead use not(in(...)) |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
function not_in($haystack, $strategy = null, $strict = false) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
if (!is_bool($strict)) { |
76
|
|
|
throw new \InvalidArgumentException('$STRICT must be a boolean'); |
77
|
|
|
} |
78
|
|
|
if (!is_array($haystack)) { |
79
|
|
|
$haystack = Itertools\iterable($haystack)->values(); |
80
|
|
|
} |
81
|
|
|
$strategy = conversions\mixed_to_value_getter($strategy); |
|
|
|
|
82
|
|
|
return function ($value, $key = null) use ($haystack, $strategy, $strict) { |
83
|
|
|
return !in_array($strategy($value, $key), $haystack, $strict); |
84
|
|
|
}; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns a filter closure that only accepts values that are equal to $EXPECTED |
89
|
|
|
* |
90
|
|
|
* For example, the following will return a list where all items |
91
|
|
|
* equal 'bar': |
92
|
|
|
* > $list = iterable(['foo', 'bar']); |
93
|
|
|
* > $result = $list->filter(equals('bar')); |
94
|
|
|
* > // {1: 'bar'} |
95
|
|
|
* |
96
|
|
|
* For example, the following will return a list where all items |
97
|
|
|
* have a property or array index 'foo' that equals 'bar': |
98
|
|
|
* > $list = iterable([['key' => 'foo'], ['key' => 'bar']]); |
99
|
|
|
* > $result = $list->filter(equals('bar', 'key')); |
100
|
|
|
* > // {1: ['key' => 'bar']} |
101
|
|
|
* |
102
|
|
|
* @param mixed $expected |
103
|
|
|
* @param null|string|\Closure $strategy |
104
|
|
|
* @param boolean $strict |
105
|
|
|
* @return \Closure |
106
|
|
|
* @deprecated Use \Zicht\Itertools\util\Filters::equals($heystack, $strategy, $strict), will be removed in version 3.0 |
107
|
|
|
*/ |
108
|
|
|
function equals($expected, $strategy = null, $strict = false) |
109
|
|
|
{ |
110
|
|
|
return Filters::equals($expected, $strategy, $strict); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns a filter closure that only accepts values that are after $EXPECTED |
115
|
|
|
* |
116
|
|
|
* For example, the following will return a list where only |
117
|
|
|
* returns entries that are after 2020-04-01: |
118
|
|
|
* > $list = iterable([new \DateTime('2020-01-01'), new \DateTime('2020-03-01'), new \DateTime('2020-05-01')]) |
119
|
|
|
* > $result = $list->filer(after(new \DateTimeImmutable('2020-04-01'))); |
120
|
|
|
* > // [new \DateTime('2020-05-01')] |
121
|
|
|
* |
122
|
|
|
* @param \DateTimeInterface|int|float $expected |
123
|
|
|
* @param null|string|\Closure $strategy |
124
|
|
|
* @param bool $orEqual |
125
|
|
|
* @return \Closure |
126
|
|
|
* @deprecated Use \Zicht\Itertools\util\Filters::after($expected, $strategy, $orEqual), will be removed in version 3.0 |
127
|
|
|
*/ |
128
|
|
|
function after($expected, $strategy = null, $orEqual = false) |
129
|
|
|
{ |
130
|
|
|
return Filters::after($expected, $strategy, $orEqual); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns a filter closure that only accepts values that are before $EXPECTED |
135
|
|
|
* |
136
|
|
|
* For example, the following will return a list where only |
137
|
|
|
* returns entries that are before 2020-04-01: |
138
|
|
|
* > $list = iterable([new \DateTime('2020-01-01'), new \DateTime('2020-03-01'), new \DateTime('2020-05-01')]) |
139
|
|
|
* > $result = $list->filer(before(new \DateTimeImmutable('2020-04-01'))); |
140
|
|
|
* > // [new \DateTime('2020-01-01'), new \DateTime('2020-03-01')] |
141
|
|
|
* |
142
|
|
|
* @param \DateTimeInterface|int|float $expected |
143
|
|
|
* @param null|string|\Closure $strategy |
144
|
|
|
* @param bool $orEqual |
145
|
|
|
* @return \Closure |
146
|
|
|
* @deprecated Use \Zicht\Itertools\util\Filters::before($expected, $strategy, $orEqual), will be removed in version 3.0 |
147
|
|
|
*/ |
148
|
|
|
function before($expected, $strategy = null, $orEqual = false) |
149
|
|
|
{ |
150
|
|
|
return Filters::before($expected, $strategy, $orEqual); |
151
|
|
|
} |
152
|
|
|
/** |
153
|
|
|
* Returns a filter closure that inverts the value |
154
|
|
|
* |
155
|
|
|
* For example, the following will return a list where none |
156
|
|
|
* of the items equal 'bar' |
157
|
|
|
* > $list = iterable(['foo', 'bar']); |
158
|
|
|
* > $result = $list->filter(not(equals('bar'))); |
159
|
|
|
* > // {0: 'foo'} |
160
|
|
|
* |
161
|
|
|
* @param null|string|\Closure $strategy |
162
|
|
|
* @return \Closure |
163
|
|
|
* @deprecated Use \Zicht\Itertools\util\Filters::not($strategy), will be removed in version 3.0 |
164
|
|
|
*/ |
165
|
|
|
function not($strategy = null) |
166
|
|
|
{ |
167
|
|
|
return Filters::not($strategy); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns a filter closure that only accepts values that match the given regular expression. |
172
|
|
|
* |
173
|
|
|
* For example, the following will return a list where all items |
174
|
|
|
* match 'bar': |
175
|
|
|
* > $list = iterable(['-= foo =-', '-= bar =-']); |
176
|
|
|
* > $result = $list->filter(match('/bar/i')); |
177
|
|
|
* > // {1: '-= bar =-'} |
178
|
|
|
* |
179
|
|
|
* @param string $pattern |
180
|
|
|
* @param null|string|\Closure $strategy |
181
|
|
|
* @return \Closure |
182
|
|
|
* @deprecated Use \Zicht\Itertools\util\Filters::match($pattern, $strategy), will be removed in version 3.0 |
183
|
|
|
*/ |
184
|
|
|
function match($pattern, $strategy = null) |
185
|
|
|
{ |
186
|
|
|
return Filters::match($pattern, $strategy); |
187
|
|
|
} |
188
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.