| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FL\Assistant\Data; |
| 5 |
|
| 6 |
|
| 7 |
/** |
| 8 |
* Class Pager |
| 9 |
* @package FL\Assistant\Data |
| 10 |
*/ |
| 11 |
class Pager { |
| 12 |
/** |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $items; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var int |
| 19 |
*/ |
| 20 |
protected $total; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var int |
| 24 |
*/ |
| 25 |
protected $limit; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var int |
| 29 |
*/ |
| 30 |
protected $offset; |
| 31 |
/** |
| 32 |
* @var mixed |
| 33 |
*/ |
| 34 |
protected $last_page; |
| 35 |
/** |
| 36 |
* @var float |
| 37 |
*/ |
| 38 |
protected $current_page; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var bool |
| 42 |
*/ |
| 43 |
protected $has_more; |
| 44 |
|
| 45 |
/** |
| 46 |
* Pager constructor. |
| 47 |
* |
| 48 |
* @param array $items |
| 49 |
* @param int $total |
| 50 |
* @param int $limit |
| 51 |
* @param int $offset |
| 52 |
*/ |
| 53 |
public function __construct( array $items = [], $total = 0, $limit = 20, $offset = 0 ) { |
| 54 |
|
| 55 |
$this->items = $items; |
| 56 |
$this->total = intval( $total ); |
| 57 |
$this->limit = intval( $limit ); |
| 58 |
$this->offset = intval( $offset ); |
| 59 |
|
| 60 |
if ( 0 === count( $items ) || 0 === $this->total || 0 === $this->limit ) { |
| 61 |
$this->last_page = 1; |
| 62 |
$this->current_page = 1; |
| 63 |
$this->has_more = false; |
| 64 |
} else { |
| 65 |
$this->last_page = max( (int) ceil( $this->total / $this->limit ), 1 ); |
| 66 |
$this->current_page = ceil( ( $this->offset + 1 ) / $this->limit ); |
| 67 |
$this->has_more = ( $this->current_page < $this->last_page ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public function items() { |
| 75 |
return $this->items; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @param array $items |
| 80 |
* |
| 81 |
* @return Pager |
| 82 |
*/ |
| 83 |
public function set_items( array $items = [] ) { |
| 84 |
$this->items = $items; |
| 85 |
|
| 86 |
return $this; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Transforms the internal items array |
| 91 |
* |
| 92 |
* @param callable $transformer |
| 93 |
* |
| 94 |
* @return Pager |
| 95 |
*/ |
| 96 |
public function apply_transform( callable $transformer ) { |
| 97 |
$this->items = array_map( $transformer, $this->items ); |
| 98 |
|
| 99 |
return $this; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* @return int |
| 105 |
*/ |
| 106 |
public function total() { |
| 107 |
return $this->total; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* @return int |
| 113 |
*/ |
| 114 |
public function limit() { |
| 115 |
return $this->limit; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* @return int |
| 121 |
*/ |
| 122 |
public function offset() { |
| 123 |
return $this->offset; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* @return mixed |
| 129 |
*/ |
| 130 |
public function last_page() { |
| 131 |
return $this->last_page; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* @return float |
| 137 |
*/ |
| 138 |
public function current_page() { |
| 139 |
return $this->current_page; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* @return bool |
| 145 |
*/ |
| 146 |
public function has_more() { |
| 147 |
return $this->has_more; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
/** |
| 152 |
* @return array |
| 153 |
*/ |
| 154 |
public function to_array() { |
| 155 |
return [ |
| 156 |
'items' => $this->items, |
| 157 |
'items_count' => $this->total, |
| 158 |
'items_per_page' => $this->limit, |
| 159 |
'current_page' => $this->current_page, |
| 160 |
'current_offset' => $this->offset, |
| 161 |
'first_page' => 1, |
| 162 |
'last_page' => $this->last_page, |
| 163 |
'has_more' => $this->has_more, |
| 164 |
]; |
| 165 |
} |
| 166 |
|
| 167 |
public function to_rest_response() { |
| 168 |
return rest_ensure_response( $this->to_array() ); |
| 169 |
} |
| 170 |
} |
| 171 |
|