コード例 #1
0
ファイル: Dictionary.php プロジェクト: karimgeiger/generics
 /**
  * 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;
 }
コード例 #2
0
ファイル: BasicTest.php プロジェクト: karimgeiger/generics
 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());
 }
コード例 #3
0
 public function testSetInvalidValue()
 {
     $generic = new Dictionary('integer', 'string');
     $this->setExpectedException(InvalidTypeException::class, 'Type must be string, but integer was given.');
     $generic->put(1, 1);
 }
コード例 #4
0
ファイル: ArrayList.php プロジェクト: karimgeiger/generics
 /**
  * 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);
 }