PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/framework/Data/AbstractCriteria.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 304 lines | 140 code | 19 blank | 145 comment | 16 complexity | 81a3a576d04afe0ba28a463114bcbd6e MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data;
  7. use Magento\Framework\DataObject;
  8. /**
  9. * Class AbstractCriteria
  10. */
  11. abstract class AbstractCriteria implements \Magento\Framework\Api\CriteriaInterface
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $data = [
  17. self::PART_FIELDS => ['list' => []],
  18. self::PART_FILTERS => ['list' => []],
  19. self::PART_ORDERS => ['list' => []],
  20. self::PART_CRITERIA_LIST => ['list' => []],
  21. ];
  22. /**
  23. * @var string
  24. */
  25. protected $mapperInterfaceName;
  26. /**
  27. * Get associated Mapper Interface name
  28. *
  29. * @throws \Exception
  30. * @return string
  31. */
  32. public function getMapperInterfaceName()
  33. {
  34. if (!$this->mapperInterfaceName) {
  35. throw new \Exception(
  36. (string)new \Magento\Framework\Phrase(
  37. 'Missed Mapper Interface for Criteria Interface: %1',
  38. [get_class($this)]
  39. )
  40. );
  41. }
  42. return $this->mapperInterfaceName;
  43. }
  44. /**
  45. * Add field to select
  46. *
  47. * @param string|array $field
  48. * @param string|null $alias
  49. * @return void
  50. */
  51. public function addField($field, $alias = null)
  52. {
  53. if ($field === '*') {
  54. $this->data[self::PART_FIELDS]['list'] = [$field];
  55. } else {
  56. if (is_array($field)) {
  57. foreach ($field as $key => $value) {
  58. $this->addField($value, is_string($key) ? $key : null);
  59. }
  60. } else {
  61. if ($alias === null) {
  62. $this->data[self::PART_FIELDS]['list'][$field] = $field;
  63. } else {
  64. $this->data[self::PART_FIELDS]['list'][$alias] = $field;
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * Add field filter to collection
  71. *
  72. * If $condition integer or string - exact value will be filtered ('eq' condition)
  73. *
  74. * If $condition is array - one of the following structures is expected:
  75. * <pre>
  76. * - ["from" => $fromValue, "to" => $toValue]
  77. * - ["eq" => $equalValue]
  78. * - ["neq" => $notEqualValue]
  79. * - ["like" => $likeValue]
  80. * - ["in" => [$inValues]]
  81. * - ["nin" => [$notInValues]]
  82. * - ["notnull" => $valueIsNotNull]
  83. * - ["null" => $valueIsNull]
  84. * - ["moreq" => $moreOrEqualValue]
  85. * - ["gt" => $greaterValue]
  86. * - ["lt" => $lessValue]
  87. * - ["gteq" => $greaterOrEqualValue]
  88. * - ["lteq" => $lessOrEqualValue]
  89. * - ["finset" => $valueInSet]
  90. * </pre>
  91. *
  92. * If non matched - sequential parallel arrays are expected and OR conditions
  93. * will be built using above mentioned structure.
  94. *
  95. * Example:
  96. * <pre>
  97. * $field = ['age', 'name'];
  98. * $condition = [42, ['like' => 'Mage']];
  99. * $type = 'or';
  100. * </pre>
  101. * The above would find where age equal to 42 OR name like %Mage%.
  102. *
  103. * @param string $name
  104. * @param string|array $field
  105. * @param string|int|array $condition
  106. * @param string $type
  107. * @throws \Exception
  108. * @return void
  109. */
  110. public function addFilter($name, $field, $condition = null, $type = 'and')
  111. {
  112. if (isset($this->data[self::PART_FILTERS]['list'][$name])) {
  113. throw new \Exception(
  114. (string)new \Magento\Framework\Phrase(
  115. 'Filter already exists in Criteria object: %1',
  116. [$name]
  117. )
  118. );
  119. }
  120. $filter = new DataObject();
  121. // implements ArrayAccess
  122. $filter['name'] = $name;
  123. $filter['field'] = $field;
  124. $filter['condition'] = $condition;
  125. $filter['type'] = strtolower($type);
  126. $this->data[self::PART_FILTERS]['list'][$name] = $filter;
  127. }
  128. /**
  129. * self::setOrder() alias
  130. *
  131. * @param string $field
  132. * @param string $direction
  133. * @param bool $unShift
  134. * @return void
  135. */
  136. public function addOrder($field, $direction = self::SORT_ORDER_DESC, $unShift = false)
  137. {
  138. $direction = strtoupper($direction) == self::SORT_ORDER_ASC ? self::SORT_ORDER_ASC : self::SORT_ORDER_DESC;
  139. unset($this->data[self::PART_ORDERS]['list'][$field]);
  140. // avoid ordering by the same field twice
  141. if ($unShift) {
  142. $orders = [$field => $direction];
  143. foreach ($this->data[self::PART_ORDERS]['list'] as $key => $dir) {
  144. $orders[$key] = $dir;
  145. }
  146. $this->data[self::PART_ORDERS]['list'] = $orders;
  147. } else {
  148. $this->data[self::PART_ORDERS]['list'][$field] = $direction;
  149. }
  150. }
  151. /**
  152. * Set Query limit
  153. *
  154. * @param int $offset
  155. * @param int $size
  156. * @return void
  157. */
  158. public function setLimit($offset, $size)
  159. {
  160. $this->data[self::PART_LIMIT] = [$offset, $size];
  161. }
  162. /**
  163. * Removes field from select
  164. *
  165. * @param string|null $field
  166. * @param bool $isAlias Alias identifier
  167. * @return void
  168. */
  169. public function removeField($field, $isAlias = false)
  170. {
  171. if ($isAlias) {
  172. if (isset($this->data[self::PART_FIELDS]['list'][$field])) {
  173. unset($this->data[self::PART_FIELDS]['list'][$field]);
  174. }
  175. } else {
  176. foreach ($this->data[self::PART_FIELDS]['list'] as $key => $value) {
  177. if ($value === $field) {
  178. unset($this->data[self::PART_FIELDS]['list'][$key]);
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. /**
  185. * Removes all fields from select
  186. *
  187. * @return void
  188. */
  189. public function removeAllFields()
  190. {
  191. $this->data[self::PART_FIELDS]['list'] = [];
  192. }
  193. /**
  194. * Removes filter by name
  195. *
  196. * @param string $name
  197. * @return void
  198. */
  199. public function removeFilter($name)
  200. {
  201. if (isset($this->data[self::PART_FILTERS]['list'][$name])) {
  202. unset($this->data[self::PART_FILTERS]['list'][$name]);
  203. }
  204. }
  205. /**
  206. * Removes all filters
  207. *
  208. * @return void
  209. */
  210. public function removeAllFilters()
  211. {
  212. $this->data[self::PART_FILTERS]['list'] = [];
  213. }
  214. /**
  215. * Get Criteria objects added to current Composite Criteria
  216. *
  217. * @return array
  218. */
  219. public function getCriteriaList()
  220. {
  221. return $this->data[self::PART_CRITERIA_LIST]['list'];
  222. }
  223. /**
  224. * Get list of filters
  225. *
  226. * @return array
  227. */
  228. public function getFilters()
  229. {
  230. return $this->data[self::PART_FILTERS]['list'];
  231. }
  232. /**
  233. * Get ordering criteria
  234. *
  235. * @return array
  236. */
  237. public function getOrders()
  238. {
  239. return $this->data[self::PART_ORDERS]['list'];
  240. }
  241. /**
  242. * Get limit
  243. * (['offset', 'page'])
  244. *
  245. * @return array
  246. */
  247. public function getLimit()
  248. {
  249. return $this->data[self::PART_LIMIT];
  250. }
  251. /**
  252. * Retrieve criteria part
  253. *
  254. * @param string $name
  255. * @param mixed $default
  256. * @return mixed
  257. */
  258. public function getPart($name, $default = null)
  259. {
  260. return isset($this->data[$name]) ? $this->data[$name] : $default;
  261. }
  262. /**
  263. * Return all criteria parts as array
  264. *
  265. * @return array
  266. */
  267. public function toArray()
  268. {
  269. return $this->data;
  270. }
  271. /**
  272. * Reset criteria
  273. *
  274. * @return void
  275. */
  276. public function reset()
  277. {
  278. $this->data = [
  279. self::PART_FIELDS => ['list' => []],
  280. self::PART_FILTERS => ['list' => []],
  281. self::PART_ORDERS => ['list' => []],
  282. self::PART_CRITERIA_LIST => ['list' => []],
  283. ];
  284. }
  285. }