Add remove() and insert()

Also add support for multi insert and multi remove.
This commit is contained in:
mark_story 2012-01-26 21:33:03 -05:00
parent 6b69ed269a
commit 3d8a955043
2 changed files with 185 additions and 35 deletions

View file

@ -974,21 +974,107 @@ class Set2Test extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/**
* Test insert()
*
* @return void
*/
public function testInsertSimple() {
$a = array(
'pages' => array('name' => 'page')
);
$result = Set2::insert($a, 'files', array('name' => 'files'));
$expected = array(
'pages' => array('name' => 'page'),
'files' => array('name' => 'files')
);
$this->assertEquals($expected, $result);
$a = array(
'pages' => array('name' => 'page')
);
$result = Set2::insert($a, 'pages.name', array());
$expected = array(
'pages' => array('name' => array()),
);
$this->assertEquals($expected, $result);
}
/** /**
* Test remove() * Test inserting with multiple values.
*
* @return void
*/
public function testInsertMulti() {
$data = self::articleData();
$result = Set2::insert($data, '{n}.Article.insert', 'value');
$this->assertEquals('value', $result[0]['Article']['insert']);
$this->assertEquals('value', $result[1]['Article']['insert']);
$result = Set2::insert($data, '{n}.Comment.{n}.insert', 'value');
$this->assertEquals('value', $result[0]['Comment'][0]['insert']);
$this->assertEquals('value', $result[0]['Comment'][1]['insert']);
}
/**
* Test remove() method.
* *
* @return void * @return void
*/ */
public function testRemove() { public function testRemove() {
$a = array(
'pages' => array('name' => 'page'),
'files' => array('name' => 'files')
);
$result = Set2::remove($a, 'files');
$expected = array(
'pages' => array('name' => 'page')
);
$this->assertEquals($expected, $result);
$a = array(
'pages' => array(
0 => array('name' => 'main'),
1 => array(
'name' => 'about',
'vars' => array('title' => 'page title')
)
)
);
$result = Set2::remove($a, 'pages.1.vars');
$expected = array(
'pages' => array(
0 => array('name' => 'main'),
1 => array('name' => 'about')
)
);
$this->assertEquals($expected, $result);
$result = Set2::remove($a, 'pages.2.vars');
$expected = $a;
$this->assertEquals($expected, $result);
}
/**
* Test removing multiple values.
*
* @return void
*/
public function testRemoveMulti() {
$data = self::articleData(); $data = self::articleData();
$result = Set2::insert($data, '{n}.Article', array('test')); $result = Set2::remove($data, '{n}.Article.title');
debug($result); $this->assertFalse(isset($result[0]['Article']['title']));
$this->assertFalse(isset($result[1]['Article']['title']));
$result = Set2::remove($data, '{n}.Article'); $result = Set2::remove($data, '{n}.Article.{s}');
debug($result); $this->assertFalse(isset($result[0]['Article']['id']));
$this->assertFalse(isset($data[0]['Article'])); $this->assertFalse(isset($result[0]['Article']['user_id']));
$this->assertFalse(isset($result[0]['Article']['title']));
$this->assertFalse(isset($result[0]['Article']['body']));
} }
} }

View file

@ -94,23 +94,6 @@ class Set2 {
return (array) self::get($data, $path); return (array) self::get($data, $path);
} }
return self::_traverse($data, $path, function ($value) {
return $value;
});
}
/**
* Traverses $data for $path. $callback is called for each terminal element.
* The results of all the callbacks are returned.
*
* @param array $data The data to traverse.
* @param string $path The set path to walk.
* @param callable $callback to call on the result set.
* @return array Results of the callback mapped over the leaf nodes of the path expression.
*/
protected static function _traverse(array &$data, $path, $callback) {
$result = array();
if (strpos('[', $path) === false) { if (strpos('[', $path) === false) {
$tokens = explode('.', $path); $tokens = explode('.', $path);
} else { } else {
@ -150,12 +133,10 @@ class Set2 {
} }
$next = $filter; $next = $filter;
} }
$context = array($_key => $next); $context = array($_key => $next);
} while (!empty($tokens)); } while (!empty($tokens));
return $context[$_key];
return array_map($callback, $context[$_key]);
} }
/** /**
@ -234,16 +215,58 @@ class Set2 {
return true; return true;
} }
/**
* Insert $values into an array with the given $path.
*
* @param array $data The data to insert into.
* @param string $path The path to insert at.
* @param mixed $values The values to insert.
* @return array The data with $values inserted.
*/
public static function insert(array $data, $path, $values = null) { public static function insert(array $data, $path, $values = null) {
if (empty($path)) { $tokens = explode('.', $path);
return $data; if (strpos($path, '{') === false) {
return self::_simpleInsert($data, $tokens, $values);
} }
$result = self::_traverse($data, $path, function (&$value) use ($values) { $token = array_shift($tokens);
$value['test'] = $values; $nextPath = implode('.', $tokens);
return $value; foreach ($data as $k => $v) {
}); if (self::_matchToken($k, $token)) {
$data[$k] = self::insert($v, $nextPath, $values);
}
}
return $data;
}
/**
* Inserts values into simple paths.
*
* @param array $data Data to insert into.
* @param string $path The path to insert into.
* @param mixed $values The values to insert.
* @return array Data with values inserted at $path.
*/
protected static function _simpleInsert($data, $path, $values) {
$_list =& $data;
$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] = $values;
} else {
if (!isset($_list[$key])) {
$_list[$key] = array();
}
$_list =& $_list[$key];
}
if (!is_array($_list)) {
return array();
}
}
return $data; return $data;
} }
@ -255,13 +278,54 @@ class Set2 {
* @return array The modified array. * @return array The modified array.
*/ */
public static function remove(array $data, $path) { public static function remove(array $data, $path) {
$tokens = explode('.', $path);
if (strpos($path, '{') === false) {
return self::_simpleRemove($data, $path);
}
$token = array_shift($tokens);
$nextPath = implode('.', $tokens);
foreach ($data as $k => $v) {
$match = self::_matchToken($k, $token);
if ($match && is_array($v)) {
$data[$k] = self::remove($v, $nextPath);
} elseif ($match) {
unset($data[$k]);
}
}
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)) { if (empty($path)) {
return $data; return $data;
} }
if (!is_array($path)) {
$path = explode('.', $path);
}
$_list =& $data;
return self::_traverse($data, $path, function ($value) { foreach ($path as $i => $key) {
return $value; 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) {