forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiCollection.php
More file actions
155 lines (134 loc) · 4.59 KB
/
ApiCollection.php
File metadata and controls
155 lines (134 loc) · 4.59 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace ProcessMaker\Http\Resources;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use ProcessMaker\Models\UserResourceView;
/**
* @OA\Schema(
* schema="metadata",
* @OA\Property(property="filter", type="string"),
* @OA\Property(property="sort_by", type="string"),
* @OA\Property(property="sort_order", type="string", enum={"asc", "desc"}),
* @OA\Property(property="count", type="integer"),
* @OA\Property(property="total_pages", type="integer"),
*
* @OA\Property(property="current_page", type="integer"),
* @OA\Property(property="form", type="integer"),
* @OA\Property(property="last_page", type="integer"),
* @OA\Property(property="path", type="string"),
* @OA\Property(property="per_page", type="integer"),
* @OA\Property(property="to", type="integer"),
* @OA\Property(property="total", type="integer"),
* )
*/
class ApiCollection extends ResourceCollection
{
public $appends = [];
protected $appended;
protected $totalCount;
/**
* Create a new resource instance.
*
* @param mixed $resource
* @return void
*/
public function __construct($resource, $total = null)
{
parent::__construct($resource);
if ($total !== null) {
$this->totalCount = (int) $total;
}
$this->appended = (object) [];
foreach ($this->appends as $property) {
if (property_exists($resource, $property)) {
$this->appended->{$property} = $resource->{$property};
}
}
}
/**
* Generic collection to add sorting and filtering metadata.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
UserResourceView::addToResourceCollection($this->collection, $request->user());
$payload = [
'data' => $this->collection,
'meta' => [
'filter' => htmlentities($request->input('filter', '')),
'sort_by' => $request->input('order_by', ''),
'sort_order' => $request->input('order_direction', ''),
/**
* count: (integer, total items in current response)
*/
'count' => $this->resource->count(),
/**
* total_pages: (integer, the total number of pages available, based on per_page and total)
*/
'total_pages' => ceil($this->resource->total() / $this->resource->perPage()),
],
];
return $payload;
}
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function toResponse($request)
{
if ($this->resource instanceof Collection) {
$this->resource = $this->collectionToPaginator($this->resource, $request);
}
return $this->resource instanceof AbstractPaginator
? (new ApiPaginatedResourceResponse($this))->toResponse($request)
: parent::toResponse($request);
}
/**
* Convert a Collection to a LengthAwarePaginator
*
* @param \Illuminate\Support\Collection $collection
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
public function collectionToPaginator(Collection $collection, Request $request)
{
if ($this->totalCount === null) {
$count = $collection->count();
} else {
$count = $this->totalCount;
}
$page = (int) $request->input('page', 1);
$perPage = (int) $request->input('per_page', 10);
$startIndex = ($page - 1) * $perPage;
$limit = $perPage;
if ($this->collection->count() > $limit) {
$this->collection = $collection->slice($startIndex, $limit)->values();
}
return new LengthAwarePaginator($this->collection, $count, $perPage);
}
/**
* Return the total count.
*
* @return int
*/
public function getTotal()
{
if ($this->totalCount !== null) {
return $this->totalCount;
} else {
$resource = $this->resource->toArray();
if (isset($resource['total'])) {
return $resource['total'];
}
}
return null;
}
}