-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from Wixel/multidimensional
Support multidimensional arrays validation
- Loading branch information
Showing
7 changed files
with
355 additions
and
112 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace GUMP; | ||
|
||
class ArrayHelpers | ||
{ | ||
public static function data_get($target, $key, $default = null) | ||
{ | ||
if (is_null($key)) { | ||
return $target; | ||
} | ||
|
||
$key = is_array($key) ? $key : explode('.', $key); | ||
|
||
while (! is_null($segment = array_shift($key))) { | ||
if ($segment === '*') { | ||
if (! is_array($target)) { | ||
return $default; | ||
} | ||
|
||
$result = []; | ||
|
||
foreach ($target as $item) { | ||
$result[] = self::data_get($item, $key); | ||
} | ||
|
||
return in_array('*', $key) ? self::collapse($result) : $result; | ||
} | ||
|
||
if (self::accessible($target) && self::exists($target, $segment)) { | ||
$target = $target[$segment]; | ||
} elseif (is_object($target) && isset($target->{$segment})) { | ||
$target = $target->{$segment}; | ||
} else { | ||
return $default; | ||
} | ||
} | ||
|
||
return $target; | ||
} | ||
|
||
/** | ||
* Determine whether the given value is array accessible. | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public static function accessible($value) | ||
{ | ||
return is_array($value) || $value instanceof ArrayAccess; | ||
} | ||
|
||
/** | ||
* Determine if the given key exists in the provided array. | ||
* | ||
* @param \ArrayAccess|array $array | ||
* @param string|int $key | ||
* @return bool | ||
*/ | ||
public static function exists($array, $key) | ||
{ | ||
if ($array instanceof ArrayAccess) { | ||
return $array->offsetExists($key); | ||
} | ||
|
||
return array_key_exists($key, $array); | ||
} | ||
|
||
/** | ||
* Collapse an array of arrays into a single array. | ||
* | ||
* @param array $array | ||
* @return array | ||
*/ | ||
public static function collapse($array) | ||
{ | ||
$results = []; | ||
|
||
foreach ($array as $values) { | ||
if (! is_array($values)) { | ||
continue; | ||
} | ||
|
||
$results[] = $values; | ||
} | ||
|
||
return array_merge([], ...$results); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters