Move Set::check across.

This commit is contained in:
mark_story 2012-02-19 23:11:43 -05:00
parent 771efd950e
commit aa4dca6c0c
2 changed files with 45 additions and 1 deletions

View file

@ -1046,4 +1046,32 @@ class Set2Test extends CakeTestCase {
$this->assertFalse(isset($result[0]['Article']['body'])); $this->assertFalse(isset($result[0]['Article']['body']));
} }
/**
* testCheck method
*
* @return void
*/
public function testCheck() {
$set = array(
'My Index 1' => array('First' => 'The first item')
);
$this->assertTrue(Set2::check($set, 'My Index 1.First'));
$this->assertTrue(Set2::check($set, 'My Index 1'));
$set = array(
'My Index 1' => array(
'First' => array(
'Second' => array(
'Third' => array(
'Fourth' => 'Heavy. Nesting.'
)
)
)
)
);
$this->assertTrue(Set2::check($set, 'My Index 1.First.Second'));
$this->assertTrue(Set2::check($set, 'My Index 1.First.Second.Third'));
$this->assertTrue(Set2::check($set, 'My Index 1.First.Second.Third.Fourth'));
$this->assertFalse(Set2::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
}
} }

View file

@ -350,8 +350,24 @@ class Set2 {
return true; return true;
} }
/**
* Test whether or not a given path exists in $data.
* This method uses the same path syntax as Set2::extract()
*
* Checking for paths that could target more than one element will
* make sure that at least one matching element exists.
*
* @param array $data The data to check.
* @param string $path The path to check for.
* @return boolean Existence of path.
* @see Set2::extract()
*/
public static function check(array $data, $path) { public static function check(array $data, $path) {
$results = self::extract($data, $path);
if (!is_array($results)) {
return false;
}
return count($results) > 0;
} }
/** /**