Exemplo n.º 1
0
 /**
  * Checks whether the provided type equals this type or is a subtype of this type.
  *
  * @param Type $type
  * @return boolean
  */
 public function isAssignableFrom(Type $type)
 {
     if ($type->getName() === $this->getName()) {
         return true;
     }
     if (!$type instanceof ClassType) {
         return false;
     }
     return $type->isSubtypeOf($this);
 }
 /**
  * Checks whether the provided type is either an interface that is equal to or extends this interface
  * or is a class that implements this interface.
  *
  * @param Type $type
  * @return boolean
  */
 public function isAssignableFrom(Type $type)
 {
     if ($type->getName() === $this->getName()) {
         return true;
     }
     if ($type instanceof InterfaceType) {
         return $type->isSubtypeOf($this);
     } else {
         if ($type instanceof ClassType) {
             return $type->isImplementorOf($this);
         }
     }
     return false;
 }
Exemplo n.º 3
0
 public function testTypeSubsets()
 {
     $this->assertTrue(Type::isSubtypeOf("string", "string"));
     $this->assertTrue(Type::isSubtypeOf("string", "scalar"));
     $this->assertTrue(Type::isSubtypeOf("numeric", "scalar"));
     $this->assertTrue(Type::isSubtypeOf("bool", "scalar"));
     $this->assertFalse(Type::isSubtypeOf("array", "scalar"));
     $this->assertFalse(Type::isSubtypeOf("object", "scalar"));
     $this->assertTrue(Type::isSubtypeOf("array<string>", "array"));
     $this->assertTrue(Type::isSubtypeOf("array<scalar>", "array"));
     $this->assertTrue(Type::isSubtypeOf("array<string>", "array<scalar>"));
     $this->assertTrue(Type::isSubtypeOf("array<numeric>", "array<numeric>"));
     $this->assertFalse(Type::isSubtypeOf("array<scalar>", "array<string>"));
     $this->assertFalse(Type::isSubtypeOf("array<numeric>", "array<string>"));
 }
Exemplo n.º 4
0
 public function testTypeChecking()
 {
     $types = array('string' => 'hello', 'numeric' => 3, 'bool' => true, 'array' => array(3, 'hello', false, array(1, 2, 3)), 'array<string>' => array('one', 'two'), 'array<scalar>' => array(3, 'hello', false));
     foreach ($types as $expect => $expect_value) {
         foreach ($types as $got => $got_value) {
             $msg = sprintf("Testing expect=%s vs got=%s", $expect, $got);
             $validation = new Validator(array('arg' => $expect), array('arg' => $got_value));
             $msg .= " " . implode(" ", $validation->getErrors());
             if (Type::isSubtypeOf($got, $expect)) {
                 $this->assertTrue($validation->passes(), $msg);
             } else {
                 if ($validation->passes()) {
                     //echo sprintf("%s is NOT a subtype of %s", $got, $expect);
                     $this->assertTrue($validation->fails(), $msg);
                 }
             }
         }
     }
 }