Permalink
Browse files

Move Set::check across.

  • Loading branch information...
1 parent 771efd9 commit aa4dca6c0c6c55b6a41a451b3b107cb861873c3e @markstory markstory committed Feb 20, 2012
Showing with 45 additions and 1 deletion.
  1. +28 −0 lib/Cake/Test/Case/Utility/Set2Test.php
  2. +17 −1 lib/Cake/Utility/Set2.php
@@ -1046,4 +1046,32 @@ public function testRemoveMulti() {
$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
@@ -350,8 +350,24 @@ public static function contains(array $data, array $needle) {
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) {
-
+ $results = self::extract($data, $path);
+ if (!is_array($results)) {
+ return false;
+ }
+ return count($results) > 0;
}
/**

0 comments on commit aa4dca6

Please sign in to comment.