From 98815127aa6d54c07d32c0e814906ed013426e07 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Fri, 24 Sep 2010 10:42:42 -0500 Subject: [PATCH] Allow $path to be an array in Arr::path, fixes #3260 --- classes/kohana/arr.php | 41 +++++++++++++++++++++++++--------------- tests/kohana/ArrTest.php | 2 ++ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/classes/kohana/arr.php b/classes/kohana/arr.php index 468c42543..69fdee260 100644 --- a/classes/kohana/arr.php +++ b/classes/kohana/arr.php @@ -48,34 +48,45 @@ public static function is_assoc(array $array) * // Get the values of "color" in theme * $colors = Arr::path($array, 'theme.*.color'); * + * // Using an array of keys + * $colors = Arr::path($array, array('theme', '*', 'color')); + * * @param array array to search - * @param string key path, delimiter separated + * @param mixed key path string (delimiter separated) or array of keys * @param mixed default value if the path is not set * @param string key path delimiter * @return mixed */ public static function path($array, $path, $default = NULL, $delimiter = NULL) { - if (array_key_exists($path, $array)) + if (is_array($path)) { - // No need to do extra processing - return $array[$path]; + // The path has already been separated into keys + $keys = $path; } - - if ($delimiter === NULL) + else { - // Use the default delimiter - $delimiter = Arr::$delimiter; - } + if (array_key_exists($path, $array)) + { + // No need to do extra processing + return $array[$path]; + } - // Remove starting delimiters and spaces - $path = ltrim($path, "{$delimiter} "); + if ($delimiter === NULL) + { + // Use the default delimiter + $delimiter = Arr::$delimiter; + } - // Remove ending delimiters, spaces, and wildcards - $path = rtrim($path, "{$delimiter} *"); + // Remove starting delimiters and spaces + $path = ltrim($path, "{$delimiter} "); - // Split the keys by delimiter - $keys = explode($delimiter, $path); + // Remove ending delimiters, spaces, and wildcards + $path = rtrim($path, "{$delimiter} *"); + + // Split the keys by delimiter + $keys = explode($delimiter, $path); + } do { diff --git a/tests/kohana/ArrTest.php b/tests/kohana/ArrTest.php index 72d312622..fcc5481d2 100644 --- a/tests/kohana/ArrTest.php +++ b/tests/kohana/ArrTest.php @@ -260,6 +260,8 @@ public function provider_path() array(NULL, $array, 'users.*.fans'), // Starting wildcards, issue #3269 array(array('matt', 'john'), $array['users'], '*.name'), + // Path as array, issue #3260 + array($array['users'][2]['name'], $array, array('users', 2, 'name')), ); }