Skip to content
Permalink
Browse files

Adjust behaviour of (has/remove)NestedKey to treat non-existing key i…

…n array of keys.
  • Loading branch information...
DarioSwain committed Sep 28, 2019
1 parent 07ade91 commit 97ffc70dff9b6ac472a4be09541e3932cc3534c3
Showing with 66 additions and 2 deletions.
  1. +5 −1 src/Ouzo/Goodies/Utilities/Arrays.php
  2. +61 −1 test/src/Ouzo/Goodies/Utilities/ArraysTest.php
@@ -890,6 +890,10 @@ public static function removeNestedKey(array &$array, array $keys, $removeEmptyP
if (count($keys) == 0) {
unset($array[$key]);
} elseif (isset($array[$key])) {
if (!is_array($array[$key])) {
return;
}
self::removeNestedKey($array[$key], $keys, $removeEmptyParents);
if ($removeEmptyParents && empty($array[$key])) {
unset($array[$key]);
@@ -928,7 +932,7 @@ public static function removeNestedKey(array &$array, array $keys, $removeEmptyP
public static function hasNestedKey(array $array, array $keys, $flags = null)
{
foreach ($keys as $key) {
if (!array_key_exists($key, $array) || (!($flags & self::TREAT_NULL_AS_VALUE) && !isset($array[$key]))) {
if (!is_array($array) || !array_key_exists($key, $array) || (!($flags & self::TREAT_NULL_AS_VALUE) && !isset($array[$key]))) {
return false;
}
$array = self::getValue($array, $key);
@@ -760,7 +760,7 @@ public function getNestedValueShouldReturnNullWhenKeyNotFound()
/**
* @test
*/
public function getNestedValueShouldReturnNullWhenZeroKeyNotFound()
public function getNestedValueShouldReturnNullWhenZeroStringKeyNotFound()
{
//given
$array = ['1' => ['2' => ['3' => 'value']]];
@@ -772,6 +772,36 @@ public function getNestedValueShouldReturnNullWhenZeroKeyNotFound()
$this->assertNull($value);
}
/**
* @test
*/
public function getNestedValueShouldReturnNullWhenZeroIntKeyNotFound()
{
//given
$array = ['1' => ['2' => ['3' => 'value']]];
//when
$value = Arrays::getNestedValue($array, ['1', '2', '3', 0]);
//then
$this->assertNull($value);
}
/**
* @test
*/
public function getNestedValueShouldReturnNullWhenMultipleStringZeroKeysNotFound()
{
//given
$array = ['1' => ['2' => ['3' => 'value']]];
//when
$value = Arrays::getNestedValue($array, ['1', '2', '3', '0', '0', '0']);
//then
$this->assertNull($value);
}
/**
* @test
*/
@@ -877,6 +907,21 @@ public function shouldNotRemoveNestedKeyWhenKeyNotFoundAndValueIsNull()
$this->assertEquals(['1' => null], $array);
}
/**
* @test
*/
public function shouldNotChangeInputArrayWhenKeyNotFound()
{
//given
$array = ['1' => ['2' => ['3' => 'value']]];
//when
Arrays::removeNestedKey($array, ['1', '2', '3', '0']);
//then
$this->assertEquals(['1' => ['2' => ['3' => 'value']]], $array);
}
/**
* @test
*/
@@ -999,6 +1044,21 @@ public function hasNestedKeyShouldReturnFalseWhenKeyIsNullAndNullIsNotValue()
$this->assertFalse($value);
}
/**
* @test
*/
public function hasNestedKeyShouldReturnFalseWhenKeyStringZeroDoesNotExist()
{
//given
$array = ['1' => ['2' => ['3' => 'value']]];
//when
$value = Arrays::hasNestedKey($array, ['1', '2', '3', '0']);
//then
$this->assertFalse($value);
}
/**
* @test
*/

0 comments on commit 97ffc70

Please sign in to comment.
You can’t perform that action at this time.