SortOrder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Api;
  7. use Magento\Framework\Exception\InputException;
  8. use Magento\Framework\Phrase;
  9. /**
  10. * Data object for sort order.
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class SortOrder extends AbstractSimpleObject
  16. {
  17. const FIELD = 'field';
  18. const DIRECTION = 'direction';
  19. const SORT_ASC = 'ASC';
  20. const SORT_DESC = 'DESC';
  21. /**
  22. * Initialize object and validate sort direction
  23. *
  24. * @param array $data
  25. * @throws InputException
  26. */
  27. public function __construct(array $data = [])
  28. {
  29. parent::__construct($data);
  30. if (null !== $this->getDirection()) {
  31. $this->validateDirection($this->getDirection());
  32. }
  33. if ($this->getField() !== null) {
  34. $this->validateField($this->getField());
  35. }
  36. }
  37. /**
  38. * Get sorting field.
  39. *
  40. * @return string
  41. */
  42. public function getField()
  43. {
  44. return $this->_get(SortOrder::FIELD);
  45. }
  46. /**
  47. * Set sorting field.
  48. *
  49. * @param string $field
  50. * @throws InputException
  51. *
  52. * @return $this
  53. */
  54. public function setField($field)
  55. {
  56. $this->validateField($field);
  57. return $this->setData(SortOrder::FIELD, $field);
  58. }
  59. /**
  60. * Get sorting direction.
  61. *
  62. * @return string
  63. */
  64. public function getDirection()
  65. {
  66. return $this->_get(SortOrder::DIRECTION);
  67. }
  68. /**
  69. * Set sorting direction.
  70. *
  71. * @param string $direction
  72. * @throws InputException
  73. *
  74. * @return $this
  75. */
  76. public function setDirection($direction)
  77. {
  78. $this->validateDirection($direction);
  79. return $this->setData(SortOrder::DIRECTION, $this->normalizeDirectionInput($direction));
  80. }
  81. /**
  82. * Validate direction argument ASC or DESC
  83. *
  84. * @param mixed $direction
  85. * @return void
  86. * @throws InputException
  87. */
  88. private function validateDirection($direction): void
  89. {
  90. $this->validateDirectionIsString($direction);
  91. $this->validateDirectionIsAscOrDesc($direction);
  92. }
  93. /**
  94. * @param string $direction
  95. * @throws InputException
  96. * @return void
  97. */
  98. private function validateDirectionIsString($direction): void
  99. {
  100. if (!is_string($direction)) {
  101. throw new InputException(new Phrase(
  102. 'The sort order has to be specified as a string, got %1.',
  103. [gettype($direction)]
  104. ));
  105. }
  106. }
  107. /**
  108. * @param string $direction
  109. * @throws InputException
  110. * @return void
  111. */
  112. private function validateDirectionIsAscOrDesc($direction): void
  113. {
  114. $normalizedDirection = $this->normalizeDirectionInput($direction);
  115. if (!in_array($normalizedDirection, [SortOrder::SORT_ASC, SortOrder::SORT_DESC], true)) {
  116. throw new InputException(new Phrase(
  117. 'The sort order has to be specified as %1 for ascending order or %2 for descending order.',
  118. [SortOrder::SORT_ASC, SortOrder::SORT_DESC]
  119. ));
  120. }
  121. }
  122. /**
  123. * @param string $direction
  124. * @return string
  125. */
  126. private function normalizeDirectionInput($direction)
  127. {
  128. return strtoupper($direction);
  129. }
  130. /**
  131. * Check if given value can be used as sorting field.
  132. *
  133. * @param string $field
  134. * @return void
  135. * @throws InputException
  136. */
  137. private function validateField(string $field): void
  138. {
  139. if (preg_match('/[^a-z0-9\_]/i', $field)) {
  140. throw new InputException(
  141. new Phrase(
  142. 'Sort order field %1 contains restricted symbols',
  143. [$field]
  144. )
  145. );
  146. }
  147. }
  148. }