Skip to content

Commit

Permalink
Merge pull request #22 from nullpunkt/hasKeys
Browse files Browse the repository at this point in the history
new method: __::hasKeys
  • Loading branch information
Maciej A. Czyzewski authored Jan 3, 2017
2 parents 8067aa5 + 8134651 commit f43e257
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ Put the require statement in your `composer.json` file and run `composer install
```json
{
"require": {
...
"maciejczyzewski/bottomline": "*"
...
}
}
```
Expand All @@ -83,7 +81,7 @@ __::append([1, 2, 3], 4);
```

##### [__::chunk](src/__/arrays/chunk.php)
Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
Creates an array of elements split into groups the length of size. If an array can't be split evenly, the final chunk will be the remaining elements.
```php
__::chunk([1, 2, 3, 4, 5], 3);
// >> [[1, 2, 3], [4, 5]]
Expand Down Expand Up @@ -172,11 +170,19 @@ __::first([1, 2, 3, 4, 5], 2);
```

##### [__::get](src/__/collections/get.php)
Get item of an array by index, aceepting nested index
```php
__::get(['foo' => ['bar' => 'ter']], 'foo.bar');
// >> 'ter'
```

##### [__::hasKeys](src/__/collections/hasKeys.php)
Returns if $input contains all requested $keys. If $strict is true it also checks if $input exclusively contains the given $keys.
```php
__::hasKeys(['foo' => 'bar', 'foz' => 'baz'], ['foo', 'foz']);
// >> true
```

##### [__::last](src/__/collections/last.php)
Gets the last element of an array. Passing n returns the last n elements.
```php
Expand Down Expand Up @@ -326,3 +332,4 @@ See LICENSE file in this repository.

* Brandtley McMinn ([@bmcminn](https://github.com/bmcminn))
* Ivan Ternovtsiy ([@diaborn19](https://github.com/diaborn19))
* Tobias Seipke ([@nullpunkt](https://github.com/nullpunkt))
23 changes: 23 additions & 0 deletions src/__/collections/hasKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace collections;

/**
* Returns if $input contains all requested $keys. If $strict is true it also checks if $input exclusively contains the given $keys.
*
** __::hasKeys(['foo' => 'bar', 'foz' => 'baz'], ['foo', 'foz']);
** // → true
*
* @param array $collection of key values pairs
* @param array $keys collection of keys to look for
* @param boolean $strict to exclusively check
*
* @return boolean
*
*/
function hasKeys(array $collection = [], array $keys = [], $strict = false)
{
$keysExist = (count(array_intersect($keys, array_keys($collection)))==count($keys));

return (!$strict && $keysExist) || ($strict && $keysExist && count($keys) === count($collection));
}
3 changes: 2 additions & 1 deletion src/__/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
* @method static array ease(array $input, string $glue = '.')
* @method static array filter(array|\Traversable $input, \Closure $func = null) Returns the values in the collection that pass the truth test.
* @method static array|mixed first(array $input, int $count = null) Gets the first element of an array. Passing n returns the first n elements.
* @method static array get(array|\Traversable $input, string $path, \Closure|mixed $default = null)
* @method static array|mixed get(array|\Traversable $input, string $path, \Closure|mixed $default = null)
* @method static bool hasKeys(array $input, array $keys, $strict = false) Returns if $input contains all requested $keys. If $strict is true it also checks if $input exclusively contains the given $keys.
* @method static array|mixed last(array $input, int $count = null) Gets the last element of an array. Passing n returns the last n elements.
* @method static array map(array $input, \Closure $func = null) Returns an array of values by mapping each in collection through the iterator.
* @method static array max(array|\Traversable $input) Returns the maximum value from the collection. If passed an iterator, max will return max value returned by the iterator.
Expand Down
36 changes: 36 additions & 0 deletions tests/collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,42 @@ public function testGet()
$this->assertEquals('ter', $x);
}

public function testHasKeys()
{
// Arrange
$a = ['foo' => 'bar'];

// Act
$x = __::hasKeys($a, ['foo', 'foz'], false);
$y = __::hasKeys($a, ['foo', 'foz'], true);

// Assert
$this->assertFalse($x);
$this->assertFalse($y);

//Rearrange
$a['foz'] = 'baz';

//React
$x = __::hasKeys($a, ['foo', 'foz'], false);
$y = __::hasKeys($a, ['foo', 'foz'], true);

// Assert
$this->assertTrue($x);
$this->assertTrue($y);

//Rearrange
$a['xxx'] = 'bay';

//React
$x = __::hasKeys($a, ['foo', 'foz'], false);
$y = __::hasKeys($a, ['foo', 'foz'], true);

// Assert
$this->assertTrue($x);
$this->assertFalse($y);
}

public function testLast()
{
// Arrange
Expand Down

0 comments on commit f43e257

Please sign in to comment.