GenericMapper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB;
  7. use Magento\Framework\Api\CriteriaInterface;
  8. /**
  9. * Class GenericMapper
  10. */
  11. class GenericMapper extends AbstractMapper
  12. {
  13. /**
  14. * Set initial conditions
  15. *
  16. * @return void
  17. */
  18. protected function init()
  19. {
  20. //
  21. }
  22. /**
  23. * Map criteria list
  24. *
  25. * @param \Magento\Framework\Api\CriteriaInterface[] $criteriaList
  26. * @return void
  27. */
  28. public function mapCriteriaList(array $criteriaList)
  29. {
  30. foreach ($criteriaList as $criteria) {
  31. /** @var CriteriaInterface $criteria */
  32. $mapper = $criteria->getMapperInterfaceName();
  33. $mapperInstance = $this->mapperFactory->create($mapper, ['select' => $this->select]);
  34. $this->select = $mapperInstance->map($criteria);
  35. }
  36. }
  37. /**
  38. * Map filters
  39. *
  40. * @param array $filters
  41. * @return void
  42. */
  43. public function mapFilters(array $filters)
  44. {
  45. $this->renderFiltersBefore();
  46. foreach ($filters as $filter) {
  47. switch ($filter['type']) {
  48. case 'or':
  49. $condition = $this->getConnection()->quoteInto($filter['field'] . '=?', $filter['condition']);
  50. $this->getSelect()->orWhere($condition);
  51. break;
  52. case 'string':
  53. $this->getSelect()->where($filter['condition']);
  54. break;
  55. case 'public':
  56. $field = $this->getMappedField($filter['field']);
  57. $condition = $filter['condition'];
  58. $this->getSelect()->where($this->getConditionSql($field, $condition), null, Select::TYPE_CONDITION);
  59. break;
  60. default:
  61. $condition = $this->getConnection()->quoteInto($filter['field'] . '=?', $filter['condition']);
  62. $this->getSelect()->where($condition);
  63. }
  64. }
  65. }
  66. /**
  67. * Map order
  68. *
  69. * @param array $orders
  70. * @return void
  71. */
  72. public function mapOrders(array $orders)
  73. {
  74. foreach ($orders as $field => $direction) {
  75. $this->select->order(new \Zend_Db_Expr($field . ' ' . $direction));
  76. }
  77. }
  78. /**
  79. * Map fields
  80. *
  81. * @param array $fields
  82. * @throws \Zend_Db_Select_Exception
  83. * @return void
  84. * @SuppressWarnings(PHPMD.NPathComplexity)
  85. */
  86. public function mapFields(array $fields)
  87. {
  88. $columns = $this->getSelect()->getPart(\Magento\Framework\DB\Select::COLUMNS);
  89. $selectedUniqueNames = [];
  90. foreach ($fields as $fieldInfo) {
  91. if (is_string($fieldInfo)) {
  92. $fieldInfo = isset($this->map[$fieldInfo]) ? $this->map[$fieldInfo] : $fieldInfo;
  93. }
  94. list($correlationName, $field, $alias) = $fieldInfo;
  95. if (!is_string($alias)) {
  96. $alias = null;
  97. }
  98. if ($field instanceof \Zend_Db_Expr) {
  99. $field = $field->__toString();
  100. }
  101. $selectedUniqueName = $alias ?: $field;
  102. if (in_array($selectedUniqueName, $selectedUniqueNames)) {
  103. // ignore field since the alias is already used by another field
  104. continue;
  105. }
  106. $selectedUniqueNames[] = $selectedUniqueName;
  107. $columns[] = [$correlationName, $field, $alias];
  108. }
  109. $this->getSelect()->setPart(\Magento\Framework\DB\Select::COLUMNS, $columns);
  110. }
  111. /**
  112. * Map limit
  113. *
  114. * @param int $offset
  115. * @param int $size
  116. * @return void
  117. */
  118. public function mapLimit($offset, $size)
  119. {
  120. $this->select->limitPage($offset, $size);
  121. }
  122. /**
  123. * Map distinct flag
  124. *
  125. * @param bool $flag
  126. * @return void
  127. */
  128. public function mapDistinct($flag)
  129. {
  130. $this->select->distinct($flag);
  131. }
  132. }