| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Bulk_Editor\Domain\Posts; |
| 5 |
|
| 6 |
/** |
| 7 |
* Describes a single page of bulk editor posts together with the totals needed for pagination. |
| 8 |
*/ |
| 9 |
class Posts_Page { |
| 10 |
|
| 11 |
/** |
| 12 |
* The posts on this page. |
| 13 |
* |
| 14 |
* @var Posts_List |
| 15 |
*/ |
| 16 |
private $posts; |
| 17 |
|
| 18 |
/** |
| 19 |
* The total number of posts matching the query across all pages. |
| 20 |
* |
| 21 |
* @var int |
| 22 |
*/ |
| 23 |
private $total; |
| 24 |
|
| 25 |
/** |
| 26 |
* The page of results this represents, starting at 1. |
| 27 |
* |
| 28 |
* @var int |
| 29 |
*/ |
| 30 |
private $page; |
| 31 |
|
| 32 |
/** |
| 33 |
* The number of posts per page. |
| 34 |
* |
| 35 |
* @var int |
| 36 |
*/ |
| 37 |
private $per_page; |
| 38 |
|
| 39 |
/** |
| 40 |
* The constructor. |
| 41 |
* |
| 42 |
* @param Posts_List $posts The posts on this page. |
| 43 |
* @param int $total The total number of posts matching the query. |
| 44 |
* @param int $page The page of results this represents, starting at 1. |
| 45 |
* @param int $per_page The number of posts per page. |
| 46 |
*/ |
| 47 |
public function __construct( Posts_List $posts, int $total, int $page, int $per_page ) { |
| 48 |
$this->posts = $posts; |
| 49 |
$this->total = $total; |
| 50 |
$this->page = $page; |
| 51 |
$this->per_page = $per_page; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns the total number of pages. |
| 56 |
* |
| 57 |
* @return int The total number of pages. |
| 58 |
*/ |
| 59 |
public function get_total_pages(): int { |
| 60 |
if ( $this->per_page < 1 ) { |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
return (int) \ceil( $this->total / $this->per_page ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Parses the page to the expected key value representation. |
| 69 |
* |
| 70 |
* @return array<string, int|array<array<string, int|string>>> The page presented as the expected key value representation. |
| 71 |
*/ |
| 72 |
public function to_array(): array { |
| 73 |
return [ |
| 74 |
'posts' => $this->posts->to_array(), |
| 75 |
'total' => $this->total, |
| 76 |
'total_pages' => $this->get_total_pages(), |
| 77 |
'page' => $this->page, |
| 78 |
'per_page' => $this->per_page, |
| 79 |
]; |
| 80 |
} |
| 81 |
} |
| 82 |
|