Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NativeAttributeCondition.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
21 {
25  private $resourceConnection;
26 
30  public function __construct(
31  \Magento\Framework\App\ResourceConnection $resourceConnection
32  ) {
33  $this->resourceConnection = $resourceConnection;
34  }
35 
44  public function build(Filter $filter): string
45  {
46  $conditionType = $this->mapConditionType($filter->getConditionType(), $filter->getField());
47  $conditionValue = $this->mapConditionValue($conditionType, $filter->getValue());
48 
49  return $this->resourceConnection
50  ->getConnection()
51  ->prepareSqlCondition(
52  Collection::MAIN_TABLE_ALIAS . '.' . $filter->getField(),
53  [
54  $conditionType => $conditionValue
55  ]
56  );
57  }
58 
66  private function mapConditionType(string $conditionType, string $field): string
67  {
68  if (strtolower($field) === ProductInterface::SKU) {
69  $conditionsMap = [
70  'eq' => 'like',
71  'neq' => 'nlike'
72  ];
73  } else {
74  $conditionsMap = [
75  'eq' => 'in',
76  'neq' => 'nin'
77  ];
78  }
79 
80  return $conditionsMap[$conditionType] ?? $conditionType;
81  }
82 
90  private function mapConditionValue(string $conditionType, string $conditionValue): string
91  {
92  $conditionsMap = ['like', 'nlike'];
93 
94  if (in_array($conditionType, $conditionsMap)) {
95  $conditionValue = '%' . $conditionValue . '%';
96  }
97 
98  return $conditionValue;
99  }
100 }