Argument.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\GraphQl\Config\Element;
  8. /**
  9. * Class representing 'argument' GraphQL config element.
  10. *
  11. * Arguments of a type in GraphQL are used to gather client input to affect how a query will return data.
  12. */
  13. class Argument implements FieldInterface
  14. {
  15. /**
  16. * @var string
  17. */
  18. private $name;
  19. /**
  20. * @var string
  21. */
  22. private $type;
  23. /**
  24. * @var string
  25. */
  26. private $description;
  27. /**
  28. * @var string
  29. */
  30. private $baseType;
  31. /**
  32. * @var bool
  33. */
  34. private $required;
  35. /**
  36. * @var bool
  37. */
  38. private $isList;
  39. /**
  40. * @var bool
  41. */
  42. private $itemsRequired;
  43. /**
  44. * @var string|null
  45. */
  46. private $defaultValue;
  47. /**
  48. * @param string $name
  49. * @param string $type
  50. * @param string $baseType
  51. * @param string $description
  52. * @param bool $required
  53. * @param bool $isList
  54. * @param string $itemType
  55. * @param bool $itemsRequired
  56. * @param string $defaultValue
  57. */
  58. public function __construct(
  59. string $name,
  60. string $type,
  61. string $baseType,
  62. string $description,
  63. bool $required,
  64. bool $isList,
  65. string $itemType = '',
  66. bool $itemsRequired = false,
  67. string $defaultValue = null
  68. ) {
  69. $this->name = $name;
  70. $this->type = $isList ? $itemType : $type;
  71. $this->baseType = $baseType;
  72. $this->description = $description;
  73. $this->required = $required;
  74. $this->isList = $isList;
  75. $this->itemsRequired = $itemsRequired;
  76. $this->defaultValue = $defaultValue;
  77. }
  78. /**
  79. * Get the name of the argument.
  80. *
  81. * @return string
  82. */
  83. public function getName() : string
  84. {
  85. return $this->name;
  86. }
  87. /**
  88. * Get the argument type's configured name
  89. *
  90. * @return string
  91. */
  92. public function getTypeName() : string
  93. {
  94. return $this->type;
  95. }
  96. /**
  97. * Get the argument's base type. This can be used to inherit fields for a filter or sort input, etc.
  98. *
  99. * @return string
  100. */
  101. public function getBaseType() : string
  102. {
  103. return $this->baseType;
  104. }
  105. /**
  106. * Return true if argument is a list of input items, otherwise false if it is a single object/scalar.
  107. *
  108. * @return bool
  109. */
  110. public function isList() : bool
  111. {
  112. return $this->isList;
  113. }
  114. /**
  115. * Return true if argument is required when invoking the query where the argument is specified. False otherwise.
  116. *
  117. * @return bool
  118. */
  119. public function isRequired() : bool
  120. {
  121. return $this->required;
  122. }
  123. /**
  124. * Return true if item is a list, and if that list must be populated by at least one item. False otherwise.
  125. *
  126. * @return bool
  127. */
  128. public function areItemsRequired() : bool
  129. {
  130. return $this->itemsRequired;
  131. }
  132. /**
  133. * Return the human-readable description of the argument containing it's documentation.
  134. *
  135. * @return string
  136. */
  137. public function getDescription() : string
  138. {
  139. return $this->description;
  140. }
  141. /**
  142. * Return defaultValue if argument is a scalar and has a configured defaultValue. Otherwise return an empty string.
  143. *
  144. * @return string|null
  145. */
  146. public function getDefaultValue() : ?string
  147. {
  148. return $this->defaultValue;
  149. }
  150. /**
  151. * Return true if defaultValue is set, otherwise false
  152. *
  153. * @return bool
  154. */
  155. public function hasDefaultValue() : bool
  156. {
  157. return $this->defaultValue ? true: false;
  158. }
  159. }