Skip to content

Commit

Permalink
updated contains method
Browse files Browse the repository at this point in the history
  • Loading branch information
AVMG20 committed Jan 13, 2025
1 parent 6a9d20b commit f73b10c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
14 changes: 13 additions & 1 deletion docs/Arr.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,29 @@ $result = Arr::whereNot($array, 'role', 'admin');

### contains()

Check if an array contains a given key-value pair.
Check if an array contains a value, key-value pair, or matches a callback condition.

```php
// Array of items
$array = [
['name' => 'John', 'age' => 25],
['name' => 'Jane', 'age' => 30],
['name' => 'Bob', 'age' => 25]
];

// Check for key-value pair
$result = Arr::contains($array, 'age', 25);
// Returns: true

// Check with callback
$result = Arr::contains($array, function($value) {
return $value['age'] > 30;
});
// Returns: false

// Check for simple value
$result = Arr::contains($array, 'Bob');
// Returns: true
```

### first()
Expand Down
35 changes: 27 additions & 8 deletions src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,41 @@ public static function where(array $array, string|callable $key, mixed $operator
}

/**
* Determine if an array contains a given key-value pair.
* Determine if an array contains a given item or key-value pair,
* or if a callback returns true for any item.
*
* @param array $array
* @param string $key
* @param mixed $value
* @param mixed|callable|string $key
* @param mixed $operator
* @return bool
*/
public static function contains(array $array, string $key, mixed $value): bool
public static function contains(array $array, mixed $key, mixed $operator = null): bool
{
foreach ($array as $item) {
if (static::dataGet($item, $key) === $value) {
return true;
if (is_callable($key) && !is_string($key)) {
foreach ($array as $k => $value) {
if ($key($value, $k)) {
return true;
}
}
return false;
}

if (func_num_args() === 2) {
foreach ($array as $item) {
if (is_array($item)) {
if (in_array($key, $item, true)) {
return true;
}
} else {
if ($item === $key) {
return true;
}
}
}
return false;
}

return false;
return count(static::where($array, $key, '=', $operator)) > 0;
}

/**
Expand Down
30 changes: 28 additions & 2 deletions tests/Unit/ArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,35 @@ public function testEach()
$this->assertEquals(['John', 'Jane', 'Bob', 'Alice'], $result);
}

public function testContains()
public function testContainsWithCallback()
{
$result = Arr::contains($this->testArray, function ($value) {
return $value['age'] > 30;
});
$this->assertTrue($result);

$result = Arr::contains($this->testArray, function ($value) {
return $value['age'] > 40;
});
$this->assertFalse($result);
}

public function testContainsSimpleValue()
{
$simpleArray = ['name' => 'Desk', 'price' => 100];
$this->assertTrue(Arr::contains($simpleArray, 'Desk'));
$this->assertFalse(Arr::contains($simpleArray, 'Chair'));
}

public function testContainsKeyValuePair()
{
$this->assertTrue(Arr::contains($this->testArray, 'name', 'John'));
$this->assertFalse(Arr::contains($this->testArray, 'name', 'Non-existent'));
$this->assertFalse(Arr::contains($this->testArray, 'name', 'Mark'));
}

public function testContainsNestedValue()
{
$this->assertTrue(Arr::contains($this->testArray, 'info.active', true));
$this->assertTrue(Arr::contains($this->testArray, 'info.active', false));
}
}

0 comments on commit f73b10c

Please sign in to comment.