vendor/webonyx/graphql-php/src/Type/Definition/Type.php line 362

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace GraphQL\Type\Definition;
  4. use Exception;
  5. use GraphQL\Error\InvariantViolation;
  6. use GraphQL\Language\AST\TypeDefinitionNode;
  7. use GraphQL\Language\AST\TypeExtensionNode;
  8. use GraphQL\Type\Introspection;
  9. use GraphQL\Utils\Utils;
  10. use JsonSerializable;
  11. use ReflectionClass;
  12. use Throwable;
  13. use function array_keys;
  14. use function array_merge;
  15. use function implode;
  16. use function in_array;
  17. use function preg_replace;
  18. use function trigger_error;
  19. use const E_USER_DEPRECATED;
  20. /**
  21.  * Registry of standard GraphQL types
  22.  * and a base class for all other types.
  23.  */
  24. abstract class Type implements JsonSerializable
  25. {
  26.     public const STRING  'String';
  27.     public const INT     'Int';
  28.     public const BOOLEAN 'Boolean';
  29.     public const FLOAT   'Float';
  30.     public const ID      'ID';
  31.     /** @var Type[] */
  32.     private static $standardTypes;
  33.     /** @var Type[] */
  34.     private static $builtInTypes;
  35.     /** @var string */
  36.     public $name;
  37.     /** @var string|null */
  38.     public $description;
  39.     /** @var TypeDefinitionNode|null */
  40.     public $astNode;
  41.     /** @var mixed[] */
  42.     public $config;
  43.     /** @var TypeExtensionNode[] */
  44.     public $extensionASTNodes;
  45.     /**
  46.      * @return IDType
  47.      *
  48.      * @api
  49.      */
  50.     public static function id()
  51.     {
  52.         return self::getStandardType(self::ID);
  53.     }
  54.     /**
  55.      * @param string $name
  56.      *
  57.      * @return (IDType|StringType|FloatType|IntType|BooleanType)[]|IDType|StringType|FloatType|IntType|BooleanType
  58.      */
  59.     private static function getStandardType($name null)
  60.     {
  61.         if (self::$standardTypes === null) {
  62.             self::$standardTypes = [
  63.                 self::ID      => new IDType(),
  64.                 self::STRING  => new StringType(),
  65.                 self::FLOAT   => new FloatType(),
  66.                 self::INT     => new IntType(),
  67.                 self::BOOLEAN => new BooleanType(),
  68.             ];
  69.         }
  70.         return $name self::$standardTypes[$name] : self::$standardTypes;
  71.     }
  72.     /**
  73.      * @return StringType
  74.      *
  75.      * @api
  76.      */
  77.     public static function string()
  78.     {
  79.         return self::getStandardType(self::STRING);
  80.     }
  81.     /**
  82.      * @return BooleanType
  83.      *
  84.      * @api
  85.      */
  86.     public static function boolean()
  87.     {
  88.         return self::getStandardType(self::BOOLEAN);
  89.     }
  90.     /**
  91.      * @return IntType
  92.      *
  93.      * @api
  94.      */
  95.     public static function int()
  96.     {
  97.         return self::getStandardType(self::INT);
  98.     }
  99.     /**
  100.      * @return FloatType
  101.      *
  102.      * @api
  103.      */
  104.     public static function float()
  105.     {
  106.         return self::getStandardType(self::FLOAT);
  107.     }
  108.     /**
  109.      * @param Type|ObjectType|InterfaceType|UnionType|ScalarType|InputObjectType|EnumType|ListOfType|NonNull $wrappedType
  110.      *
  111.      * @return ListOfType
  112.      *
  113.      * @api
  114.      */
  115.     public static function listOf($wrappedType)
  116.     {
  117.         return new ListOfType($wrappedType);
  118.     }
  119.     /**
  120.      * @param NullableType $wrappedType
  121.      *
  122.      * @return NonNull
  123.      *
  124.      * @api
  125.      */
  126.     public static function nonNull($wrappedType)
  127.     {
  128.         return new NonNull($wrappedType);
  129.     }
  130.     /**
  131.      * Checks if the type is a builtin type
  132.      *
  133.      * @return bool
  134.      */
  135.     public static function isBuiltInType(Type $type)
  136.     {
  137.         return in_array($type->namearray_keys(self::getAllBuiltInTypes()), true);
  138.     }
  139.     /**
  140.      * Returns all builtin in types including base scalar and
  141.      * introspection types
  142.      *
  143.      * @return Type[]
  144.      */
  145.     public static function getAllBuiltInTypes()
  146.     {
  147.         if (self::$builtInTypes === null) {
  148.             self::$builtInTypes array_merge(
  149.                 Introspection::getTypes(),
  150.                 self::getStandardTypes()
  151.             );
  152.         }
  153.         return self::$builtInTypes;
  154.     }
  155.     /**
  156.      * Returns all builtin scalar types
  157.      *
  158.      * @return Type[]
  159.      */
  160.     public static function getStandardTypes()
  161.     {
  162.         return self::getStandardType();
  163.     }
  164.     /**
  165.      * @deprecated Use method getStandardTypes() instead
  166.      *
  167.      * @return Type[]
  168.      */
  169.     public static function getInternalTypes()
  170.     {
  171.         trigger_error(__METHOD__ ' is deprecated. Use Type::getStandardTypes() instead'E_USER_DEPRECATED);
  172.         return self::getStandardTypes();
  173.     }
  174.     /**
  175.      * @param Type[] $types
  176.      */
  177.     public static function overrideStandardTypes(array $types)
  178.     {
  179.         $standardTypes self::getStandardTypes();
  180.         foreach ($types as $type) {
  181.             Utils::invariant(
  182.                 $type instanceof Type,
  183.                 'Expecting instance of %s, got %s',
  184.                 self::class,
  185.                 Utils::printSafe($type)
  186.             );
  187.             Utils::invariant(
  188.                 isset($type->name$standardTypes[$type->name]),
  189.                 'Expecting one of the following names for a standard type: %s, got %s',
  190.                 implode(', 'array_keys($standardTypes)),
  191.                 Utils::printSafe($type->name ?? null)
  192.             );
  193.             $standardTypes[$type->name] = $type;
  194.         }
  195.         self::$standardTypes $standardTypes;
  196.     }
  197.     /**
  198.      * @param Type $type
  199.      *
  200.      * @return bool
  201.      *
  202.      * @api
  203.      */
  204.     public static function isInputType($type)
  205.     {
  206.         return $type instanceof InputType &&
  207.             (
  208.                 ! $type instanceof WrappingType ||
  209.                 self::getNamedType($type) instanceof InputType
  210.             );
  211.     }
  212.     /**
  213.      * @param Type $type
  214.      *
  215.      * @return ObjectType|InterfaceType|UnionType|ScalarType|InputObjectType|EnumType
  216.      *
  217.      * @api
  218.      */
  219.     public static function getNamedType($type)
  220.     {
  221.         if ($type === null) {
  222.             return null;
  223.         }
  224.         while ($type instanceof WrappingType) {
  225.             $type $type->getWrappedType();
  226.         }
  227.         return $type;
  228.     }
  229.     /**
  230.      * @param Type $type
  231.      *
  232.      * @return bool
  233.      *
  234.      * @api
  235.      */
  236.     public static function isOutputType($type)
  237.     {
  238.         return $type instanceof OutputType &&
  239.             (
  240.                 ! $type instanceof WrappingType ||
  241.                 self::getNamedType($type) instanceof OutputType
  242.             );
  243.     }
  244.     /**
  245.      * @param Type $type
  246.      *
  247.      * @return bool
  248.      *
  249.      * @api
  250.      */
  251.     public static function isLeafType($type)
  252.     {
  253.         return $type instanceof LeafType;
  254.     }
  255.     /**
  256.      * @param Type $type
  257.      *
  258.      * @return bool
  259.      *
  260.      * @api
  261.      */
  262.     public static function isCompositeType($type)
  263.     {
  264.         return $type instanceof CompositeType;
  265.     }
  266.     /**
  267.      * @param Type $type
  268.      *
  269.      * @return bool
  270.      *
  271.      * @api
  272.      */
  273.     public static function isAbstractType($type)
  274.     {
  275.         return $type instanceof AbstractType;
  276.     }
  277.     /**
  278.      * @param mixed $type
  279.      *
  280.      * @return mixed
  281.      */
  282.     public static function assertType($type)
  283.     {
  284.         Utils::invariant(
  285.             self::isType($type),
  286.             'Expected ' Utils::printSafe($type) . ' to be a GraphQL type.'
  287.         );
  288.         return $type;
  289.     }
  290.     /**
  291.      * @param Type $type
  292.      *
  293.      * @return bool
  294.      *
  295.      * @api
  296.      */
  297.     public static function isType($type)
  298.     {
  299.         return $type instanceof Type;
  300.     }
  301.     /**
  302.      * @param Type $type
  303.      *
  304.      * @return NullableType
  305.      *
  306.      * @api
  307.      */
  308.     public static function getNullableType($type)
  309.     {
  310.         return $type instanceof NonNull $type->getWrappedType() : $type;
  311.     }
  312.     /**
  313.      * @throws InvariantViolation
  314.      */
  315.     public function assertValid()
  316.     {
  317.         Utils::assertValidName($this->name);
  318.     }
  319.     /**
  320.      * @return string
  321.      */
  322.     public function jsonSerialize()
  323.     {
  324.         return $this->toString();
  325.     }
  326.     /**
  327.      * @return string
  328.      */
  329.     public function toString()
  330.     {
  331.         return $this->name;
  332.     }
  333.     /**
  334.      * @return string
  335.      */
  336.     public function __toString()
  337.     {
  338.         try {
  339.             return $this->toString();
  340.         } catch (Exception $e) {
  341.             echo $e;
  342.         } catch (Throwable $e) {
  343.             echo $e;
  344.         }
  345.     }
  346.     /**
  347.      * @return string|null
  348.      */
  349.     protected function tryInferName()
  350.     {
  351.         if ($this->name) {
  352.             return $this->name;
  353.         }
  354.         // If class is extended - infer name from className
  355.         // QueryType -> Type
  356.         // SomeOtherType -> SomeOther
  357.         $tmp  = new ReflectionClass($this);
  358.         $name $tmp->getShortName();
  359.         if ($tmp->getNamespaceName() !== __NAMESPACE__) {
  360.             return preg_replace('~Type$~'''$name);
  361.         }
  362.         return null;
  363.     }
  364. }