diff --git a/src/System/Collection/Collection.php b/src/System/Collection/Collection.php index fb15e255..2b269755 100644 --- a/src/System/Collection/Collection.php +++ b/src/System/Collection/Collection.php @@ -507,4 +507,68 @@ public function complementAssoc($collection) array_diff_assoc($collection, $this->collection) ); } + + /** + * Filter where using operator. + * + * @param TKey $key + * @param TValue $value + * + * @return $this + */ + public function where($key, string $operator, $value): self + { + if ('=' === $operator || '==' === $operator) { + return $this->filter(fn ($TValue) => array_key_exists($key, $TValue) && $TValue[$key] == $value); + } + if ('===' === $operator) { + return $this->filter(fn ($TValue) => array_key_exists($key, $TValue) && $TValue[$key] === $value); + } + if ('!=' === $operator) { + return $this->filter(fn ($TValue) => array_key_exists($key, $TValue) && $TValue[$key] != $value); + } + if ('!==' === $operator) { + return $this->filter(fn ($TValue) => array_key_exists($key, $TValue) && $TValue[$key] !== $value); + } + if ('>' === $operator) { + return $this->filter(fn ($TValue) => array_key_exists($key, $TValue) && $TValue[$key] > $value); + } + if ('>=' === $operator) { + return $this->filter(fn ($TValue) => array_key_exists($key, $TValue) && $TValue[$key] >= $value); + } + if ('<' === $operator) { + return $this->filter(fn ($TValue) => array_key_exists($key, $TValue) && $TValue[$key] < $value); + } + if ('<=' === $operator) { + return $this->filter(fn ($TValue) => array_key_exists($key, $TValue) && $TValue[$key] <= $value); + } + + return $this->replace([]); + } + + /** + * Filter where in range. + * + * @param TKey $key + * @param TValue[] $range + * + * @return $this + */ + public function whereIn($key, $range): self + { + return $this->filter(fn ($TValue) => array_key_exists($key, $TValue) && in_array($TValue[$key], $range)); + } + + /** + * Filter where not in range. + * + * @param TKey $key + * @param TValue[] $range + * + * @return $this + */ + public function whereNotIn($key, $range): self + { + return $this->filter(fn ($TValue) => array_key_exists($key, $TValue) && false === in_array($TValue[$key], $range)); + } } diff --git a/tests/Collection/CollectionTest.php b/tests/Collection/CollectionTest.php index ed186c4d..a78123de 100644 --- a/tests/Collection/CollectionTest.php +++ b/tests/Collection/CollectionTest.php @@ -542,4 +542,111 @@ public function itCanGetComplementUsingAssoc() 'used' => 100, ], $coll->toArray()); } + + /** + * @test + */ + public function itCanGetFilteredUsingWhere() + { + $data = [ + ['user' => 'user1', 'age' => 10], + ['user' => 'user2', 'age' => 12], + ['user' => 'user3', 'age' => 10], + ['user' => 'user4', 'age' => 13], + ['user' => 'user5', 'age' => 14], + ]; + $equal = (new Collection($data))->where('age', '=', '13'); + $this->assertEquals([ + 3 => ['user' => 'user4', 'age' => 13], + ], $equal->toArray()); + + $identical = (new Collection($data))->where('age', '===', 13); + $this->assertEquals([ + 3 => ['user' => 'user4', 'age' => 13], + ], $identical->toArray()); + + $notequal = (new Collection($data))->where('age', '!=', '13'); + $this->assertEquals([ + ['user' => 'user1', 'age' => 10], + ['user' => 'user2', 'age' => 12], + ['user' => 'user3', 'age' => 10], + 4 => ['user' => 'user5', 'age' => 14], + ], $notequal->toArray()); + + $notequalidentical = (new Collection($data))->where('age', '!==', 13); + $this->assertEquals([ + ['user' => 'user1', 'age' => 10], + ['user' => 'user2', 'age' => 12], + ['user' => 'user3', 'age' => 10], + 4 => ['user' => 'user5', 'age' => 14], + ], $notequalidentical->toArray()); + + $greathat = (new Collection($data))->where('age', '>', 13); + $this->assertEquals([ + 4 => ['user' => 'user5', 'age' => 14], + ], $greathat->toArray()); + + $greathatequal = (new Collection($data))->where('age', '>=', 13); + $this->assertEquals([ + 3 => ['user' => 'user4', 'age' => 13], + 4 => ['user' => 'user5', 'age' => 14], + ], $greathatequal->toArray()); + + $lessthat = (new Collection($data))->where('age', '<', 13); + $this->assertEquals([ + ['user' => 'user1', 'age' => 10], + ['user' => 'user2', 'age' => 12], + ['user' => 'user3', 'age' => 10], + ], $lessthat->toArray()); + + $lessthatequal = (new Collection($data))->where('age', '<=', 13); + $this->assertEquals([ + ['user' => 'user1', 'age' => 10], + ['user' => 'user2', 'age' => 12], + ['user' => 'user3', 'age' => 10], + ['user' => 'user4', 'age' => 13], + ], $lessthatequal->toArray()); + } + + /** + * @test + */ + public function itCanFilterDataUsingWhereIn() + { + $data = [ + ['user' => 'user1', 'age' => 10], + ['user' => 'user2', 'age' => 12], + ['user' => 'user3', 'age' => 10], + ['user' => 'user4', 'age' => 13], + ['user' => 'user5', 'age' => 14], + ]; + + $wherein = (new Collection($data))->whereIn('age', [10, 12]); + $this->assertEquals([ + ['user' => 'user1', 'age' => 10], + ['user' => 'user2', 'age' => 12], + ['user' => 'user3', 'age' => 10], + ], $wherein->toArray()); + } + + /** + * @test + */ + public function itCanFilterDataUsingWhereNotIn() + { + $data = [ + ['user' => 'user1', 'age' => 10], + ['user' => 'user2', 'age' => 12], + ['user' => 'user3', 'age' => 10], + ['user' => 'user4', 'age' => 13], + ['user' => 'user5', 'age' => 14], + ]; + + $wherein = (new Collection($data))->whereNotIn('age', [13, 14]); + $this->assertEquals([ + ['user' => 'user1', 'age' => 10], + ['user' => 'user2', 'age' => 12], + ['user' => 'user3', 'age' => 10], + ], $wherein->toArray()); + } }