Skip to content
Permalink
Browse files

Merge pull request #278 from DarioSwain/master

Fix issue in Arrays::getNestedValue
  • Loading branch information...
bbankowski committed Oct 1, 2019
2 parents 990a08d + 97ffc70 commit 79a741a1184fde8ad095e01f65e8ce91784407a8
Showing with 84 additions and 1 deletion.
  1. +9 −1 src/Ouzo/Goodies/Utilities/Arrays.php
  2. +75 −0 test/src/Ouzo/Goodies/Utilities/ArraysTest.php
@@ -843,6 +843,10 @@ public static function getNestedValue(array $array, array $keys)
if (!$array) {
return $array;
}
if (!is_array($array) && self::last($keys) !== $key) {
return null;
}
}
return $array;
}
@@ -886,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]);
@@ -924,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);
@@ -757,6 +757,51 @@ public function getNestedValueShouldReturnNullWhenKeyNotFound()
$this->assertNull($value);
}
/**
* @test
*/
public function getNestedValueShouldReturnNullWhenZeroStringKeyNotFound()
{
//given
$array = ['1' => ['2' => ['3' => 'value']]];
//when
$value = Arrays::getNestedValue($array, ['1', '2', '3', '0']);
//then
$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
*/
@@ -862,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
*/
@@ -984,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 79a741a

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