/** * 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; }
public function testMergeSelf() { $generic = new Dictionary('string', 'string', ['foo' => 'bar']); $generic->merge(new Dictionary('string', 'string', ['baz' => 'foo'])); $this->assertEquals(['foo' => 'bar', 'baz' => 'foo'], $generic->toArray()); }
public function testSetInvalidValue() { $generic = new Dictionary('integer', 'string'); $this->setExpectedException(InvalidTypeException::class, 'Type must be string, but integer was given.'); $generic->put(1, 1); }
/** * ArrayList constructor. Key will always be int. Allowed types for TValue: * * boolean/bool * * integer/int * * double/float * * string * * [any class, e.g.: stdClass/Generic/...] * * @param string $TValue Type for each item. This will be returned instead of TValue. * @param array|Iterator|Dictionary $data Data to fill. Must be compatible. */ public function __construct(string $TValue, $data = null) { parent::__construct('integer', $TValue, $data); }