| Total Complexity | 12 |
| Total Lines | 91 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| 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 | 1 | public function addOrder($field, $direction = self::ASC) |
|
| 21 | { |
||
| 22 | 1 | $this->fields[$field] = self::parseDirection($direction); |
|
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Remove existing order |
||
| 27 | * |
||
| 28 | * @param string $fieldToRemove |
||
| 29 | */ |
||
| 30 | 1 | public function removeOrder($fieldToRemove) |
|
| 31 | { |
||
| 32 | 1 | $order = []; |
|
| 33 | 1 | if (count($this->fields) > 0) { |
|
| 34 | 1 | foreach ($this->getOrders() as $field => $direction) { |
|
| 35 | 1 | if (strtolower($fieldToRemove) === strtolower($field)) { |
|
| 36 | 1 | continue; |
|
| 37 | } |
||
| 38 | 1 | $order[$field] = $direction; |
|
| 39 | } |
||
| 40 | } |
||
| 41 | 1 | $this->fields = $order; |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Set an order field |
||
| 46 | * |
||
| 47 | * @param string $field |
||
| 48 | * @param string $direction |
||
| 49 | */ |
||
| 50 | 1 | public function setOrder($field, $direction = self::ASC) |
|
| 51 | { |
||
| 52 | 1 | $this->fields = [$field => self::parseDirection($direction)]; |
|
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Parse direction string |
||
| 57 | * @param string $direction |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | 1 | public static function parseDirection($direction = self::ASC) |
|
| 62 | { |
||
| 63 | 1 | if (preg_match('/^asc$/i', $direction)) { |
|
| 64 | 1 | return self::ASC; |
|
| 65 | } else { |
||
| 66 | 1 | return self::DESC; |
|
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Return all order fields |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | 1 | public function getOrders() |
|
| 75 | { |
||
| 76 | 1 | return $this->fields; |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param array $object |
||
| 81 | */ |
||
| 82 | 1 | public function fromArray(array $object = []) |
|
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | 1 | public function toArray() |
|
| 95 | } |
||
| 96 | } |
||
| 97 |