public function testDefaultScenario() { $generic = new Dictionary('integer', 'string', [1 => 'bar']); $this->assertEquals(['integer', 'string'], $generic->getTypes()); $generic->put(2, 'foo'); $generic->put(3, 'baz'); $this->assertEquals([1 => 'bar', 2 => 'foo', 3 => 'baz'], $generic->toArray()); $this->assertEquals('baz', $generic->pull(3)); $this->assertCount(2, $generic); $generic[100] = 'something'; $this->assertCount(3, $generic); $this->assertTrue($generic->has('something')); $this->assertFalse($generic->has('something_not')); $this->assertTrue(isset($generic[100])); }
/** * Validate if both Generics or arrays are compatible. * * @param Dictionary|Iterator|array $other * @return bool */ public function isCompatible($other) : bool { try { if ($other instanceof Dictionary) { return $this->getTypes() === $other->getTypes(); } elseif (is_array($other) || $other instanceof Iterator) { foreach ($other as $key => $value) { $this->validateEntry($key, $value); } return true; } } catch (InvalidTypeException $e) { } return false; }