Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MappableConditionsProcessor.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
23 {
27  private $customConditionProvider;
28 
32  private $eavConfig;
33 
38  public function __construct(
39  CustomConditionProviderInterface $customConditionProvider,
40  \Magento\Eav\Model\Config $eavConfig
41  ) {
42  $this->customConditionProvider = $customConditionProvider;
43  $this->eavConfig = $eavConfig;
44  }
45 
50  public function rebuildConditionsTree(CombinedCondition $conditions): CombinedCondition
51  {
52  return $this->rebuildCombinedCondition($conditions);
53  }
54 
60  private function rebuildCombinedCondition(CombinedCondition $originalConditions): CombinedCondition
61  {
62  $validConditions = [];
63  $invalidConditions = [];
64 
65  foreach ($originalConditions->getConditions() as $condition) {
66  if ($condition->getType() === CombinedCondition::class) {
67  $rebuildSubCondition = $this->rebuildCombinedCondition($condition);
68 
69  if (count($rebuildSubCondition->getConditions()) > 0) {
70  $validConditions[] = $rebuildSubCondition;
71  } else {
72  $invalidConditions[] = $rebuildSubCondition;
73  }
74 
75  continue;
76  }
77 
78  if ($condition->getType() === SimpleCondition::class) {
79  if ($this->validateSimpleCondition($condition)) {
80  $validConditions[] = $condition;
81  } else {
82  $invalidConditions[] = $condition;
83  }
84 
85  continue;
86  }
87 
88  throw new InputException(
89  __('Undefined condition type "%1" passed in.', $condition->getType())
90  );
91  }
92 
93  // if resulted condition group has left no mappable conditions - we can remove it at all
94  if (count($invalidConditions) > 0 && strtolower($originalConditions->getAggregator()) === 'any') {
95  $validConditions = [];
96  }
97 
98  $rebuildCondition = clone $originalConditions;
99  $rebuildCondition->setConditions($validConditions);
100 
101  return $rebuildCondition;
102  }
103 
108  private function validateSimpleCondition(SimpleCondition $originalConditions): bool
109  {
110  return $this->canUseFieldForMapping($originalConditions->getAttribute());
111  }
112 
119  private function canUseFieldForMapping(string $fieldName): bool
120  {
121  // We can map field to search criteria if we have custom processor for it
122  if ($this->customConditionProvider->hasProcessorForField($fieldName)) {
123  return true;
124  }
125 
126  // Also we can map field to search criteria if it is an EAV attribute
127  $attribute = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $fieldName);
128 
129  // We have this weird check for getBackendType() to verify that attribute really exists
130  // because due to eavConfig behavior even if pass non existing attribute code we still receive AbstractAttribute
131  // getAttributeId() is not sufficient too because some attributes don't have it - e.g. attribute_set_id
132  if ($attribute && $attribute->getBackendType() !== null) {
133  return true;
134  }
135 
136  // In any other cases we can't map field to search criteria
137  return false;
138  }
139 }
__()
Definition: __.php:13
__construct(CustomConditionProviderInterface $customConditionProvider, \Magento\Eav\Model\Config $eavConfig)