Skip to content

Commit

Permalink
Extract common method.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 27, 2012
1 parent 3d8a955 commit 57d50cd
Showing 1 changed file with 29 additions and 49 deletions.
78 changes: 29 additions & 49 deletions lib/Cake/Utility/Set2.php
Expand Up @@ -226,7 +226,7 @@ protected static function _matches(array $data, $selector) {
public static function insert(array $data, $path, $values = null) { public static function insert(array $data, $path, $values = null) {
$tokens = explode('.', $path); $tokens = explode('.', $path);
if (strpos($path, '{') === false) { if (strpos($path, '{') === false) {
return self::_simpleInsert($data, $tokens, $values); return self::_simpleOp('insert', $data, $tokens, $values);
} }


$token = array_shift($tokens); $token = array_shift($tokens);
Expand All @@ -240,31 +240,43 @@ public static function insert(array $data, $path, $values = null) {
} }


/** /**
* Inserts values into simple paths. * Perform a simple insert/remove operation.
* *
* @param array $data Data to insert into. * @param string $op The operation to do.
* @param string $path The path to insert into. * @param array $data The data to operate on.
* @param mixed $values The values to insert. * @param array $path The path to work on.
* @return array Data with values inserted at $path. * @param mixed $values The values to insert when doing inserts.
* @return array $data.
*/ */
protected static function _simpleInsert($data, $path, $values) { protected static function _simpleOp($op, $data, $path, $values = null) {
$_list =& $data; $_list =& $data;


$count = count($path); $count = count($path);
foreach ($path as $i => $key) { foreach ($path as $i => $key) {
if (is_numeric($key) && intval($key) > 0 || $key === '0') { if (is_numeric($key) && intval($key) > 0 || $key === '0') {
$key = intval($key); $key = intval($key);
} }
if ($i === $count - 1 && is_array($_list)) { if ($op === 'insert') {
$_list[$key] = $values; if ($i === $count - 1 && is_array($_list)) {
} else { $_list[$key] = $values;
if (!isset($_list[$key])) { } else {
$_list[$key] = array(); if (!isset($_list[$key])) {
$_list[$key] = array();
}
$_list =& $_list[$key];
}
if (!is_array($_list)) {
return array();
}
} elseif ($op === 'remove') {
if ($i === count($path) - 1) {
unset($_list[$key]);
} else {
if (!isset($_list[$key])) {
return $data;
}
$_list =& $_list[$key];
} }
$_list =& $_list[$key];
}
if (!is_array($_list)) {
return array();
} }
} }
return $data; return $data;
Expand All @@ -280,7 +292,7 @@ protected static function _simpleInsert($data, $path, $values) {
public static function remove(array $data, $path) { public static function remove(array $data, $path) {
$tokens = explode('.', $path); $tokens = explode('.', $path);
if (strpos($path, '{') === false) { if (strpos($path, '{') === false) {
return self::_simpleRemove($data, $path); return self::_simpleOp('remove', $data, $tokens);
} }


$token = array_shift($tokens); $token = array_shift($tokens);
Expand All @@ -296,38 +308,6 @@ public static function remove(array $data, $path) {
return $data; return $data;
} }


/**
* Remove values along a simple path.
*
* @param array $data Array to operate on.
* @param string $path The path to remove.
* @return array Data with value removed.
*/
protected static function _simpleRemove($data, $path) {
if (empty($path)) {
return $data;
}
if (!is_array($path)) {
$path = explode('.', $path);
}
$_list =& $data;

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 $data;
}
$_list =& $_list[$key];
}
}
return $data;
}

public static function combine(array $data, $keyPath, $valuePath = null) { public static function combine(array $data, $keyPath, $valuePath = null) {


} }
Expand Down

0 comments on commit 57d50cd

Please sign in to comment.