Skip to content

Commit

Permalink
Add Arrays::getNestedValue
Browse files Browse the repository at this point in the history
  • Loading branch information
jfilla committed Nov 15, 2019
1 parent 21613e5 commit a34de23
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
27 changes: 20 additions & 7 deletions src/Utils/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Flow\JSONPath\JSONPath;
use Flow\JSONPath\JSONPathException;
use Nette\InvalidArgumentException;
use Nette\InvalidStateException;
use Nette\Utils\ArrayHash;
use Nette\Utils\Arrays as NetteArrays;
Expand Down Expand Up @@ -225,15 +224,12 @@ public static function fromArrayHash(ArrayHash $arrayHash): array

/**
* @param array<mixed> $array
* @param string ...$keyParts
* @param string ...$keys
* @return bool
*/
public static function hasNestedKey(array $array, string ...$keyParts): bool
public static function hasNestedKey(array $array, string ...$keys): bool
{
if (count($keyParts) === 0) {
throw new InvalidArgumentException('Argument "keyParts" should have at least one element.');
}
foreach ($keyParts as $keyPart) {
foreach ($keys as $keyPart) {
if (key_exists($keyPart, $array)) {
$array = $array[$keyPart];
if (!is_array($array)) {
Expand All @@ -246,6 +242,23 @@ public static function hasNestedKey(array $array, string ...$keyParts): bool
return true;
}

/**
* @param array<mixed> $array
* @param string ...$keys
* @return mixed
*/
public static function getNestedValue(array $array, string ...$keys)
{
if (self::hasNestedKey($array, ...$keys)) {
$current = $array;
foreach ($keys as $key) {
$current = $current[$key];
}
return $current;
}
return null;
}

/**
* @param array<mixed> $a1
* @param array<mixed> $a2
Expand Down
19 changes: 15 additions & 4 deletions tests/UtilsTests/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Wavevision\UtilsTests;

use Nette\InvalidArgumentException;
use Nette\InvalidStateException;
use Nette\Utils\ArrayHash;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -140,9 +139,21 @@ public function testHasNestedKey(): void
$this->assertEquals(true, Arrays::hasNestedKey($array, 'parts', '0', 'format'));
$this->assertEquals(false, Arrays::hasNestedKey($array, 'parts', '1', 'format'));
$this->assertEquals(false, Arrays::hasNestedKey($array, '42'));
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Argument "keyParts" should have at least one element.');
Arrays::hasNestedKey([]);
$this->assertEquals(true, Arrays::hasNestedKey($array));
}

public function testGetNestedValue(): void
{
$array = [
'1' => [
'2' => 42,
],
];
$this->assertEquals(['2' => 42], Arrays::getNestedValue($array, '1'));
$this->assertEquals(42, Arrays::getNestedValue($array, '1', '2'));
$this->assertEquals(null, Arrays::getNestedValue($array, '3'));
$this->assertEquals(null, Arrays::getNestedValue([], 'x'));
$this->assertEquals($array, Arrays::getNestedValue($array));
}

public function testHasSameValues(): void
Expand Down

0 comments on commit a34de23

Please sign in to comment.