public function testCreate() { $set = new Set(['a', 'b']); $this->assertTrue($set->contains('a')); $this->assertTrue($set->contains('b')); $this->assertFalse($set->contains('c')); $this->assertEquals(['a', 'b'], $set->toArray()); }
public function is_subset(Set $other) { foreach ($this as $item) { if (!$other->contains($item)) { return false; } } return true; }
/** * @param Set $other * A set of items to intersect with this set * * @return Set * A new set which contains only items in this * Set and the given Set */ public function intersect(Set $other) : Set { $set = new Set(); foreach ($this as $element) { if ($other->contains($element)) { $set->attach($element); } } return $set; }
/** * testContains method * * @return void */ public function testContains() { $a = array(0 => array('name' => 'main'), 1 => array('name' => 'about')); $b = array(0 => array('name' => 'main'), 1 => array('name' => 'about'), 2 => array('name' => 'contact'), 'a' => 'b'); $this->assertTrue(Set::contains($a, $a)); $this->assertFalse(Set::contains($a, $b)); $this->assertTrue(Set::contains($b, $a)); }
/** * Determines if one Set or array contains the exact keys and values of another. * * @param array $val1 First value * @param array $val2 Second value * @return boolean true if $val1 contains $val2, false otherwise * @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::contains */ public static function contains($val1, $val2 = null) { if (empty($val1) || empty($val2)) { return false; } foreach ($val2 as $key => $val) { if (is_numeric($key)) { Set::contains($val, $val1); } else { if (!isset($val1[$key]) || $val1[$key] != $val) { return false; } } } return true; }
public function testContainsAnEmptySet() { $generator = new Set($this->singleElementGenerator); $this->assertTrue($generator->contains([])); }
/** * Checks if the collection contains the given element. * * @param mixed $value * @param bool $strict Use a strict comparison (===) if TRUE */ public function contains($value, $strict = false) { if ($this->guard !== null) { $this->guard->checkMemberGuard(new GuardPermission(__FUNCTION__, 'call')); } return $this->set->contains($value, $strict); }
echo get_class($e), ': ', $e->getMessage(), "\n\n"; } // readonly collection echo "Construct as readonly\n"; $set2 = new Set($arr); $set2->freeze(); Debug::dump($set2->isFrozen()); try { echo "Adding Jack\n"; Debug::dump($set2->append($jack)); } catch (Exception $e) { echo get_class($e), ': ', $e->getMessage(), "\n\n"; } try { echo "Removing Jack\n"; $set2->remove($jack); } catch (Exception $e) { echo get_class($e), ': ', $e->getMessage(), "\n\n"; } try { echo "Clearing\n"; $set2->clear(); } catch (Exception $e) { echo get_class($e), ': ', $e->getMessage(), "\n\n"; } foreach ($set2 as $key => &$val) { $val = FALSE; } echo "Contains Jack?\n"; Debug::dump($set2->contains($jack));