Exemplo n.º 1
0
 public function testConstruct()
 {
     $type = new Type('object', true, 'ArrayObject', true, new Type('int'), new Type('string'));
     $this->assertEquals(Type::BUILTIN_TYPE_OBJECT, $type->getBuiltinType());
     $this->assertTrue($type->isNullable());
     $this->assertEquals('ArrayObject', $type->getClassName());
     $this->assertTrue($type->isCollection());
     $collectionKeyType = $type->getCollectionKeyType();
     $this->assertInstanceOf('Symfony\\Component\\PropertyInfo\\Type', $collectionKeyType);
     $this->assertEquals(Type::BUILTIN_TYPE_INT, $collectionKeyType->getBuiltinType());
     $collectionValueType = $type->getCollectionValueType();
     $this->assertInstanceOf('Symfony\\Component\\PropertyInfo\\Type', $collectionValueType);
     $this->assertEquals(Type::BUILTIN_TYPE_STRING, $collectionValueType->getBuiltinType());
 }
Exemplo n.º 2
0
 /**
  * Validates the type of the value. Allows using integers as floats for JSON formats.
  *
  * @param string      $attribute
  * @param Type        $type
  * @param mixed       $value
  * @param string|null $format
  *
  * @throws InvalidArgumentException
  */
 protected function validateType(string $attribute, Type $type, $value, string $format = null)
 {
     $builtinType = $type->getBuiltinType();
     if (Type::BUILTIN_TYPE_FLOAT === $builtinType && false !== strpos($format, 'json')) {
         $isValid = is_float($value) || is_int($value);
     } else {
         $isValid = call_user_func('is_' . $builtinType, $value);
     }
     if (!$isValid) {
         throw new InvalidArgumentException(sprintf('The type of the "%s" attribute must be "%s", "%s" given.', $attribute, $builtinType, gettype($value)));
     }
 }