PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Module/System/src/Cmf/System/Sort.php

https://github.com/itcreator/custom-cmf
PHP | 114 lines | 53 code | 17 blank | 44 comment | 3 complexity | a133dfe9f9975fa4aee1e9b60518436c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the Custom CMF.
  4. *
  5. * @link https://github.com/itcreator/custom-cmf for the canonical source repository
  6. * @copyright Copyright (c) 2011 Vital Leshchyk <vitalleshchyk@gmail.com>
  7. * @license https://github.com/itcreator/custom-cmf/blob/master/LICENSE
  8. */
  9. namespace Cmf\System;
  10. /**
  11. * @author Vital Leshchyk <vitalleshchyk@gmail.com>
  12. */
  13. class Sort
  14. {
  15. const DIRECTION_ASC = 'ASC';
  16. const DIRECTION_DESC = 'DESC';
  17. /** @var string|null */
  18. protected $field;
  19. /** @var string|null */
  20. protected $direction;
  21. /** @var string */
  22. protected $requestVariable = 'sort';
  23. /**
  24. * @param string $requestVariable
  25. */
  26. public function __construct($requestVariable = 'sort')
  27. {
  28. $this->requestVariable = $requestVariable;
  29. }
  30. /**
  31. * @param string $field
  32. * @return Sort
  33. */
  34. public function setField($field)
  35. {
  36. $this->field = $field;
  37. return $this;
  38. }
  39. /**
  40. * @return null|string
  41. */
  42. public function getField()
  43. {
  44. return $this->field;
  45. }
  46. /**
  47. * @return null|string
  48. */
  49. public function getDirection()
  50. {
  51. return $this->direction;
  52. }
  53. /**
  54. * @param $direction
  55. * @return Sort
  56. */
  57. public function setDirection($direction)
  58. {
  59. $direction = mb_strtoupper($direction);
  60. $this->direction = $direction == self::DIRECTION_DESC ? self::DIRECTION_DESC : self::DIRECTION_ASC;
  61. return $this;
  62. }
  63. /**
  64. * @return Sort
  65. */
  66. public function setFromRequest()
  67. {
  68. $sort = Application::getRequest()->get($this->requestVariable);
  69. $this
  70. ->setField(isset($sort['field']) ? $sort['field'] : null)
  71. ->setDirection(isset($sort['direction']) ? $sort['direction'] : null);
  72. return $this;
  73. }
  74. /**
  75. * @param $field
  76. * @return bool
  77. */
  78. public function isSortField($field)
  79. {
  80. return mb_strtolower($field) == mb_strtolower($this->field);
  81. }
  82. /**
  83. * @param string $direction
  84. * @return bool
  85. */
  86. public function isDirection($direction)
  87. {
  88. return mb_strtolower($direction) == mb_strtolower($this->direction);
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getRequestVariable()
  94. {
  95. return $this->requestVariable;
  96. }
  97. }