From 4e66128ea41fd6daa9807e888650bd824bceb56a Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 15 Sep 2018 13:30:51 +0200 Subject: [PATCH] Extend functionality based on module needs --- Module/InfoManager.php | 12 ++++++++++++ Utils/ArrayUtils.php | 9 +++++---- tests/Module/InfoManagerTest.php | 1 + tests/Utils/ArrayUtilsTest.php | 3 +++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Module/InfoManager.php b/Module/InfoManager.php index 04a71d5b9..45dfce351 100644 --- a/Module/InfoManager.php +++ b/Module/InfoManager.php @@ -138,6 +138,18 @@ final class InfoManager return $this->info; } + /** + * Get info data. + * + * @return int + * + * @since 1.0.0 + */ + public function getId() : int + { + return $this->info['name']['id'] ?? 0; + } + /** * Get info data. * diff --git a/Utils/ArrayUtils.php b/Utils/ArrayUtils.php index b443c66b8..ca66a4796 100644 --- a/Utils/ArrayUtils.php +++ b/Utils/ArrayUtils.php @@ -151,20 +151,21 @@ final class ArrayUtils * * @param mixed $needle Needle for search * @param array $haystack Haystack for search + * @param mixed $key Key that has to match (optional) * * @return bool * * @since 1.0.0 */ - public static function inArrayRecursive($needle, array $haystack) : bool + public static function inArrayRecursive($needle, array $haystack, $key = null) : bool { $found = false; - foreach ($haystack as $item) { - if ($item === $needle) { + foreach ($haystack as $k => $item) { + if ($item === $needle && ($key === null || $key === $k)) { return true; } elseif (\is_array($item)) { - $found = self::inArrayRecursive($needle, $item); + $found = self::inArrayRecursive($needle, $item, $key); if ($found) { return true; diff --git a/tests/Module/InfoManagerTest.php b/tests/Module/InfoManagerTest.php index 6ac7d988c..f0b6b46ce 100644 --- a/tests/Module/InfoManagerTest.php +++ b/tests/Module/InfoManagerTest.php @@ -27,6 +27,7 @@ class InfoManagerTest extends \PHPUnit\Framework\TestCase $jarray = \json_decode(file_get_contents(__DIR__ . '/info-test.json'), true); self::assertEquals($jarray, $info->get()); + self::assertEquals($jarray['name']['id'], $info->getId()); self::assertEquals($jarray['name']['internal'], $info->getInternalName()); self::assertEquals($jarray['name']['external'], $info->getExternalName()); self::assertEquals($jarray['category'], $info->getCategory()); diff --git a/tests/Utils/ArrayUtilsTest.php b/tests/Utils/ArrayUtilsTest.php index 8db395924..88e0281b4 100644 --- a/tests/Utils/ArrayUtilsTest.php +++ b/tests/Utils/ArrayUtilsTest.php @@ -64,6 +64,9 @@ class ArrayUtilsTest extends \PHPUnit\Framework\TestCase ]; self::assertTrue(ArrayUtils::inArrayRecursive('aba', $expected)); + self::assertTrue(ArrayUtils::inArrayRecursive('2a', $expected)); + self::assertTrue(ArrayUtils::inArrayRecursive('2a', $expected, 2)); + self::assertFalse(ArrayUtils::inArrayRecursive('2a', $expected, 3)); self::assertFalse(ArrayUtils::inArrayRecursive('aba', ArrayUtils::unsetArray('a/ab', $expected, '/'))); }