Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WrappedTypeProcessor.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
19 {
23  private $scalarTypes;
24 
28  private $typeFactory;
29 
34  public function __construct(TypeFactory $typeFactory, ScalarTypes $scalarTypes)
35  {
36  $this->typeFactory = $typeFactory;
37  $this->scalarTypes = $scalarTypes;
38  }
39 
49  public function processWrappedType(FieldInterface $field, TypeInterface $object = null) : TypeInterface
50  {
51  return $this->processIsNullable($field, $this->processIsList($field, $object));
52  }
53 
63  public function processScalarWrappedType(
64  FieldInterface $field,
65  TypeInterface $object = null
66  ) : \GraphQL\Type\Definition\Type {
67  if (!$object) {
68  $object = $this->scalarTypes->getScalarTypeInstance($field->getTypeName());
69  }
70  return $this->processScalarIsNullable($field, $this->processScalarIsList($field, $object));
71  }
72 
80  private function processIsNullable(FieldInterface $field, TypeInterface $object = null) : TypeInterface
81  {
82  if ($field->isRequired()) {
83  return $this->typeFactory->createNonNull($object);
84  }
85  return $object;
86  }
87 
95  private function processIsList(FieldInterface $field, TypeInterface $object = null) : TypeInterface
96  {
97  if ($field->isList()) {
98  if ($field instanceof \Magento\Framework\GraphQl\Config\Element\Argument) {
99  if ($field->areItemsRequired()) {
100  $object = $this->typeFactory->createNonNull($object);
101  }
102  }
103  return $this->typeFactory->createList($object);
104  }
105  return $object;
106  }
107 
115  private function processScalarIsNullable(
116  FieldInterface $field,
117  \GraphQL\Type\Definition\Type $object = null
118  ) : \GraphQL\Type\Definition\Type {
119  $object = $object ?: $this->scalarTypes->getScalarTypeInstance($field->getTypeName());
120  if ($field->isRequired()) {
121  return $this->scalarTypes->createNonNull($object);
122  }
123  return $object;
124  }
125 
133  private function processScalarIsList(
134  FieldInterface $field,
135  \GraphQL\Type\Definition\Type $object = null
136  ) : \GraphQL\Type\Definition\Type {
137  $object = $object ?: $this->scalarTypes->getScalarTypeInstance($field->getTypeName());
138  if ($field->isList()) {
139  if ($field instanceof \Magento\Framework\GraphQl\Config\Element\Argument) {
140  if ($field->areItemsRequired()) {
141  $object = $this->scalarTypes->createNonNull($object);
142  }
143  }
144  return $this->scalarTypes->createList($object);
145  }
146  return $object;
147  }
148 }
processScalarWrappedType(FieldInterface $field, TypeInterface $object=null)
__construct(TypeFactory $typeFactory, ScalarTypes $scalarTypes)
processWrappedType(FieldInterface $field, TypeInterface $object=null)