-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathResponse.php
More file actions
70 lines (64 loc) · 1.65 KB
/
Response.php
File metadata and controls
70 lines (64 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php declare(strict_types=1);
namespace Yard\OpenWOO\RestAPI;
use WP_Query;
use WP_REST_Response;
/**
* @OA\Schema()
*/
class Response extends WP_REST_Response
{
/**
* @OA\Property(
* property="WOOVerzoeken",
* type="array",
* @OA\Items(ref="#/components/schemas/OpenWOO"),
* @OA\Link(link="OpenWOORepository", ref="#/components/links/OpenWOORepository")
* )
*/
public function __construct(array $data, WP_Query $query)
{
if (! $query->is_single) {
$data = \array_merge_recursive(
$data,
$this->addPaginator($query),
$this->getQuery($query)
);
}
parent::__construct($data);
}
/**
* @OA\Property(
* property="pagination",
* type="array",
* @OA\Items(
*
* )
* )
*/
protected function addPaginator(WP_Query $query): array
{
$page = $query->get('paged');
$page = (0 === $page) ? 1 : $page;
return [
'pagination' => [
'total' => (int) $query->found_posts,
'limit' => (int) $query->get('posts_per_page'),
'pages' => [
'total' => (int) $query->max_num_pages,
'current' => (int) $page,
]
]
];
}
/**
* @OA\Property(
* property="query_parameters"
* )
*/
protected function getQuery(WP_Query $query): array
{
return [
'query_parameters' => $query->query
];
}
}