mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-07 12:36:25 +00:00
Remove duplication where possible.
This commit is contained in:
parent
66eabe462a
commit
6e0e15682f
1 changed files with 11 additions and 171 deletions
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
App::uses('String', 'Utility');
|
App::uses('String', 'Utility');
|
||||||
|
App::uses('Hash', 'Utility');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class used for manipulation of arrays.
|
* Class used for manipulation of arrays.
|
||||||
|
@ -44,20 +45,7 @@ class Set {
|
||||||
*/
|
*/
|
||||||
public static function merge($arr1, $arr2 = null) {
|
public static function merge($arr1, $arr2 = null) {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
|
return call_user_func_array('Hash::merge', $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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,25 +56,7 @@ class Set {
|
||||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::filter
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::filter
|
||||||
*/
|
*/
|
||||||
public static function filter(array $var) {
|
public static function filter(array $var) {
|
||||||
foreach ($var as $k => $v) {
|
return Hash::filter($var);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -214,25 +184,7 @@ class Set {
|
||||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::numeric
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::numeric
|
||||||
*/
|
*/
|
||||||
public static function numeric($array = null) {
|
public static function numeric($array = null) {
|
||||||
if (empty($array)) {
|
return Hash::numeric($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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -667,29 +619,7 @@ class Set {
|
||||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::insert
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::insert
|
||||||
*/
|
*/
|
||||||
public static function insert($list, $path, $data = null) {
|
public static function insert($list, $path, $data = null) {
|
||||||
if (!is_array($path)) {
|
return Hash::insert($list, $path, $data);
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -701,28 +631,7 @@ class Set {
|
||||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::remove
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::remove
|
||||||
*/
|
*/
|
||||||
public static function remove($list, $path = null) {
|
public static function remove($list, $path = null) {
|
||||||
if (empty($path)) {
|
return Hash::remove($list, $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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -858,32 +767,10 @@ class Set {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($assoc) {
|
if ($assoc) {
|
||||||
return Set::normalize($list);
|
return Hash::normalize($list);
|
||||||
}
|
}
|
||||||
} elseif (is_array($list)) {
|
} elseif (is_array($list)) {
|
||||||
$keys = array_keys($list);
|
$list = Hash::normalize($list, $assoc);
|
||||||
$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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return $list;
|
return $list;
|
||||||
}
|
}
|
||||||
|
@ -1012,28 +899,7 @@ class Set {
|
||||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::flatten
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::flatten
|
||||||
*/
|
*/
|
||||||
public static function flatten($data, $separator = '.') {
|
public static function flatten($data, $separator = '.') {
|
||||||
$result = array();
|
return Hash::flatten($data, $separator);
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1048,22 +914,7 @@ class Set {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function expand($data, $separator = '.') {
|
public static function expand($data, $separator = '.') {
|
||||||
$result = array();
|
return Hash::expand($data, $separator);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1245,18 +1096,7 @@ class Set {
|
||||||
} else {
|
} else {
|
||||||
$keys = $path;
|
$keys = $path;
|
||||||
}
|
}
|
||||||
if (!$keys) {
|
return Hash::get($input, $keys);
|
||||||
return $input;
|
|
||||||
}
|
|
||||||
|
|
||||||
$return = $input;
|
|
||||||
foreach ($keys as $key) {
|
|
||||||
if (!isset($return[$key])) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
$return = $return[$key];
|
|
||||||
}
|
|
||||||
return $return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue