Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
3
4
7use Bitrix\Report\VisualConstructor\IProvidable;
8
13abstract class Base implements IErrorable
14{
15 private $results = array();
16 private $filters = array();
17 private $errors = array();
18 private $relations = array();
19
23 abstract protected function availableFilterKeys();
24
28 abstract protected function getManagerInstance();
29
33 abstract protected function getEntitiesList();
34
38 abstract protected function getIndices();
39
46 private function isAvailableFilter($filterKey)
47 {
48 $availableFilterKeys = $this->availableFilterKeys();
49
50 return in_array($filterKey, $availableFilterKeys);
51 }
52
58 public function addFilter($key, $value)
59 {
60 if ($this->isAvailableFilter($key))
61 {
62 $this->filters[$key][] = $this->normaliseFilterValue($value);
63 return true;
64 }
65 else
66 {
67 $this->errors[] = new Error('Filter with key:' . $key . ' not available for this provider');
68 return false;
69 }
70 }
71
76 public function addRelation($key)
77 {
78 $this->relations[] = $key;
79 }
80
84 public function getFilters()
85 {
86 return $this->filters;
87 }
88
92 public function getRelations()
93 {
94 return $this->relations;
95 }
96
100 public function execute()
101 {
102 $this->callManager();
103 $indices = $this->getIndices();
104 $filteredEntityIds = $this->getFilteredValues($indices);
105 $entities = $this->getEntitiesList();
106 $result = array();
107 $filters = $this->getFilters();
108 if (!empty($filters))
109 {
110 $result = $this->applyFilters($entities, $filteredEntityIds);
111 }
112 else
113 {
114 foreach ($entities as $key => $entity)
115 {
116 $this->processAvailableRelations($entity);
117 $result[] = $entity;
118 }
119 }
120 $this->sortResults($result);
121 $this->setResults($result);
122
123 return $this;
124 }
125
132 protected function applyFilters($entities, $filteredEntityIds)
133 {
134 $result = [];
135
136 foreach ($entities as $key => $entity)
137 {
138 if (in_array($key, $filteredEntityIds))
139 {
140 $this->processAvailableRelations($entity);
141 $result[] = $entity;
142 }
143 }
144
145 return $result;
146 }
147
148 protected function sortResults(&$result)
149 {
150 // do nothing
151 }
152
157 private function getFilteredValues($indices)
158 {
159 $filteredEntityIds = array();
160 foreach ($this->getFilters() as $filterType => $filterValues)
161 {
162 if ($filterType !== 'primary')
163 {
164 $newFilterEntityIds = array();
165 foreach ($filterValues as $filterKey)
166 {
167 foreach ($filterKey as $filterValue)
168 {
169 if (isset($indices[$filterType][$filterValue]))
170 {
171 $newFilterEntityIds = array_merge($newFilterEntityIds, $indices[$filterType][$filterValue]);
172 }
173 }
174 }
175 if (!empty($filteredEntityIds))
176 {
177 $filteredEntityIds = array_intersect($filteredEntityIds, $newFilterEntityIds);
178 }
179 else
180 {
181 $filteredEntityIds = $newFilterEntityIds;
182 }
183 }
184 else
185 {
186 if (!empty($filteredEntityIds))
187 {
188 $filteredEntityIds = array_intersect($filteredEntityIds, $filterValues[0]);
189 }
190 else
191 {
192 $filteredEntityIds = $filterValues[0];
193 }
194 }
195 }
196 return array_unique($filteredEntityIds);
197 }
198
202 public function getErrors()
203 {
204 return $this->errors;
205 }
206
211 private function normaliseFilterValue($value)
212 {
213 $result = is_array($value) ? array_unique($value) : array($value);
214 return $result;
215 }
216
221 protected function processAvailableRelations($entity)
222 {
223 foreach ($this->getRelations() as $relationName)
224 {
225 $processMethodName = 'processWith' . ucfirst($relationName);
226 call_user_func_array(array($this, $processMethodName), array($entity));
227 }
228 }
229
230
231 protected function callManager()
232 {
233 $this->getManagerInstance()->call();
234 }
235
236
240 public function getFirstResult()
241 {
242 $results = $this->getResults();
243 return array_shift($results);
244 }
245
246
250 public function getResults()
251 {
252 return $this->results;
253 }
254
259 public function setResults($results)
260 {
261 $this->results = $results;
262 }
263
264}
applyFilters($entities, $filteredEntityIds)
Definition base.php:132