Skip to content

Commit

Permalink
Allow $path to be an array in Arr::path, fixes #3260
Browse files Browse the repository at this point in the history
  • Loading branch information
Woody Gilk committed Sep 24, 2010
1 parent d3feb21 commit 9881512
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
41 changes: 26 additions & 15 deletions classes/kohana/arr.php
Expand Up @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions tests/kohana/ArrTest.php
Expand Up @@ -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')),
);
}

Expand Down

0 comments on commit 9881512

Please sign in to comment.