| Total Complexity | 12 |
| Total Lines | 91 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 4 | class Order extends Dto |
||
| 5 | { |
||
| 6 | const ASC = 'ASC'; |
||
| 7 | const DESC = 'DESC'; |
||
| 8 | /** |
||
| 9 | * Fields to use to order |
||
| 10 | * @var array fields |
||
| 11 | */ |
||
| 12 | protected $fields = array(); |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Add new order to dto |
||
| 16 | * |
||
| 17 | * @param string $field |
||
| 18 | * @param string $direction |
||
| 19 | */ |
||
| 20 | public function addOrder($field, $direction = self::ASC) |
||
| 21 | { |
||
| 22 | $this->fields[$field] = self::parseDirection($direction); |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Remove existing order |
||
| 27 | * |
||
| 28 | * @param string $fieldToRemove |
||
| 29 | */ |
||
| 30 | public function removeOrder($fieldToRemove) |
||
| 31 | { |
||
| 32 | $order = []; |
||
| 33 | if (count($this->fields) > 0) { |
||
| 34 | foreach ($this->getOrders() as $field => $direction) { |
||
| 35 | if (strtolower($fieldToRemove) === strtolower($field)) { |
||
| 36 | continue; |
||
| 37 | } |
||
| 38 | $order[$field] = $direction; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | $this->fields = $order; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Set an order field |
||
| 46 | * |
||
| 47 | * @param string $field |
||
| 48 | * @param string $direction |
||
| 49 | */ |
||
| 50 | public function setOrder($field, $direction = self::ASC) |
||
| 51 | { |
||
| 52 | $this->fields = [$field => self::parseDirection($direction)]; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Parse direction string |
||
| 57 | * @param string $direction |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public static function parseDirection($direction = self::ASC) |
||
| 62 | { |
||
| 63 | if (preg_match('/^asc$/i', $direction)) { |
||
| 64 | return self::ASC; |
||
| 65 | } else { |
||
| 66 | return self::DESC; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Return all order fields |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | public function getOrders() |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param array $object |
||
| 81 | */ |
||
| 82 | public function fromArray(array $object = []) |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function toArray() |
||
| 95 | } |
||
| 96 | } |
||
| 97 |