Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
AttributeOptions.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Magento\EavGraphQl\Model\Resolver\DataProvider\AttributeOptions as AttributeOptionsDataProvider;
20 
25 {
29  private $attributeOptionsDataProvider;
30 
34  private $valueFactory;
35 
40  public function __construct(
41  AttributeOptionsDataProvider $attributeOptionsDataProvider,
42  ValueFactory $valueFactory
43  ) {
44  $this->attributeOptionsDataProvider = $attributeOptionsDataProvider;
45  $this->valueFactory = $valueFactory;
46  }
47 
51  public function resolve(
52  Field $field,
53  $context,
55  array $value = null,
56  array $args = null
57  ) : Value {
58 
59  return $this->valueFactory->create(function () use ($value) {
60  $entityType = $this->getEntityType($value);
61  $attributeCode = $this->getAttributeCode($value);
62 
63  $optionsData = $this->getAttributeOptionsData($entityType, $attributeCode);
64  return $optionsData;
65  });
66  }
67 
73  private function getEntityType(array $value): int
74  {
75  if (!isset($value['entity_type'])) {
76  throw new GraphQlInputException(__('"Entity type should be specified'));
77  }
78 
79  return (int)$value['entity_type'];
80  }
81 
87  private function getAttributeCode(array $value): string
88  {
89  if (!isset($value['attribute_code'])) {
90  throw new GraphQlInputException(__('"Attribute code should be specified'));
91  }
92 
93  return $value['attribute_code'];
94  }
95 
103  private function getAttributeOptionsData(int $entityType, string $attributeCode): array
104  {
105  try {
106  $optionsData = $this->attributeOptionsDataProvider->getData($entityType, $attributeCode);
107  } catch (InputException $e) {
108  throw new GraphQlInputException(__($e->getMessage()), $e);
109  } catch (StateException $e) {
110  throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
111  }
112  return $optionsData;
113  }
114 }
__()
Definition: __.php:13
resolve(Field $field, $context, ResolveInfo $info, array $value=null, array $args=null)
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
__construct(AttributeOptionsDataProvider $attributeOptionsDataProvider, ValueFactory $valueFactory)