Youshido\GraphQL\Type\TypeFactory::getScalarType PHP Метод

getScalarType() публичный статический Метод

public static getScalarType ( string $type ) : AbstractScalarType
$type string
Результат Youshido\GraphQL\Type\Scalar\AbstractScalarType
    public static function getScalarType($type)
    {
        if (TypeService::isScalarType($type)) {
            if (is_object($type)) {
                $typeName = $type->getName();
                if (empty(self::$objectsHash[$typeName])) {
                    self::$objectsHash[$typeName] = $type;
                }
                return self::$objectsHash[$typeName];
            }
            if (empty(self::$objectsHash[$type])) {
                $name = ucfirst($type);
                $name = $name == 'Datetime' ? 'DateTime' : $name;
                $name = $name == 'Datetimetz' ? 'DateTimeTz' : $name;
                $className = 'Youshido\\GraphQL\\Type\\Scalar\\' . $name . 'Type';
                self::$objectsHash[$type] = new $className();
            }
            return self::$objectsHash[$type];
        } else {
            throw new ConfigurationException('Configuration problem with type ' . $type);
        }
    }

Usage Example

Пример #1
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());
 }
All Usage Examples Of Youshido\GraphQL\Type\TypeFactory::getScalarType