Skip to content

Commit 0e9a680

Browse files
author
Diego Relyveld
authored
Add paginated voucher list (#49)
* Add paginated voucher list * Fix PHPStan * Fix PHPStan
1 parent fa39209 commit 0e9a680

4 files changed

Lines changed: 173 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Piggy\Api\Mappers;
4+
5+
abstract class BasePaginatedMapper
6+
{
7+
/**
8+
* @var mixed[]
9+
*/
10+
public $data;
11+
12+
/**
13+
* @var mixed[]
14+
*/
15+
public $meta;
16+
17+
/**
18+
* @param mixed[] $data
19+
* @param mixed[] $meta
20+
*/
21+
public function __construct(array $data, array $meta)
22+
{
23+
$this->data = $data;
24+
$this->meta = $meta;
25+
}
26+
27+
/**
28+
* Get the mapped data
29+
*
30+
* @return mixed[]
31+
*/
32+
abstract public function getData(): array;
33+
34+
/**
35+
* @return mixed[]
36+
*/
37+
public function getMeta(): array
38+
{
39+
return $this->meta;
40+
}
41+
42+
/**
43+
* Get the current page
44+
*/
45+
public function getCurrentPage(): int
46+
{
47+
return $this->getMeta()['current_page'];
48+
}
49+
50+
51+
/**
52+
* Get the starting item number
53+
*/
54+
public function getFrom(): int
55+
{
56+
return $this->getMeta()['from'];
57+
}
58+
59+
/**
60+
* Get the last page
61+
*/
62+
public function getLastPage(): int
63+
{
64+
return $this->getMeta()['last_page'];
65+
}
66+
67+
/**
68+
* Get the pagination links
69+
*
70+
* @return mixed[]
71+
*/
72+
public function getLinks(): array
73+
{
74+
return $this->getMeta()['links'];
75+
}
76+
77+
/**
78+
* Get the current path
79+
*/
80+
public function getPath(): string
81+
{
82+
return $this->getMeta()['path'];
83+
}
84+
85+
/**
86+
* Get the items per page count
87+
*/
88+
public function getPerPage(): int
89+
{
90+
return $this->getMeta()['per_page'];
91+
}
92+
93+
/**
94+
* Get the ending item number
95+
*/
96+
public function getTo(): int
97+
{
98+
return $this->getMeta()['to'];
99+
}
100+
101+
/**
102+
* Get the total count
103+
*/
104+
public function getTotal(): int
105+
{
106+
return $this->getMeta()['total'];
107+
}
108+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Piggy\Api\Mappers\Vouchers;
4+
5+
use Piggy\Api\Mappers\BasePaginatedMapper;
6+
use Piggy\Api\Models\Vouchers\Voucher;
7+
8+
class PaginatedVouchersMapper extends BasePaginatedMapper
9+
{
10+
/**
11+
* Get the mapped vouchers
12+
*
13+
* @return Voucher[]
14+
*/
15+
public function getData(): array
16+
{
17+
$mapper = new VoucherMapper();
18+
$vouchers = [];
19+
20+
foreach ($this->data as $item) {
21+
$vouchers[] = $mapper->map($item);
22+
}
23+
24+
return $vouchers;
25+
}
26+
}

src/Models/Vouchers/Voucher.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Piggy\Api\Exceptions\MaintenanceModeException;
99
use Piggy\Api\Exceptions\PiggyRequestException;
1010
use Piggy\Api\Models\Contacts\Contact;
11+
use Piggy\Api\Mappers\Vouchers\PaginatedVouchersMapper;
1112
use Piggy\Api\StaticMappers\Vouchers\VoucherLockMapper;
1213
use Piggy\Api\StaticMappers\Vouchers\VoucherMapper;
1314
use Piggy\Api\StaticMappers\Vouchers\VouchersMapper;
@@ -213,6 +214,24 @@ public static function list(array $params = []): array
213214
return VouchersMapper::map((array) $response->getData());
214215
}
215216

217+
/**
218+
* @param mixed[] $params
219+
*
220+
* @throws MaintenanceModeException|GuzzleException|PiggyRequestException
221+
*/
222+
public static function paginatedList(int $page = 1, int $limit = 30, array $params = []): PaginatedVouchersMapper
223+
{
224+
$response = ApiClient::get(
225+
self::resourceUri,
226+
array_merge($params, ['page' => $page, 'limit' => $limit])
227+
);
228+
229+
return new PaginatedVouchersMapper(
230+
(array) $response->getData(),
231+
(array) $response->getMeta()
232+
);
233+
}
234+
216235
/**
217236
* @param mixed[] $params
218237
*

src/Resources/Shared/Vouchers/BaseVouchersResource.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTime;
66
use Piggy\Api\Exceptions\PiggyRequestException;
7+
use Piggy\Api\Mappers\Vouchers\PaginatedVouchersMapper;
78
use Piggy\Api\Mappers\Vouchers\VoucherLockMapper;
89
use Piggy\Api\Mappers\Vouchers\VoucherMapper;
910
use Piggy\Api\Mappers\Vouchers\VouchersMapper;
@@ -51,6 +52,25 @@ public function list(int $page = 1, int $limit = 30, ?string $promotionUuid = nu
5152
return $mapper->map((array) $response->getData());
5253
}
5354

55+
/**
56+
* @throws PiggyRequestException
57+
*/
58+
public function paginatedList(int $page = 1, int $limit = 30, ?string $promotionUuid = null, ?string $contactUuid = null, ?string $status = null): PaginatedVouchersMapper
59+
{
60+
$response = $this->client->get($this->resourceUri, [
61+
'page' => $page,
62+
'limit' => $limit,
63+
'promotion_uuid' => $promotionUuid,
64+
'contact_uuid' => $contactUuid,
65+
'status' => $status,
66+
]);
67+
68+
return new PaginatedVouchersMapper(
69+
(array) $response->getData(),
70+
(array) $response->getMeta()
71+
);
72+
}
73+
5474
/**
5575
* @throws PiggyRequestException
5676
*/

0 commit comments

Comments
 (0)