Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Fields.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
22 
26 class Fields implements FormatterInterface
27 {
31  private $objectManager;
32 
36  private $outputMapper;
37 
41  private $inputMapper;
42 
46  private $typeFactory;
47 
51  private $scalarTypes;
52 
56  private $wrappedTypeProcessor;
57 
61  private $resolveInfoFactory;
62 
72  public function __construct(
73  ObjectManagerInterface $objectManager,
74  OutputMapper $outputMapper,
75  InputMapper $inputMapper,
76  TypeFactory $typeFactory,
77  ScalarTypes $scalarTypes,
78  WrappedTypeProcessor $wrappedTypeProcessor,
79  ResolveInfoFactory $resolveInfoFactory
80  ) {
81  $this->objectManager = $objectManager;
82  $this->outputMapper = $outputMapper;
83  $this->inputMapper = $inputMapper;
84  $this->typeFactory = $typeFactory;
85  $this->scalarTypes = $scalarTypes;
86  $this->wrappedTypeProcessor = $wrappedTypeProcessor;
87  $this->resolveInfoFactory = $resolveInfoFactory;
88  }
89 
93  public function format(TypeInterface $configElement, OutputTypeInterface $outputType): array
94  {
95  $typeConfig = [
96  'fields' => function () use ($configElement, $outputType) {
97  $fieldsConfig = [];
98  foreach ($configElement->getFields() as $field) {
99  $fieldsConfig[$field->getName()] = $this->getFieldConfig($configElement, $outputType, $field);
100  }
101  return $fieldsConfig;
102  }
103  ];
104  return $typeConfig;
105  }
106 
115  private function getFieldType(TypeInterface $typeConfigElement, OutputTypeInterface $outputType, Field $field)
116  {
117  if ($this->scalarTypes->isScalarType($field->getTypeName())) {
118  $type = $this->wrappedTypeProcessor->processScalarWrappedType($field);
119  } else {
120  if ($typeConfigElement->getName() == $field->getTypeName()) {
121  $type = $outputType;
122  } else {
123  $type = $this->outputMapper->getOutputType($field->getTypeName());
124  }
125 
126  $type = $this->wrappedTypeProcessor->processWrappedType($field, $type);
127  }
128  return $type;
129  }
130 
139  private function getFieldConfig(
140  TypeInterface $typeConfigElement,
141  OutputTypeInterface $outputType,
142  Field $field
143  ): array {
144  $type = $this->getFieldType($typeConfigElement, $outputType, $field);
145  $fieldConfig = [
146  'name' => $field->getName(),
147  'type' => $type,
148  ];
149 
150  if (!empty($field->getDescription())) {
151  $fieldConfig['description'] = $field->getDescription();
152  }
153 
154  if ($field->getResolver() != null) {
156  $resolver = $this->objectManager->get($field->getResolver());
157 
158  $fieldConfig['resolve'] =
159  function ($value, $args, $context, $info) use ($resolver, $field) {
160  $wrapperInfo = $this->resolveInfoFactory->create($info);
161  return $resolver->resolve($field, $context, $wrapperInfo, $value, $args);
162  };
163  }
164  return $this->formatArguments($field, $fieldConfig);
165  }
166 
174  private function formatArguments(Field $field, array $config) : array
175  {
176  foreach ($field->getArguments() as $argument) {
177  $inputType = $this->inputMapper->getRepresentation($argument);
178 
179  $config['args'][$argument->getName()] = $inputType;
180  }
181 
182  return $config;
183  }
184 }
__construct(ObjectManagerInterface $objectManager, OutputMapper $outputMapper, InputMapper $inputMapper, TypeFactory $typeFactory, ScalarTypes $scalarTypes, WrappedTypeProcessor $wrappedTypeProcessor, ResolveInfoFactory $resolveInfoFactory)
Definition: Fields.php:72
$objectManager
Definition: bootstrap.php:17
$config
Definition: fraud_order.php:17
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
format(TypeInterface $configElement, OutputTypeInterface $outputType)
Definition: Fields.php:93
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52