42 lines
		
	
	
		
			859 B
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
		
			859 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(object|string $field, ? string $direction = null) : self
 | |
|     {
 | |
|         $this->validateFieldType($field);
 | |
| 
 | |
|         $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)
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
| }
 |