Skip to content

Commit

Permalink
[+]: added "Arrayy->containsKeys()" && "Arrayy->containsValues()" + t…
Browse files Browse the repository at this point in the history
…ests + doc
  • Loading branch information
voku committed Dec 11, 2016
1 parent 44b075f commit 095c196
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
22 changes: 20 additions & 2 deletions README.md
Expand Up @@ -404,22 +404,40 @@ $arrayy = a(['three' => 3, 'one' => 1, 'two' => 2]);
$resultArrayy = $arrayy->customSortValues($callable); // Arrayy['one' => 1, 'two' => 2, 'three' => 3]
```

##### contains(mixed $value) : boolean
##### contains(string|int|float $value) : boolean

Check if an item is in the current array.

alias: "Arrayy->containsValue()"

```php
a([1, true])->contains(true); // true
```

##### containsKey(mixed $key) : boolean
##### containsValues(array $values) : boolean

Check if all given needles are present in the array.

```php
a([1, true])->containsValues(array(1, true)); // true
```

##### containsKey(string|int|float $key) : boolean

Check if the given key/index exists in the array.

```php
a([1 => true])->containsKey(1); // true
```

##### containsKeys(array $key) : boolean

Check if all given needles are present in the array as key/index.

```php
a([1 => true])->containsKeys(array(1 => 0)); // true
```

##### containsCaseInsensitive(string $value) : boolean

Check if an (case-insensitive) string is in the current array.
Expand Down
72 changes: 66 additions & 6 deletions src/Arrayy.php
Expand Up @@ -110,6 +110,8 @@ public function __unset($key)
/**
* alias: for "Arrayy->append()"
*
* @see Arrayy::append()
*
* @param mixed $value
*
* @return self (Mutable) Return this Arrayy object, with the appended values.
Expand All @@ -136,7 +138,9 @@ public function append($value)
/**
* Count the values from the current array.
*
* INFO: only a alias for "$arrayy->size()"
* alias: for "Arrayy->size()"
*
* @see Arrayy::size()
*
* @return int
*/
Expand Down Expand Up @@ -314,7 +318,7 @@ public function clear()
/**
* Check if an item is in the current array.
*
* @param mixed $value
* @param string|int|float $value
*
* @return bool
*/
Expand Down Expand Up @@ -348,7 +352,7 @@ public function containsCaseInsensitive($value)
/**
* Check if the given key/index exists in the array.
*
* @param mixed $key Key/index to search for
* @param string|int|float $key key/index to search for
*
* @return bool Returns true if the given key/index exists in the array, false otherwise
*/
Expand All @@ -357,6 +361,44 @@ public function containsKey($key)
return $this->offsetExists($key);
}

/**
* Check if all given needles are present in the array as key/index.
*
* @param array $needles
*
* @return bool Returns true if the given keys/indexes exists in the array, false otherwise
*/
public function containsKeys(array $needles)
{
return count(array_intersect($needles, $this->keys()->getArray())) === count($needles);
}

/**
* alias: for "Arrayy->contains()"
*
* @see Arrayy::contains()
*
* @param string|int|float $value
*
* @return bool
*/
public function containsValue($value)
{
return $this->contains($value);
}

/**
* Check if all given needles are present in the array.
*
* @param array $needles
*
* @return bool Returns true if the given values exists in the array, false otherwise
*/
public function containsValues(array $needles)
{
return count(array_intersect($needles, $this->array)) === count($needles);
}

/** @noinspection ArrayTypeOfParameterByDefaultValueInspection */
/**
* Creates an Arrayy object.
Expand Down Expand Up @@ -653,7 +695,7 @@ protected function fallbackForArray(&$array)

if ($array instanceof ArrayAccess) {
/** @noinspection ReferenceMismatchInspection */
return self::createFromObject($array);
return self::createFromObject($array)->getArray();
}

if (is_object($array) && method_exists($array, '__toArray')) {
Expand Down Expand Up @@ -976,6 +1018,8 @@ protected function getDirection($direction)
/**
* alias: for "Arrayy->keys()"
*
* @see Arrayy::keys()
*
* @return Arrayy (Immutable)
*/
public function getKeys()
Expand All @@ -984,7 +1028,9 @@ public function getKeys()
}

/**
* alias: for "Arrayy->random()"
* alias: for "Arrayy->randomImmutable()"
*
* @see Arrayy::randomImmutable()
*
* @return Arrayy (Immutable)
*/
Expand All @@ -996,6 +1042,8 @@ public function getRandom()
/**
* alias: for "Arrayy->randomKey()"
*
* @see Arrayy::randomKey()
*
* @return mixed get a key/index or null if there wasn't a key/index
*/
public function getRandomKey()
Expand All @@ -1006,6 +1054,8 @@ public function getRandomKey()
/**
* alias: for "Arrayy->randomKeys()"
*
* @see Arrayy::randomKeys()
*
* @param int $number
*
* @return Arrayy (Immutable)
Expand All @@ -1018,6 +1068,8 @@ public function getRandomKeys($number)
/**
* alias: for "Arrayy->randomValue()"
*
* @see Arrayy::randomValue()
*
* @return mixed get a random value or null if there wasn't a value
*/
public function getRandomValue()
Expand All @@ -1028,6 +1080,8 @@ public function getRandomValue()
/**
* alias: for "Arrayy->randomValues()"
*
* @see Arrayy::randomValues()
*
* @param int $number
*
* @return Arrayy (Immutable)
Expand Down Expand Up @@ -1125,6 +1179,8 @@ public function indexBy($key)
/**
* alias: for "Arrayy->searchIndex()"
*
* @see Arrayy::searchIndex()
*
* @param mixed $value Value to search for
*
* @return mixed
Expand Down Expand Up @@ -1417,7 +1473,9 @@ public function lastsMutable($number = null)
/**
* Count the values from the current array.
*
* INFO: only a alias for "$arrayy->size()"
* alias: for "Arrayy->size()"
*
* @see Arrayy::size()
*
* @return int
*/
Expand Down Expand Up @@ -2409,6 +2467,8 @@ public function swap($swapA, $swapB)

/**
* alias: for "Arrayy->getArray()"
*
* @see Arrayy::getArray()
*/
public function toArray()
{
Expand Down
19 changes: 19 additions & 0 deletions tests/ArrayyTest.php
Expand Up @@ -4040,4 +4040,23 @@ public function uniqueProvider()
),
);
}

public function testContainsValues()
{
$this->assertTrue(A::create(array('a', 'b', 'c'))->containsValues(array('a', 'b')));
$this->assertFalse(A::create(array('a', 'b', 'd'))->containsValues(array('a', 'b', 'c')));
$this->assertTrue(A::create(array())->containsValues(array()));
$this->assertTrue(A::create(array('a', 'b', 'c'))->containsValues(array()));
$this->assertFalse(A::create(array())->containsValues(array('a', 'b', 'c')));
}

public function testContainsKeys()
{
$this->assertTrue(A::create(array('a' => 0, 'b' => 1, 'c' => 2))->containsKeys(array('a', 'b')));
$this->assertFalse(A::create(array('a' => 0, 'b' => 1, 'd' => 2))->containsKeys(array('a', 'b', 'c')));
$this->assertTrue(A::create(array())->containsKeys(array()));
$this->assertTrue(A::create(array('a' => 0, 'b' => 1, 'c' => 2))->containsKeys(array()));
$this->assertFalse(A::create(array())->containsKeys(array('a', 'b', 'c')));
}

}

0 comments on commit 095c196

Please sign in to comment.