Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCriteria.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
18  protected $data = [
19  self::PART_FIELDS => ['list' => []],
20  self::PART_FILTERS => ['list' => []],
21  self::PART_ORDERS => ['list' => []],
22  self::PART_CRITERIA_LIST => ['list' => []],
23  ];
24 
29 
36  public function getMapperInterfaceName()
37  {
38  if (!$this->mapperInterfaceName) {
39  throw new \Exception(
40  (string)new \Magento\Framework\Phrase(
41  'Missed Mapper Interface for Criteria Interface: %1',
42  [get_class($this)]
43  )
44  );
45  }
47  }
48 
56  public function addField($field, $alias = null)
57  {
58  if ($field === '*') {
59  $this->data[self::PART_FIELDS]['list'] = [$field];
60  } else {
61  if (is_array($field)) {
62  foreach ($field as $key => $value) {
63  $this->addField($value, is_string($key) ? $key : null);
64  }
65  } else {
66  if ($alias === null) {
67  $this->data[self::PART_FIELDS]['list'][$field] = $field;
68  } else {
69  $this->data[self::PART_FIELDS]['list'][$alias] = $field;
70  }
71  }
72  }
73  }
74 
116  public function addFilter($name, $field, $condition = null, $type = 'and')
117  {
118  if (isset($this->data[self::PART_FILTERS]['list'][$name])) {
119  throw new \Exception(
120  (string)new \Magento\Framework\Phrase(
121  'Filter already exists in Criteria object: %1',
122  [$name]
123  )
124  );
125  }
126  $filter = new DataObject();
127  // implements ArrayAccess
128  $filter['name'] = $name;
129  $filter['field'] = $field;
130  $filter['condition'] = $condition;
131  $filter['type'] = strtolower($type);
132  $this->data[self::PART_FILTERS]['list'][$name] = $filter;
133  }
134 
143  public function addOrder($field, $direction = self::SORT_ORDER_DESC, $unShift = false)
144  {
145  $direction = strtoupper($direction) == self::SORT_ORDER_ASC ? self::SORT_ORDER_ASC : self::SORT_ORDER_DESC;
146  unset($this->data[self::PART_ORDERS]['list'][$field]);
147  // avoid ordering by the same field twice
148  if ($unShift) {
149  $orders = [$field => $direction];
150  foreach ($this->data[self::PART_ORDERS]['list'] as $key => $dir) {
151  $orders[$key] = $dir;
152  }
153  $this->data[self::PART_ORDERS]['list'] = $orders;
154  } else {
155  $this->data[self::PART_ORDERS]['list'][$field] = $direction;
156  }
157  }
158 
166  public function setLimit($offset, $size)
167  {
168  $this->data[self::PART_LIMIT] = [$offset, $size];
169  }
170 
178  public function removeField($field, $isAlias = false)
179  {
180  if ($isAlias) {
181  if (isset($this->data[self::PART_FIELDS]['list'][$field])) {
182  unset($this->data[self::PART_FIELDS]['list'][$field]);
183  }
184  } else {
185  foreach ($this->data[self::PART_FIELDS]['list'] as $key => $value) {
186  if ($value === $field) {
187  unset($this->data[self::PART_FIELDS]['list'][$key]);
188  break;
189  }
190  }
191  }
192  }
193 
199  public function removeAllFields()
200  {
201  $this->data[self::PART_FIELDS]['list'] = [];
202  }
203 
210  public function removeFilter($name)
211  {
212  if (isset($this->data[self::PART_FILTERS]['list'][$name])) {
213  unset($this->data[self::PART_FILTERS]['list'][$name]);
214  }
215  }
216 
222  public function removeAllFilters()
223  {
224  $this->data[self::PART_FILTERS]['list'] = [];
225  }
226 
232  public function getCriteriaList()
233  {
234  return $this->data[self::PART_CRITERIA_LIST]['list'];
235  }
236 
242  public function getFilters()
243  {
244  return $this->data[self::PART_FILTERS]['list'];
245  }
246 
252  public function getOrders()
253  {
254  return $this->data[self::PART_ORDERS]['list'];
255  }
256 
263  public function getLimit()
264  {
265  return $this->data[self::PART_LIMIT];
266  }
267 
275  public function getPart($name, $default = null)
276  {
277  return $this->data[$name] ?? $default;
278  }
279 
285  public function toArray()
286  {
287  return $this->data;
288  }
289 
295  public function reset()
296  {
297  $this->data = [
298  self::PART_FIELDS => ['list' => []],
299  self::PART_FILTERS => ['list' => []],
300  self::PART_ORDERS => ['list' => []],
301  self::PART_CRITERIA_LIST => ['list' => []],
302  ];
303  }
304 }
addOrder($field, $direction=self::SORT_ORDER_DESC, $unShift=false)
addFilter($name, $field, $condition=null, $type='and')
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
if(!trim($html)) $alias
Definition: details.phtml:20
if(!isset($_GET['name'])) $name
Definition: log.php:14