File tree Expand file tree Collapse file tree 3 files changed +29
-10
lines changed
src/Picqer/Carriers/SendCloud/Query Expand file tree Collapse file tree 3 files changed +29
-10
lines changed Original file line number Diff line number Diff line change 1313 */
1414trait FindAll
1515{
16-
17- public function all ($ params = [])
16+ public function all ($ params = [], ?int $ maxPages = 1 ): array
1817 {
19- $ result = $ this ->connection ()->get ($ this ->url , $ params );
18+ $ allRecords = [];
19+ $ pages = 0 ;
20+
21+ while (true ) {
22+ $ result = $ this ->connection ()->get ($ this ->url , $ params );
23+
24+ $ allRecords = array_merge ($ allRecords , $ this ->collectionFromResult ($ result ));
25+
26+ if (! empty ($ result ['next ' ])) {
27+ // Get the querystring params from the next url, so we can retrieve the next page
28+ $ params = parse_url ($ result ['next ' ], PHP_URL_QUERY );
29+ } else {
30+ // If no next page is found, all records are complete
31+ break ;
32+ }
2033
21- return $ this ->collectionFromResult ($ result );
34+ $ pages ++;
35+
36+ // If max pages is set and reached, also stop the loop
37+ if (! is_null ($ maxPages ) && $ pages >= $ maxPages ) {
38+ break ;
39+ }
40+ }
41+
42+ return $ allRecords ;
2243 }
2344
24- public function collectionFromResult ($ result )
45+ public function collectionFromResult ($ result ): array
2546 {
2647 $ collection = [];
2748
@@ -36,5 +57,4 @@ public function collectionFromResult($result)
3657
3758 return $ collection ;
3859 }
39-
4060}
Original file line number Diff line number Diff line change 44
55use Picqer \Carriers \SendCloud \Connection ;
66use Picqer \Carriers \SendCloud \Model ;
7+ use Picqer \Carriers \SendCloud \SendCloudApiException ;
78
89/**
910 * Trait FindOne
1415 */
1516trait FindOne
1617{
17-
1818 /**
1919 * @param $id
2020 * @return Model|FindOne
21+ * @throws SendCloudApiException
2122 */
2223 public function find ($ id )
2324 {
Original file line number Diff line number Diff line change 44
55trait Findable
66{
7-
87 use FindOne;
98 use FindAll;
10-
11- }
9+ }
You can’t perform that action at this time.
0 commit comments