Permalink
Browse files

Remove duplication where possible.

  • Loading branch information...
1 parent 66eabe4 commit 6e0e15682ffba6bedabefb430cf12d84c034ec86 @markstory markstory committed Mar 21, 2012
Showing with 11 additions and 171 deletions.
  1. +11 −171 lib/Cake/Utility/Set.php
@@ -18,6 +18,7 @@
*/
App::uses('String', 'Utility');
+App::uses('Hash', 'Utility');
/**
* Class used for manipulation of arrays.
@@ -44,20 +45,7 @@ class Set {
*/
public static function merge($arr1, $arr2 = null) {
$args = func_get_args();
-
- $r = (array)current($args);
- while (($arg = next($args)) !== false) {
- foreach ((array)$arg as $key => $val) {
- if (!empty($r[$key]) && is_array($r[$key]) && is_array($val)) {
- $r[$key] = Set::merge($r[$key], $val);
- } elseif (is_int($key)) {
- $r[] = $val;
- } else {
- $r[$key] = $val;
- }
- }
- }
- return $r;
+ return call_user_func_array('Hash::merge', $args);
}
/**
@@ -68,25 +56,7 @@ public static function merge($arr1, $arr2 = null) {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::filter
*/
public static function filter(array $var) {
- foreach ($var as $k => $v) {
- if (is_array($v)) {
- $var[$k] = Set::filter($v);
- }
- }
- return array_filter($var, array('Set', '_filter'));
- }
-
-/**
- * Set::filter callback function
- *
- * @param array $var Array to filter.
- * @return boolean
- */
- protected static function _filter($var) {
- if ($var === 0 || $var === '0' || !empty($var)) {
- return true;
- }
- return false;
+ return Hash::filter($var);
}
/**
@@ -214,25 +184,7 @@ protected static function _map(&$array, $class, $primary = false) {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::numeric
*/
public static function numeric($array = null) {
- if (empty($array)) {
- return null;
- }
-
- if ($array === range(0, count($array) - 1)) {
- return true;
- }
-
- $numeric = true;
- $keys = array_keys($array);
- $count = count($keys);
-
- for ($i = 0; $i < $count; $i++) {
- if (!is_numeric($array[$keys[$i]])) {
- $numeric = false;
- break;
- }
- }
- return $numeric;
+ return Hash::numeric($array);
}
/**
@@ -667,29 +619,7 @@ public static function classicExtract($data, $path = null) {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::insert
*/
public static function insert($list, $path, $data = null) {
- if (!is_array($path)) {
- $path = explode('.', $path);
- }
- $_list =& $list;
-
- $count = count($path);
- foreach ($path as $i => $key) {
- if (is_numeric($key) && intval($key) > 0 || $key === '0') {
- $key = intval($key);
- }
- if ($i === $count - 1 && is_array($_list)) {
- $_list[$key] = $data;
- } else {
- if (!isset($_list[$key])) {
- $_list[$key] = array();
- }
- $_list =& $_list[$key];
- }
- if (!is_array($_list)) {
- $_list = array();
- }
- }
- return $list;
+ return Hash::insert($list, $path, $data);
}
/**
@@ -701,28 +631,7 @@ public static function insert($list, $path, $data = null) {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::remove
*/
public static function remove($list, $path = null) {
- if (empty($path)) {
- return $list;
- }
- if (!is_array($path)) {
- $path = explode('.', $path);
- }
- $_list =& $list;
-
- foreach ($path as $i => $key) {
- if (is_numeric($key) && intval($key) > 0 || $key === '0') {
- $key = intval($key);
- }
- if ($i === count($path) - 1) {
- unset($_list[$key]);
- } else {
- if (!isset($_list[$key])) {
- return $list;
- }
- $_list =& $_list[$key];
- }
- }
- return $list;
+ return Hash::remove($list, $path);
}
/**
@@ -858,32 +767,10 @@ public static function normalize($list, $assoc = true, $sep = ',', $trim = true)
}
}
if ($assoc) {
- return Set::normalize($list);
+ return Hash::normalize($list);
}
} elseif (is_array($list)) {
- $keys = array_keys($list);
- $count = count($keys);
- $numeric = true;
-
- if (!$assoc) {
- for ($i = 0; $i < $count; $i++) {
- if (!is_int($keys[$i])) {
- $numeric = false;
- break;
- }
- }
- }
- if (!$numeric || $assoc) {
- $newList = array();
- for ($i = 0; $i < $count; $i++) {
- if (is_int($keys[$i])) {
- $newList[$list[$keys[$i]]] = null;
- } else {
- $newList[$keys[$i]] = $list[$keys[$i]];
- }
- }
- $list = $newList;
- }
+ $list = Hash::normalize($list, $assoc);
}
return $list;
}
@@ -1012,28 +899,7 @@ public static function reverse($object) {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::flatten
*/
public static function flatten($data, $separator = '.') {
- $result = array();
- $path = null;
-
- if (is_array($separator)) {
- extract($separator, EXTR_OVERWRITE);
- }
-
- if (!is_null($path)) {
- $path .= $separator;
- }
-
- foreach ($data as $key => $val) {
- if (is_array($val)) {
- $result += (array)Set::flatten($val, array(
- 'separator' => $separator,
- 'path' => $path . $key
- ));
- } else {
- $result[$path . $key] = $val;
- }
- }
- return $result;
+ return Hash::flatten($data, $separator);
}
/**
@@ -1048,22 +914,7 @@ public static function flatten($data, $separator = '.') {
* @return array
*/
public static function expand($data, $separator = '.') {
- $result = array();
- foreach ($data as $flat => $value) {
- $keys = explode($separator, $flat);
- $keys = array_reverse($keys);
- $child = array(
- $keys[0] => $value
- );
- array_shift($keys);
- foreach ($keys as $k) {
- $child = array(
- $k => $child
- );
- }
- $result = Set::merge($result, $child);
- }
- return $result;
+ return Hash::expand($data, $separator);
}
/**
@@ -1245,18 +1096,7 @@ public static function get($input, $path = null) {
} else {
$keys = $path;
}
- if (!$keys) {
- return $input;
- }
-
- $return = $input;
- foreach ($keys as $key) {
- if (!isset($return[$key])) {
- return null;
- }
- $return = $return[$key];
- }
- return $return;
+ return Hash::get($input, $keys);
}
}

0 comments on commit 6e0e156

Please sign in to comment.