Skip to content

Commit f43e257

Browse files
author
Maciej A. Czyzewski
authored
Merge pull request #22 from nullpunkt/hasKeys
new method: __::hasKeys
2 parents 8067aa5 + 8134651 commit f43e257

4 files changed

Lines changed: 71 additions & 4 deletions

File tree

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ Put the require statement in your `composer.json` file and run `composer install
5555
```json
5656
{
5757
"require": {
58-
...
5958
"maciejczyzewski/bottomline": "*"
60-
...
6159
}
6260
}
6361
```
@@ -83,7 +81,7 @@ __::append([1, 2, 3], 4);
8381
```
8482

8583
##### [__::chunk](src/__/arrays/chunk.php)
86-
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.
84+
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.
8785
```php
8886
__::chunk([1, 2, 3, 4, 5], 3);
8987
// >> [[1, 2, 3], [4, 5]]
@@ -172,11 +170,19 @@ __::first([1, 2, 3, 4, 5], 2);
172170
```
173171

174172
##### [__::get](src/__/collections/get.php)
173+
Get item of an array by index, aceepting nested index
175174
```php
176175
__::get(['foo' => ['bar' => 'ter']], 'foo.bar');
177176
// >> 'ter'
178177
```
179178

179+
##### [__::hasKeys](src/__/collections/hasKeys.php)
180+
Returns if $input contains all requested $keys. If $strict is true it also checks if $input exclusively contains the given $keys.
181+
```php
182+
__::hasKeys(['foo' => 'bar', 'foz' => 'baz'], ['foo', 'foz']);
183+
// >> true
184+
```
185+
180186
##### [__::last](src/__/collections/last.php)
181187
Gets the last element of an array. Passing n returns the last n elements.
182188
```php
@@ -326,3 +332,4 @@ See LICENSE file in this repository.
326332

327333
* Brandtley McMinn ([@bmcminn](https://github.com/bmcminn))
328334
* Ivan Ternovtsiy ([@diaborn19](https://github.com/diaborn19))
335+
* Tobias Seipke ([@nullpunkt](https://github.com/nullpunkt))

src/__/collections/hasKeys.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace collections;
4+
5+
/**
6+
* Returns if $input contains all requested $keys. If $strict is true it also checks if $input exclusively contains the given $keys.
7+
*
8+
** __::hasKeys(['foo' => 'bar', 'foz' => 'baz'], ['foo', 'foz']);
9+
** // → true
10+
*
11+
* @param array $collection of key values pairs
12+
* @param array $keys collection of keys to look for
13+
* @param boolean $strict to exclusively check
14+
*
15+
* @return boolean
16+
*
17+
*/
18+
function hasKeys(array $collection = [], array $keys = [], $strict = false)
19+
{
20+
$keysExist = (count(array_intersect($keys, array_keys($collection)))==count($keys));
21+
22+
return (!$strict && $keysExist) || ($strict && $keysExist && count($keys) === count($collection));
23+
}

src/__/load.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
* @method static array ease(array $input, string $glue = '.')
4242
* @method static array filter(array|\Traversable $input, \Closure $func = null) Returns the values in the collection that pass the truth test.
4343
* @method static array|mixed first(array $input, int $count = null) Gets the first element of an array. Passing n returns the first n elements.
44-
* @method static array get(array|\Traversable $input, string $path, \Closure|mixed $default = null)
44+
* @method static array|mixed get(array|\Traversable $input, string $path, \Closure|mixed $default = null)
45+
* @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.
4546
* @method static array|mixed last(array $input, int $count = null) Gets the last element of an array. Passing n returns the last n elements.
4647
* @method static array map(array $input, \Closure $func = null) Returns an array of values by mapping each in collection through the iterator.
4748
* @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.

tests/collections.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ public function testGet()
6363
$this->assertEquals('ter', $x);
6464
}
6565

66+
public function testHasKeys()
67+
{
68+
// Arrange
69+
$a = ['foo' => 'bar'];
70+
71+
// Act
72+
$x = __::hasKeys($a, ['foo', 'foz'], false);
73+
$y = __::hasKeys($a, ['foo', 'foz'], true);
74+
75+
// Assert
76+
$this->assertFalse($x);
77+
$this->assertFalse($y);
78+
79+
//Rearrange
80+
$a['foz'] = 'baz';
81+
82+
//React
83+
$x = __::hasKeys($a, ['foo', 'foz'], false);
84+
$y = __::hasKeys($a, ['foo', 'foz'], true);
85+
86+
// Assert
87+
$this->assertTrue($x);
88+
$this->assertTrue($y);
89+
90+
//Rearrange
91+
$a['xxx'] = 'bay';
92+
93+
//React
94+
$x = __::hasKeys($a, ['foo', 'foz'], false);
95+
$y = __::hasKeys($a, ['foo', 'foz'], true);
96+
97+
// Assert
98+
$this->assertTrue($x);
99+
$this->assertFalse($y);
100+
}
101+
66102
public function testLast()
67103
{
68104
// Arrange

0 commit comments

Comments
 (0)