Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
App::uses('Set2', 'Utility'); | ||
|
||
class Set2Test extends CakeTestCase { | ||
|
||
public static function articleData() { | ||
return array( | ||
array( | ||
'Article' => array( | ||
'id' => '1', | ||
'user_id' => '1', | ||
'title' => 'First Article', | ||
'body' => 'First Article Body' | ||
), | ||
'User' => array( | ||
'id' => '1', | ||
'user' => 'mariano', | ||
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', | ||
), | ||
'Comment' => array( | ||
array( | ||
'id' => '1', | ||
'article_id' => '1', | ||
'user_id' => '2', | ||
'comment' => 'First Comment for First Article', | ||
), | ||
array( | ||
'id' => '2', | ||
'article_id' => '1', | ||
'user_id' => '4', | ||
'comment' => 'Second Comment for First Article', | ||
), | ||
), | ||
'Tag' => array( | ||
array( | ||
'id' => '1', | ||
'tag' => 'tag1', | ||
), | ||
array( | ||
'id' => '2', | ||
'tag' => 'tag2', | ||
) | ||
), | ||
'Deep' => array( | ||
'Nesting' => array( | ||
'test' => array( | ||
1 => 'foo', | ||
2 => array( | ||
'and' => array('more' => 'stuff') | ||
) | ||
) | ||
) | ||
) | ||
), | ||
array( | ||
'Article' => array( | ||
'id' => '3', | ||
'user_id' => '1', | ||
'title' => 'Second Article', | ||
'body' => 'Second Article Body', | ||
'published' => 'Y', | ||
), | ||
'User' => array( | ||
'id' => '2', | ||
'user' => 'mariano', | ||
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', | ||
), | ||
'Comment' => array(), | ||
'Tag' => array() | ||
), | ||
array( | ||
'Article' => array( | ||
'id' => '3', | ||
'user_id' => '1', | ||
'title' => 'Third Article', | ||
'body' => 'Third Article Body', | ||
), | ||
'User' => array( | ||
'id' => '3', | ||
'user' => 'mariano', | ||
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', | ||
), | ||
'Comment' => array(), | ||
'Tag' => array() | ||
), | ||
array( | ||
'Article' => array( | ||
'id' => '4', | ||
'user_id' => '1', | ||
'title' => 'Fourth Article', | ||
'body' => 'Fourth Article Body', | ||
), | ||
'User' => array( | ||
'id' => '4', | ||
'user' => 'mariano', | ||
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', | ||
), | ||
'Comment' => array(), | ||
'Tag' => array() | ||
), | ||
array( | ||
'Article' => array( | ||
'id' => '5', | ||
'user_id' => '1', | ||
'title' => 'Fifth Article', | ||
'body' => 'Fifth Article Body', | ||
), | ||
'User' => array( | ||
'id' => '5', | ||
'user' => 'mariano', | ||
'password' => '5f4dcc3b5aa765d61d8327deb882cf99', | ||
), | ||
'Comment' => array(), | ||
'Tag' => array() | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Test get() | ||
* | ||
* return void | ||
*/ | ||
public function testGet() { | ||
$data = self::articleData(); | ||
|
||
$result = Set2::get(array(), '1.Article.title'); | ||
$this->assertNull($result); | ||
|
||
$result = Set2::get($data, ''); | ||
$this->assertNull($result); | ||
|
||
$result = Set2::get($data, '1.Article.title'); | ||
$this->assertEquals('Second Article', $result); | ||
|
||
$result = Set2::get($data, '5.Article.title'); | ||
$this->assertNull($result); | ||
|
||
$result = Set2::get($data, '1.Article.title.not_there'); | ||
$this->assertNull($result); | ||
|
||
$result = Set2::get($data, '1.Article'); | ||
$this->assertEquals($data[1]['Article'], $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* PHP 5 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @package Cake.Utility | ||
* @since CakePHP(tm) v 1.2.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
|
||
App::uses('String', 'Utility'); | ||
|
||
/** | ||
* Library of array functions for manipulating and extracting data | ||
* from arrays or 'sets' of data. | ||
* | ||
* `Set2` provides an improved interface and more consistent and | ||
* predictable set of features over `Set`. While it lacks the spotty | ||
* support for pseudo Xpath, its more fully feature dot notation provides | ||
* the same utility. | ||
* | ||
* @package Cake.Utility | ||
*/ | ||
class Set2 { | ||
|
||
/** | ||
* Get a single value specified by $path out of $data. | ||
* Does not support the full dot notation feature set, | ||
* but is faster for simple operations. | ||
* | ||
* @param array $data Array of data to operate on. | ||
* @param string $path The path being searched for. | ||
* @return mixed The value fetched from the array, or null. | ||
*/ | ||
public static function get(array $data, $path) { | ||
if (empty($data) || empty($path)) { | ||
return null; | ||
} | ||
$parts = explode('.', $path); | ||
while ($key = array_shift($parts)) { | ||
if (isset($data[$key])) { | ||
$data =& $data[$key]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
return $data; | ||
} | ||
} |