Beispiel #1
0
 public static function isScalarType($type)
 {
     if (is_object($type)) {
         return $type instanceof AbstractScalarType || $type instanceof AbstractEnumType;
     }
     return in_array(strtolower($type), TypeFactory::getScalarTypesNames());
 }
Beispiel #2
0
 public function testScalarPrimitives()
 {
     foreach (TypeFactory::getScalarTypesNames() as $typeName) {
         $scalarType = TypeFactory::getScalarType($typeName);
         $testDataMethod = 'get' . $typeName . 'TestData';
         $this->assertNotEmpty($scalarType->getDescription());
         $this->assertEquals($scalarType->getKind(), TypeMap::KIND_SCALAR);
         $this->assertEquals($scalarType->isCompositeType(), false);
         $this->assertEquals(TypeService::isAbstractType($scalarType), false);
         $this->assertEquals($scalarType->getType(), $scalarType);
         $this->assertEquals($scalarType->getType(), $scalarType->getNamedType());
         $this->assertNull($scalarType->getConfig());
         foreach (call_user_func(['Youshido\\Tests\\DataProvider\\TestScalarDataProvider', $testDataMethod]) as list($data, $serialized, $isValid)) {
             $this->assertSerialization($scalarType, $data, $serialized);
             $this->assertParse($scalarType, $data, $serialized);
             if ($isValid) {
                 $this->assertTrue($scalarType->isValidValue($data), $typeName . ' validation for :' . serialize($data));
             } else {
                 $this->assertFalse($scalarType->isValidValue($data), $typeName . ' validation for :' . serialize($data));
             }
         }
     }
     try {
         TypeFactory::getScalarType('invalid type');
     } catch (\Exception $e) {
         $this->assertEquals('Configuration problem with type invalid type', $e->getMessage());
     }
     $this->assertEquals('String', (string) new StringType());
 }
Beispiel #3
0
 /**
  * NonNullType constructor.
  *
  * @param AbstractType|string $fieldType
  *
  * @throws ConfigurationException
  */
 public function __construct($fieldType)
 {
     if (!TypeService::isGraphQLType($fieldType)) {
         throw new ConfigurationException('NonNullType accepts only GraphpQL Types as argument');
     }
     if (TypeService::isScalarType($fieldType)) {
         $fieldType = TypeFactory::getScalarType($fieldType);
     }
     $this->_typeOf = $fieldType;
 }
 public function __construct(array $config = [])
 {
     if (empty($config['type'])) {
         $config['type'] = $this->getType();
         $config['name'] = $this->getName();
     }
     if (TypeService::isScalarType($config['type'])) {
         $config['type'] = TypeFactory::getScalarType($config['type']);
     }
     $this->config = new InputFieldConfig($config, $this, $this->isFinal);
     $this->build($this->config);
 }
 public function validate($data, $ruleInfo)
 {
     if (!is_string($ruleInfo)) {
         return false;
     }
     switch ($ruleInfo) {
         case TypeService::TYPE_ANY:
             return true;
         case TypeService::TYPE_ANY_OBJECT:
             return is_object($data);
         case TypeService::TYPE_CALLABLE:
             return is_callable($data);
         case TypeService::TYPE_BOOLEAN:
             return is_bool($data);
         case TypeService::TYPE_ARRAY:
             return is_array($data);
         case TypeService::TYPE_STRING:
             return TypeFactory::getScalarType($ruleInfo)->isValidValue($data);
         case TypeService::TYPE_GRAPHQL_TYPE:
             return TypeService::isGraphQLType($data);
         case TypeService::TYPE_OBJECT_TYPE:
             return TypeService::isObjectType($data);
         case TypeService::TYPE_ARRAY_OF_OBJECT_TYPES:
             return $this->isArrayOfObjectTypes($data);
         case TypeService::TYPE_ARRAY_OF_FIELDS_CONFIG:
             return $this->isArrayOfFields($data);
         case TypeService::TYPE_OBJECT_INPUT_TYPE:
             return TypeService::isInputObjectType($data);
         case TypeService::TYPE_ENUM_VALUES:
             return $this->isEnumValues($data);
         case TypeService::TYPE_ARRAY_OF_INPUT_FIELDS:
             return $this->isArrayOfInputFields($data);
         case TypeService::TYPE_ANY_INPUT:
             return TypeService::isInputType($data);
         case TypeService::TYPE_ARRAY_OF_INTERFACES:
             return $this->isArrayOfInterfaces($data);
         default:
             return false;
     }
 }