Skip to content

Commit 745676f

Browse files
wip
1 parent 9297420 commit 745676f

10 files changed

Lines changed: 1187 additions & 23 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Piggy\Api\Mappers\Orders;
4+
5+
use Piggy\Api\Models\Orders\AppliedDiscount;
6+
use stdClass;
7+
8+
class AppliedDiscountMapper
9+
{
10+
public static function map(stdClass $data): AppliedDiscount
11+
{
12+
return new AppliedDiscount(
13+
$data->uuid,
14+
$data->external_identifier,
15+
$data->name,
16+
$data->amount,
17+
$data->type,
18+
$data->value,
19+
$data->applied_to,
20+
$data->line_items,
21+
$data->sub_line_items
22+
);
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Piggy\Api\Mappers\Orders;
4+
5+
use Piggy\Api\Models\Orders\LineItem;
6+
use stdClass;
7+
8+
class AppliedDiscountsMapper
9+
{
10+
/**
11+
* @param stdClass[] $data
12+
* @return LineItem[]
13+
*/
14+
public static function map(array $data): array
15+
{
16+
$mapper = new AppliedDiscountMapper();
17+
18+
$appliedDiscounts = [];
19+
foreach ($data as $item) {
20+
$appliedDiscounts[] = $mapper->map($item);
21+
}
22+
23+
return $appliedDiscounts;
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Piggy\Api\Mappers\Orders;
4+
5+
use Piggy\Api\Models\Orders\Charge;
6+
use stdClass;
7+
8+
class ChargeMapper
9+
{
10+
public static function map(stdClass $data): Charge
11+
{
12+
return new Charge(
13+
$data->uuid,
14+
$data->external_identifier,
15+
$data->type,
16+
$data->name,
17+
$data->amount,
18+
$data->discount_amount,
19+
$data->total_amount
20+
);
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Piggy\Api\Mappers\Orders;
4+
5+
use Piggy\Api\Models\Orders\LineItem;
6+
use stdClass;
7+
8+
class ChargesMapper
9+
{
10+
/**
11+
* @param stdClass[] $data
12+
* @return LineItem[]
13+
*/
14+
public static function map(array $data): array
15+
{
16+
$mapper = new ChargeMapper();
17+
18+
$charges = [];
19+
foreach ($data as $item) {
20+
$charges[] = $mapper->map($item);
21+
}
22+
23+
return $charges;
24+
}
25+
}

src/Mappers/Orders/OrderMapper.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ public function map(stdClass $data): Order
2929
$lineItems = $mapper->map($data->line_items);
3030
}
3131

32+
$appliedDiscounts = [];
33+
if (isset($data->applied_discounts)) {
34+
$mapper = new AppliedDiscountsMapper();
35+
$appliedDiscounts = $mapper->map($data->applied_discounts);
36+
}
37+
38+
$charges = [];
39+
if (isset($data->charges)) {
40+
$mapper = new ChargesMapper();
41+
$charges = $mapper->map($data->charges);
42+
}
43+
3244
return new Order(
3345
$data->uuid,
3446
$data->external_identifier,
@@ -42,13 +54,14 @@ public function map(stdClass $data): Order
4254
(int) $data->total_discount_amount,
4355
(int) $data->total_order_amount,
4456
$data->paid_at ?? null,
57+
$data->completed_at ?? null,
4558
$data->created_at,
4659
$data->updated_at,
4760
$contact,
4861
$shop,
4962
$lineItems,
50-
$data->applied_discounts,
51-
$data->charges
63+
$appliedDiscounts, // $data->applied_discounts,
64+
$charges // $data->charges
5265
);
5366
}
5467
}

src/Models/Orders/Order.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Piggy\Api\Models\Shops\Shop;
1111
use Piggy\Api\StaticMappers\Orders\OrderMapper;
1212
use Piggy\Api\StaticMappers\Orders\OrdersMapper;
13+
use stdClass;
1314

1415
class Order
1516
{
@@ -73,6 +74,11 @@ class Order
7374
*/
7475
protected $paidAt;
7576

77+
/**
78+
* @var string|null
79+
*/
80+
protected $completedAt;
81+
7682
/**
7783
* @var string
7884
*/
@@ -131,6 +137,7 @@ public function __construct(
131137
int $totalDiscountAmount,
132138
int $totalOrderAmount,
133139
?string $paidAt,
140+
?string $completedAt,
134141
string $createdAt,
135142
string $updatedAt,
136143
?Contact $contact,
@@ -151,6 +158,7 @@ public function __construct(
151158
$this->totalDiscountAmount = $totalDiscountAmount;
152159
$this->totalOrderAmount = $totalOrderAmount;
153160
$this->paidAt = $paidAt;
161+
$this->completedAt = $completedAt;
154162
$this->createdAt = $createdAt;
155163
$this->updatedAt = $updatedAt;
156164
$this->contact = $contact;
@@ -220,6 +228,11 @@ public function getPaidAt(): ?string
220228
return $this->paidAt;
221229
}
222230

231+
public function getCompletedAt(): ?string
232+
{
233+
return $this->completedAt;
234+
}
235+
223236
public function getCreatedAt(): string
224237
{
225238
return $this->createdAt;
@@ -259,7 +272,7 @@ public function getAppliedDiscounts(): array
259272
/**
260273
* @return Charge[]
261274
*/
262-
public function charges(): array
275+
public function getCharges(): array
263276
{
264277
return $this->charges;
265278
}
@@ -325,11 +338,11 @@ public static function create(array $body): Order
325338
* @param string $uuid
326339
* @param array<string, mixed> $body
327340
*
328-
* @return array<string, mixed>
341+
* @return stdClass
329342
*
330343
* @throws GuzzleException|MaintenanceModeException|PiggyRequestException
331344
*/
332-
public static function process(string $uuid, array $body): array
345+
public static function process(string $uuid, array $body = []): stdClass
333346
{
334347
$response = ApiClient::post(self::resourceUri."$uuid/process", $body);
335348

@@ -356,11 +369,11 @@ public static function createAndProcess(array $body): array
356369
/**
357370
* @param array<string, mixed> $body
358371
*
359-
* @return array<string, mixed>
372+
* @return stdClass
360373
*
361374
* @throws GuzzleException|MaintenanceModeException|PiggyRequestException
362375
*/
363-
public static function calculate(array $body): array
376+
public static function calculate(array $body): stdClass
364377
{
365378
$response = ApiClient::post(self::resourceUri."/calculate", $body);
366379

src/Resources/OAuth/Orders/OrdersResource.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Piggy\Api\Mappers\Orders\OrdersMapper;
88
use Piggy\Api\Models\Orders\Order;
99
use Piggy\Api\Resources\BaseResource;
10+
use stdClass;
1011

1112
class OrdersResource extends BaseResource
1213
{
@@ -84,11 +85,11 @@ public function create(array $body): Order
8485
* @param string $uuid
8586
* @param array<string, mixed> $body
8687
*
87-
* @return array<string, mixed>
88+
* @return stdClass
8889
*
8990
* @throws PiggyRequestException
9091
*/
91-
public function process(string $uuid, array $body): array
92+
public function process(string $uuid, array $body = []): stdClass
9293
{
9394
$response = $this->client->post($this->resourceUri."$uuid/process", $body);
9495

@@ -109,19 +110,19 @@ public function createAndProcess(array $body): array
109110
$mapper = new OrderMapper();
110111

111112
return [
112-
'order' => $mapper->map($response->getData()),
113+
'order' => $mapper->map($response->getData()->order),
113114
'result' => $response->getData()->result,
114115
];
115116
}
116117

117118
/**
118119
* @param array<string, mixed> $body
119120
*
120-
* @return array<string, mixed>
121+
* @return stdClass
121122
*
122123
* @throws PiggyRequestException
123124
*/
124-
public function calculate(array $body): array
125+
public function calculate(array $body): stdClass
125126
{
126127
$response = $this->client->post($this->resourceUri."/calculate", $body);
127128

src/Resources/Register/Orders/OrdersResource.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Piggy\Api\Mappers\Orders\OrdersMapper;
88
use Piggy\Api\Models\Orders\Order;
99
use Piggy\Api\Resources\BaseResource;
10+
use stdClass;
1011

1112
class OrdersResource extends BaseResource
1213
{
@@ -84,11 +85,11 @@ public function create(array $body): Order
8485
* @param string $uuid
8586
* @param array<string, mixed> $body
8687
*
87-
* @return array<string, mixed>
88+
* @return stdClass
8889
*
8990
* @throws PiggyRequestException
9091
*/
91-
public function process(string $uuid, array $body): array
92+
public function process(string $uuid, array $body = []): stdClass
9293
{
9394
$response = $this->client->post($this->resourceUri."$uuid/process", $body);
9495

@@ -117,11 +118,11 @@ public function createAndProcess(array $body): array
117118
/**
118119
* @param array<string, mixed> $body
119120
*
120-
* @return array<string, mixed>
121+
* @return stdClass
121122
*
122123
* @throws PiggyRequestException
123124
*/
124-
public function calculate(array $body): array
125+
public function calculate(array $body): stdClass
125126
{
126127
$response = $this->client->post($this->resourceUri."/calculate", $body);
127128

src/StaticMappers/Orders/OrderMapper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static function map(stdClass $data): Order
3939
(int) $data->total_discount_amount,
4040
(int) $data->total_order_amount,
4141
$data->paid_at ?? null,
42+
$data->completed_at ?? null,
4243
$data->created_at,
4344
$data->updated_at,
4445
$contact,

0 commit comments

Comments
 (0)