From aa4dca6c0c6c55b6a41a451b3b107cb861873c3e Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 19 Feb 2012 23:11:43 -0500 Subject: [PATCH] Move Set::check across. --- lib/Cake/Test/Case/Utility/Set2Test.php | 28 +++++++++++++++++++++++++ lib/Cake/Utility/Set2.php | 18 +++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Utility/Set2Test.php b/lib/Cake/Test/Case/Utility/Set2Test.php index 81f17a07c..4112af170 100644 --- a/lib/Cake/Test/Case/Utility/Set2Test.php +++ b/lib/Cake/Test/Case/Utility/Set2Test.php @@ -1046,4 +1046,32 @@ class Set2Test extends CakeTestCase { $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')); + } } diff --git a/lib/Cake/Utility/Set2.php b/lib/Cake/Utility/Set2.php index b94c0984e..64ab4c7a3 100644 --- a/lib/Cake/Utility/Set2.php +++ b/lib/Cake/Utility/Set2.php @@ -350,8 +350,24 @@ class Set2 { 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; } /**