Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Set::expand as implemented by jeremyharris

  • Loading branch information...
commit 079ae8265ca38ff700d3c7be5dcd76f85594e65f 1 parent 8fdb111
@rchavik rchavik authored
Showing with 76 additions and 0 deletions.
  1. +46 −0 lib/Cake/Test/Case/Utility/SetTest.php
  2. +30 −0 lib/Cake/Utility/Set.php
View
46 lib/Cake/Test/Case/Utility/SetTest.php
@@ -3130,6 +3130,52 @@ public function testFlatten() {
}
/**
+ * Tests Set::expand
+ *
+ * @return void
+ */
+ public function testExpand() {
+ $data = array('My', 'Array', 'To', 'Flatten');
+ $flat = Set::flatten($data);
+ $result = Set::expand($flat);
+ $this->assertEqual($data, $result);
+
+ $data = array(
+ '0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1',
+ '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3',
+ '1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
+ '1.Author.user' => 'larry', '1.Author.password' => null
+ );
+ $result = Set::expand($data);
+ $expected = array(
+ array(
+ 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
+ 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
+ ),
+ array(
+ 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
+ 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
+ )
+ );
+ $this->assertEqual($result, $expected);
+
+ $data = array(
+ '0/Post/id' => 1,
+ '0/Post/name' => 'test post'
+ );
+ $result = Set::expand($data, '/');
+ $expected = array(
+ array(
+ 'Post' => array(
+ 'id' => 1,
+ 'name' => 'test post'
+ )
+ )
+ );
+ $this->assertEqual($result, $expected);
+ }
+
+/**
* test normalization
*
* @return void
View
30 lib/Cake/Utility/Set.php
@@ -1037,6 +1037,36 @@ public static function flatten($data, $separator = '.') {
}
/**
+ * Expand/unflattens an string to an array
+ *
+ * For example, unflattens an array that was collapsed with `Set::flatten()`
+ * into a multi-dimensional array. So, `array('0.Foo.Bar' => 'Far')` becomes
+ * `array(array('Foo' => array('Bar' => 'Far')))`.
+ *
+ * @param array $data Flattened array
+ * @param string $separator The delimiter used
+ * @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;
+ }
+
+/**
* Flattens an array for sorting
*
* @param array $results
Please sign in to comment.
Something went wrong with that request. Please try again.