Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- final class Fw_Recursive_Array_Helper {
- const ESCAPE = '\\';
- const SEPARATOR = '.';
- /**
- * @param mixed $array
- * @param string $path
- * @param mixed $default
- * @throws InvalidArgumentException
- * @return mixed
- */
- static public function get($array, $path, $default = null) {
- if (!assert_string($path)) {
- throw new InvalidArgumentException('Path must be a string');
- }
- $parts = self::separate($path);
- $value = $array;
- if (!self::has($array, $path)) {
- return $default;
- }
- while (count($parts)) {
- $part = array_shift($parts);
- if (array_key_exists($part, $value)) {
- $value = $value[$part];
- }
- }
- return $value;
- }
- /**
- * Determines if $path exists in $array
- *
- * @param mixed $array
- * @param string $path
- * @return unknown
- */
- static public function has($array, $path) {
- if (!assert_string($path)) {
- return false;
- }
- $parts = explode(self::SEPARATOR, $path);
- while (count($parts)) {
- $part = array_shift($parts);
- if (self::isAcceptedArrayType($array) && array_key_exists($part, $array)) {
- $array = $array[$part];
- } else {
- return false;
- }
- }
- return true;
- }
- /**
- * Sets $value in $array where $path is the recursive Path inside $array
- *
- * @param mixed $array
- * @param string $path
- * @param mixed $value
- * @throws InvalidArgumentException
- * @return array
- */
- static public function set(&$array, $path, $value) {
- if (!assert_string($path)) {
- throw new InvalidArgumentException('Path must be a string');
- }
- if (!self::isAcceptedArrayType($array)) {
- $array = array();
- }
- $parts = self::separate($path);
- if (count($parts) > 1) {
- $part = array_shift($parts);
- if (!self::isAcceptedArrayType($array)) {
- $array = array();
- }
- if (!array_key_exists($part, $array)) {
- $array[$part] = null;
- }
- $array[$part] = self::set($array[$part], join(self::SEPARATOR, $parts), $value);
- } else {
- $array[$path] = $value;
- }
- return $array;
- }
- /**
- * @param string $array
- * @param string $path
- * @return array
- */
- static public function clear(array &$array, $path) {
- $path = self::separate($path);
- self::clearKey($array, $path);
- return $array;
- }
- /**
- * @param array $array
- * @param array $path
- * @return array
- */
- static private function clearKey(array &$array, array $path) {
- if (count($path)) {
- $key = array_shift($path);
- if (array_key_exists($key, $array)) {
- if (count($path)) {
- return self::clearKey($array[$key], $path);
- }
- unset($array[$key]);
- }
- }
- return $array;
- }
- /**
- * Returns parts of $path that are separated by an escape-char
- * Example:
- * escape = \
- * separator = .
- * $path = 'a.bb.ccc.bb...a\.\.'
- * result = array('a', 'bb', 'ccc', 'bb', '', '', 'a..')
- *
- * @param string $path
- * @return array
- */
- static private function separate($path) {
- $res = array();
- $idx = 0;
- for ($i = 0; $i < strlen($path); $i++) {
- $char = $path[$i];
- if ($char == self::ESCAPE) {
- $i++;
- } elseif ($char == self::SEPARATOR) {
- $idx++;
- if (!array_key_exists($idx, $res)) {
- $res[$idx] = '';
- }
- } else {
- if (!array_key_exists($idx, $res)) {
- $res[$idx] = '';
- }
- $res[$idx] .= $char;
- }
- }
- return $res;
- }
- /**
- * Returns true when $array is an array or an ArrayObject
- *
- * @param mixed $array
- * @return bool
- */
- static private function isAcceptedArrayType($array) {
- return is_array($array) || $array instanceof ArrayObject;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement