vendor/doctrine/orm/src/Query/Expr/OrderBy.php line 15

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM\Query\Expr;
  4. use function count;
  5. use function implode;
  6. /**
  7. * Expression class for building DQL Order By parts.
  8. *
  9. * @link www.doctrine-project.org
  10. */
  11. class OrderBy
  12. {
  13. /** @var string */
  14. protected $preSeparator = '';
  15. /** @var string */
  16. protected $separator = ', ';
  17. /** @var string */
  18. protected $postSeparator = '';
  19. /** @var string[] */
  20. protected $allowedClasses = [];
  21. /** @psalm-var list<string> */
  22. protected $parts = [];
  23. /**
  24. * @param string|null $sort
  25. * @param string|null $order
  26. */
  27. public function __construct($sort = null, $order = null)
  28. {
  29. if ($sort) {
  30. $this->add($sort, $order);
  31. }
  32. }
  33. /**
  34. * @param string $sort
  35. * @param string|null $order
  36. *
  37. * @return void
  38. */
  39. public function add($sort, $order = null)
  40. {
  41. $order = ! $order ? 'ASC' : $order;
  42. $this->parts[] = $sort . ' ' . $order;
  43. }
  44. /**
  45. * @return int
  46. * @psalm-return 0|positive-int
  47. */
  48. public function count()
  49. {
  50. return count($this->parts);
  51. }
  52. /** @psalm-return list<string> */
  53. public function getParts()
  54. {
  55. return $this->parts;
  56. }
  57. /** @return string */
  58. public function __toString()
  59. {
  60. return $this->preSeparator . implode($this->separator, $this->parts) . $this->postSeparator;
  61. }
  62. }