Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
JoinProcessor.php
Go to the documentation of this file.
1 <?php
7 
12 
14 {
18  private $joins;
19 
23  private $fieldMapping;
24 
28  private $appliedFields = [];
29 
34  public function __construct(
35  array $customJoins = [],
36  array $fieldMapping = []
37  ) {
38  $this->joins = $customJoins;
39  $this->fieldMapping = $fieldMapping;
40  }
41 
50  {
51  if ($searchCriteria->getFilterGroups()) {
52  //Process filters
53  foreach ($searchCriteria->getFilterGroups() as $group) {
54  foreach ($group->getFilters() as $filter) {
55  if (!isset($this->appliedFields[$filter->getField()])) {
56  $this->applyCustomJoin($filter->getField(), $collection);
57  $this->appliedFields[$filter->getField()] = true;
58  }
59  }
60  }
61  }
62 
63  if ($searchCriteria->getSortOrders()) {
64  // Process Sortings
65  foreach ($searchCriteria->getSortOrders() as $order) {
66  if (!isset($this->appliedFields[$order->getField()])) {
67  $this->applyCustomJoin($order->getField(), $collection);
68  $this->appliedFields[$order->getField()] = true;
69  }
70  }
71  }
72  }
73 
81  private function applyCustomJoin($field, AbstractDb $collection)
82  {
83  $field = $this->getFieldMapping($field);
84  $customJoin = $this->getCustomJoin($field);
85 
86  if ($customJoin) {
87  $customJoin->apply($collection);
88  }
89  }
90 
98  private function getCustomJoin($field)
99  {
100  $filter = null;
101  if (isset($this->joins[$field])) {
102  $filter = $this->joins[$field];
103  if (!($this->joins[$field] instanceof CustomJoinInterface)) {
104  throw new \InvalidArgumentException(
105  sprintf(
106  'Custom join for %s must implement %s interface.',
107  $field,
108  CustomJoinInterface::class
109  )
110  );
111  }
112  }
113  return $filter;
114  }
115 
122  private function getFieldMapping($field)
123  {
124  return $this->fieldMapping[$field] ?? $field;
125  }
126 }
$order
Definition: order.php:55
$group
Definition: sections.phtml:16
$searchCriteria
__construct(array $customJoins=[], array $fieldMapping=[])
process(SearchCriteriaInterface $searchCriteria, AbstractDb $collection)