Skip to content

Commit ef9dc14

Browse files
authored
feat: add prepList() to api
When you need to return a formatted list which you don't want to paginate.
1 parent 8e0a5dd commit ef9dc14

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/Controllers/ApiController.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,58 @@ public function returnArray($arr)
135135
return $this->getResponse()->setBody(json_encode($arr));
136136
}
137137

138+
138139
/**
139140
* Convert a provided DataList to a PaginatedList and return the source.
140141
*
141142
* @param SS_List $list
142143
* @param callable $keyFunc
143144
* @param callabale $dataFunc
145+
* @param int $pageLength
146+
*
147+
* @return array
148+
*/
149+
public function prepList(SS_List $list, $keyFunc = null, $dataFunc = null): array
150+
{
151+
$output = [];
152+
153+
foreach ($list as $item) {
154+
if ($dataFunc) {
155+
$record = $dataFunc($item);
156+
} else if (is_array($item)) {
157+
$record = $item;
158+
} else {
159+
$record = $item->toApi();
160+
}
161+
162+
if ($keyFunc) {
163+
$output[$keyFunc($item)] = $record;
164+
} else {
165+
$output[] = $record;
166+
}
167+
}
168+
169+
return [
170+
$list,
171+
$output
172+
];
173+
}
174+
175+
176+
/**
177+
* Convert a provided List to a PaginatedList and return the source.
178+
*
179+
* @param SS_List $list
180+
* @param callable $keyFunc
181+
* @param callabale $dataFunc
182+
* @param int $pageLength
144183
*
145184
* @return array
146185
*/
147-
public function prepPaginatedOutput(SS_List $list, $keyFunc = null, $dataFunc = null): array
186+
public function prepPaginatedOutput(SS_List $list, $keyFunc = null, $dataFunc = null, $pageLength = 100): array
148187
{
149188
$list = new PaginatedList($list, $this->request);
150-
$list->setPageLength(100);
189+
$list->setPageLength($pageLength);
151190
$output = [];
152191

153192
foreach ($list as $item) {

0 commit comments

Comments
 (0)