mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Make contains() non-recursive.
Add a few more tests for contains().
This commit is contained in:
parent
d37e10a8a9
commit
885d5dfd80
2 changed files with 30 additions and 5 deletions
|
@ -481,8 +481,8 @@ class Set2Test extends CakeTestCase {
|
||||||
*/
|
*/
|
||||||
public function testContains() {
|
public function testContains() {
|
||||||
$data = array('apple', 'bee', 'cyclops');
|
$data = array('apple', 'bee', 'cyclops');
|
||||||
// $this->assertTrue(Set2::contains($data, array('apple')));
|
$this->assertTrue(Set2::contains($data, array('apple')));
|
||||||
// $this->assertFalse(Set2::contains($data, array('data')));
|
$this->assertFalse(Set2::contains($data, array('data')));
|
||||||
|
|
||||||
$a = array(
|
$a = array(
|
||||||
0 => array('name' => 'main'),
|
0 => array('name' => 'main'),
|
||||||
|
@ -498,6 +498,18 @@ class Set2Test extends CakeTestCase {
|
||||||
$this->assertTrue(Set2::contains($a, $a));
|
$this->assertTrue(Set2::contains($a, $a));
|
||||||
$this->assertFalse(Set2::contains($a, $b));
|
$this->assertFalse(Set2::contains($a, $b));
|
||||||
$this->assertTrue(Set2::contains($b, $a));
|
$this->assertTrue(Set2::contains($b, $a));
|
||||||
|
|
||||||
|
$a = array(
|
||||||
|
array('User' => array('id' => 1)),
|
||||||
|
array('User' => array('id' => 2)),
|
||||||
|
);
|
||||||
|
$b = array(
|
||||||
|
array('User' => array('id' => 1)),
|
||||||
|
array('User' => array('id' => 2)),
|
||||||
|
array('User' => array('id' => 3))
|
||||||
|
);
|
||||||
|
$this->assertTrue(Set2::contains($b, $a));
|
||||||
|
$this->assertFalse(Set2::contains($a, $b));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,15 +82,28 @@ class Set2 {
|
||||||
if (empty($data) || empty($needle)) {
|
if (empty($data) || empty($needle)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
$stack = array();
|
||||||
|
|
||||||
|
$i = 1;
|
||||||
|
while (!empty($needle)) {
|
||||||
|
$key = key($needle);
|
||||||
|
$val = $needle[$key];
|
||||||
|
unset($needle[$key]);
|
||||||
|
|
||||||
foreach ($needle as $key => $val) {
|
|
||||||
if (isset($data[$key]) && is_array($val)) {
|
if (isset($data[$key]) && is_array($val)) {
|
||||||
if (!Set2::contains($data[$key], $val)) {
|
$next = $data[$key];
|
||||||
return false;
|
unset($data[$key]);
|
||||||
|
|
||||||
|
if (!empty($val)) {
|
||||||
|
$stack[] = array($val, $next);
|
||||||
}
|
}
|
||||||
} elseif (!isset($data[$key]) || $data[$key] != $val) {
|
} elseif (!isset($data[$key]) || $data[$key] != $val) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (empty($needle) && !empty($stack)) {
|
||||||
|
list($needle, $data) = array_pop($stack);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue