vendor/webonyx/graphql-php/src/Type/Definition/FieldArgument.php line 84

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace GraphQL\Type\Definition;
  4. use GraphQL\Error\InvariantViolation;
  5. use GraphQL\Language\AST\InputValueDefinitionNode;
  6. use GraphQL\Type\Schema;
  7. use GraphQL\Utils\Utils;
  8. use function array_key_exists;
  9. use function is_array;
  10. use function is_string;
  11. use function sprintf;
  12. class FieldArgument
  13. {
  14. /** @var string */
  15. public $name;
  16. /** @var mixed */
  17. public $defaultValue;
  18. /** @var string|null */
  19. public $description;
  20. /** @var InputValueDefinitionNode|null */
  21. public $astNode;
  22. /** @var mixed[] */
  23. public $config;
  24. /** @var Type&InputType */
  25. private $type;
  26. /** @param mixed[] $def */
  27. public function __construct(array $def)
  28. {
  29. foreach ($def as $key => $value) {
  30. switch ($key) {
  31. case 'name':
  32. $this->name = $value;
  33. break;
  34. case 'defaultValue':
  35. $this->defaultValue = $value;
  36. break;
  37. case 'description':
  38. $this->description = $value;
  39. break;
  40. case 'astNode':
  41. $this->astNode = $value;
  42. break;
  43. }
  44. }
  45. $this->config = $def;
  46. }
  47. /**
  48. * @param mixed[] $config
  49. *
  50. * @return FieldArgument[]
  51. */
  52. public static function createMap(array $config) : array
  53. {
  54. $map = [];
  55. foreach ($config as $name => $argConfig) {
  56. if (! is_array($argConfig)) {
  57. $argConfig = ['type' => $argConfig];
  58. }
  59. $map[] = new self($argConfig + ['name' => $name]);
  60. }
  61. return $map;
  62. }
  63. public function getType() : Type
  64. {
  65. if (! isset($this->type)) {
  66. /**
  67. * TODO: replace this phpstan cast with native assert
  68. *
  69. * @var Type&InputType
  70. */
  71. $type = Schema::resolveType($this->config['type']);
  72. $this->type = $type;
  73. }
  74. return $this->type;
  75. }
  76. public function defaultValueExists() : bool
  77. {
  78. return array_key_exists('defaultValue', $this->config);
  79. }
  80. public function isRequired() : bool
  81. {
  82. return $this->getType() instanceof NonNull && ! $this->defaultValueExists();
  83. }
  84. public function assertValid(FieldDefinition $parentField, Type $parentType)
  85. {
  86. try {
  87. Utils::assertValidName($this->name);
  88. } catch (InvariantViolation $e) {
  89. throw new InvariantViolation(
  90. sprintf('%s.%s(%s:) %s', $parentType->name, $parentField->name, $this->name, $e->getMessage())
  91. );
  92. }
  93. $type = $this->getType();
  94. if ($type instanceof WrappingType) {
  95. $type = $type->getWrappedType(true);
  96. }
  97. Utils::invariant(
  98. $type instanceof InputType,
  99. sprintf(
  100. '%s.%s(%s): argument type must be Input Type but got: %s',
  101. $parentType->name,
  102. $parentField->name,
  103. $this->name,
  104. Utils::printSafe($this->type)
  105. )
  106. );
  107. Utils::invariant(
  108. $this->description === null || is_string($this->description),
  109. sprintf(
  110. '%s.%s(%s): argument description type must be string but got: %s',
  111. $parentType->name,
  112. $parentField->name,
  113. $this->name,
  114. Utils::printSafe($this->description)
  115. )
  116. );
  117. }
  118. }