Skip to content

Commit

Permalink
add omit keys in array utils
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Jul 17, 2016
1 parent abc240e commit ee5b8d1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
42 changes: 37 additions & 5 deletions api/core/Directus/Util/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,56 @@ public static function get($array, $key, $default = null)
}

/**
* Return a copy of the object, filtered to only have values for the whitelisted keys (or array of valid keys).
* @param array $array
* @param array $keys
* Filter an array by keys
* @param $array
* @param $keys
* @param bool $omit
* @return array
*/
public static function pick($array, $keys)
public static function filterByKey($array, $keys, $omit = false)
{
$result = [];

if (is_string($keys)) {
$keys = [$keys];
}

foreach ($array as $key => $value) {
if (in_array($key, $keys)) {
$condition = in_array($key, $keys);
if ($omit) {
$condition = !$condition;
}

if ($condition) {
$result[$key] = $value;
}
}

return $result;
}

/**
* Return a copy of the object, filtered to only have values for the whitelisted keys (or array of valid keys).
* @param array $array
* @param string|array $keys
* @return array
*/
public static function pick($array, $keys)
{
return static::filterByKey($array, $keys);
}

/**
* Return a copy of the object, filtered to omit values for the blacklisted keys (or array of valid keys).
* @param array $array
* @param string|array $keys
* @return array
*/
public static function omit($array, $keys)
{
return static::filterByKey($array, $keys, true);
}

/**
* Return whether or not a set of keys exists in an array
*
Expand Down
21 changes: 21 additions & 0 deletions tests/api/Util/ArrayUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ public function testPickItems()
$items = ['name' => 'Jim', 'age' => 79, 'sex' => 'M', 'country' => 'N/A'];
$this->assertEquals(count(ArrayUtils::pick($items, ['name', 'age'])), 2);
$this->assertEquals(count(ArrayUtils::pick($items, ['name', 'age', 'city'])), 2);
$this->assertEquals(ArrayUtils::pick($items, 'name'), ['name' => 'Jim']);

$this->assertEquals(count(ArrayUtils::filterByKey($items, ['name', 'age'])), 2);
$this->assertEquals(count(ArrayUtils::filterByKey($items, ['name', 'age', 'city'])), 2);
$this->assertEquals(ArrayUtils::filterByKey($items, 'name'), ['name' => 'Jim']);

$this->assertEquals(count(ArrayUtils::filterByKey($items, ['name', 'age'], false)), 2);
$this->assertEquals(count(ArrayUtils::filterByKey($items, ['name', 'age', 'city'], false)), 2);
$this->assertEquals(ArrayUtils::filterByKey($items, 'name'), ['name' => 'Jim'], false);
}

public function testOmitItems()
{
$items = ['name' => 'Jim', 'age' => 79, 'sex' => 'M', 'country' => 'N/A'];
$this->assertEquals(count(ArrayUtils::omit($items, ['country'])), 3);
$this->assertEquals(count(ArrayUtils::omit($items, ['country', 'city'])), 3);
$this->assertEquals(ArrayUtils::omit($items, 'name'), ['age' => 79, 'sex' => 'M', 'country' => 'N/A']);

$this->assertEquals(count(ArrayUtils::filterByKey($items, ['country'], true)), 3);
$this->assertEquals(count(ArrayUtils::filterByKey($items, ['name', 'age', 'city'], true)), 2);
$this->assertEquals(ArrayUtils::filterByKey($items, 'name', true), ['age' => 79, 'sex' => 'M', 'country' => 'N/A']);
}

public function testContainsItems()
Expand Down

0 comments on commit ee5b8d1

Please sign in to comment.