| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\QueryBuilder\Clauses; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
/** |
| 8 |
* @since 2.19.0 |
| 9 |
*/ |
| 10 |
class OrderBy |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public $column; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
public $direction; |
| 21 |
|
| 22 |
/** |
| 23 |
* @param $column |
| 24 |
* @param $direction |
| 25 |
*/ |
| 26 |
public function __construct($column, $direction) |
| 27 |
{ |
| 28 |
$this->column = trim($column); |
| 29 |
$this->direction = $this->getSortDirection($direction); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @param string $direction |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
private function getSortDirection($direction) |
| 38 |
{ |
| 39 |
$direction = strtoupper($direction); |
| 40 |
$directions = ['ASC', 'DESC']; |
| 41 |
|
| 42 |
if ( ! in_array($direction, $directions, true)) { |
| 43 |
throw new InvalidArgumentException( |
| 44 |
sprintf( |
| 45 |
'Unsupported sort direction %s. Please use one of the (%s)', |
| 46 |
$direction, |
| 47 |
implode(',', $directions) |
| 48 |
) |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
return $direction; |
| 53 |
} |
| 54 |
} |
| 55 |
|