File tree Expand file tree Collapse file tree 5 files changed +70
-0
lines changed Expand file tree Collapse file tree 5 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -203,6 +203,7 @@ $chain->reduce(function ($current, $value) {
203
203
- ` ->countValues() `
204
204
- ` ->every(callable) `
205
205
- ` ->first() `
206
+ - ` ->includes(mixed[, array]) `
206
207
- ` ->join([$glue]) `
207
208
- ` ->last() `
208
209
- ` ->reduce() `
Original file line number Diff line number Diff line change 13
13
use Cocur \Chain \Link \Find ;
14
14
use Cocur \Chain \Link \FlatMap ;
15
15
use Cocur \Chain \Link \Flip ;
16
+ use Cocur \Chain \Link \Includes ;
16
17
use Cocur \Chain \Link \Intersect ;
17
18
use Cocur \Chain \Link \IntersectAssoc ;
18
19
use Cocur \Chain \Link \IntersectKey ;
@@ -63,6 +64,7 @@ class Chain extends AbstractChain implements Countable
63
64
use First;
64
65
use FlatMap;
65
66
use Flip;
67
+ use Includes;
66
68
use Intersect;
67
69
use IntersectAssoc;
68
70
use IntersectKey;
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Cocur \Chain \Link ;
4
+
5
+ /**
6
+ * Includes.
7
+ *
8
+ * @author Nicolas Reynis
9
+ */
10
+ trait Includes
11
+ {
12
+ /**
13
+ * Returns `true` if the given `needle` is in the array or `false` otherwise.
14
+ *
15
+ * @param $needle
16
+ * @param array $options options, including `strict` to also check the type
17
+ *
18
+ * @return bool
19
+ */
20
+ public function includes ($ needle , array $ options = []): bool
21
+ {
22
+ if (!empty ($ options ['strict ' ])) {
23
+ return in_array ($ needle , $ this ->array , $ options ['strict ' ]);
24
+ } else {
25
+ return in_array ($ needle , $ this ->array );
26
+ }
27
+ }
28
+ }
Original file line number Diff line number Diff line change @@ -87,6 +87,7 @@ public function chainHasTraits(): void
87
87
$ this ->assertTrue (method_exists ($ c , 'first ' ));
88
88
$ this ->assertTrue (method_exists ($ c , 'flatMap ' ));
89
89
$ this ->assertTrue (method_exists ($ c , 'flip ' ));
90
+ $ this ->assertTrue (method_exists ($ c , 'includes ' ));
90
91
$ this ->assertTrue (method_exists ($ c , 'intersect ' ));
91
92
$ this ->assertTrue (method_exists ($ c , 'intersectAssoc ' ));
92
93
$ this ->assertTrue (method_exists ($ c , 'intersectKey ' ));
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Cocur \Chain \Link ;
4
+
5
+ /**
6
+ * IncludesTest.
7
+ *
8
+ * @author Nicolas Reynis
9
+ * @group unit
10
+ */
11
+ class IncludesTest extends \PHPUnit \Framework \TestCase
12
+ {
13
+ /**
14
+ * @test
15
+ * @covers \Cocur\Chain\Link\Includes::includes()
16
+ */
17
+ public function includesFindElement (): void
18
+ {
19
+ /** @var Includes $mock */
20
+ $ mock = $ this ->getMockForTrait (Includes::class);
21
+ $ mock ->array = ['foobar ' , 'bar ' ];
22
+ $ this ->assertTrue ($ mock ->includes ('bar ' ));
23
+ $ this ->assertFalse ($ mock ->includes ('baz ' ));
24
+ }
25
+
26
+ /**
27
+ * @test
28
+ * @covers \Cocur\Chain\Link\Includes::includes()
29
+ */
30
+ public function includesFindElementStrictly (): void
31
+ {
32
+ /** @var Includes $mock */
33
+ $ mock = $ this ->getMockForTrait (Includes::class);
34
+ $ mock ->array = ['42 ' , '43 ' ];
35
+ $ this ->assertTrue ($ mock ->includes ('42 ' , ['strict ' => true ]));
36
+ $ this ->assertFalse ($ mock ->includes (42 , ['strict ' => true ]));
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments