ulmus/src/Query/OrderBy.php
2020-10-06 11:43:31 -04:00

40 lines
809 B
PHP

<?php
namespace Ulmus\Query;
class OrderBy extends Fragment {
public int $order = 85;
public array $orderBy = [];
const SQL_TOKEN = "ORDER BY";
public function set(array $order) : self
{
$this->orderBy = $order;
return $this;
}
public function add(string $field, ? string $direction = null) : self
{
$this->orderBy[] = [ $field, $direction ];
return $this;
}
public function render() : string
{
$list = array_map(function($item) {
list($field, $direction) = $item;
return $field . ( $direction ? " $direction" : "" );
}, $this->orderBy);
return $this->renderSegments([
static::SQL_TOKEN, implode(", ", $list)
]);
}
}