Skip to content

Commit bc8a2ca

Browse files
add mappers
1 parent 4d33c7a commit bc8a2ca

6 files changed

Lines changed: 203 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Piggy\Api\Mappers\Orders;
4+
5+
use Piggy\Api\Mappers\Products\ProductMapper;
6+
use Piggy\Api\Models\Orders\LineItem;
7+
use stdClass;
8+
9+
class LineItemMapper
10+
{
11+
public static function map(stdClass $data): LineItem
12+
{
13+
$product = null;
14+
if (isset($data->product)) {
15+
$mapper = new ProductMapper();
16+
$product = $mapper->map($data->product);
17+
}
18+
19+
$subLineItems = null;
20+
if (isset($data->sub_line_items)) {
21+
$mapper = new SubLineItemsMapper();
22+
$subLineItems = $mapper->map($data->sub_line_items);
23+
}
24+
25+
return new LineItem(
26+
$data->uuid,
27+
$data->external_identifier,
28+
$data->name,
29+
$data->quantity,
30+
$data->price,
31+
$data->discount_amount,
32+
$data->total_amount,
33+
$product,
34+
$subLineItems
35+
);
36+
}
37+
}
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 LineItemsMapper
9+
{
10+
/**
11+
* @param stdClass[] $data
12+
* @return LineItem[]
13+
*/
14+
public static function map(array $data): array
15+
{
16+
$mapper = new LineItemMapper();
17+
18+
$lineItems = [];
19+
foreach ($data as $item) {
20+
$lineItems[] = $mapper->map($item);
21+
}
22+
23+
return $lineItems;
24+
}
25+
}

src/Mappers/Orders/OrderMapper.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Piggy\Api\Mappers\Orders;
4+
5+
use Piggy\Api\Mappers\Contacts\ContactMapper;
6+
use Piggy\Api\Mappers\Shops\ShopMapper;
7+
use Piggy\Api\Models\Orders\Order;
8+
use stdClass;
9+
10+
class OrderMapper
11+
{
12+
public function map(stdClass $data): Order
13+
{
14+
$contact = null;
15+
if (isset($data->contact)) {
16+
$mapper = new ContactMapper();
17+
$contact = $mapper->map($data->contact);
18+
}
19+
20+
$shop = null;
21+
if (isset($data->business_profile)) {
22+
$mapper = new ShopMapper();
23+
$shop = $mapper->map($data->business_profile);
24+
}
25+
26+
$lineItems = [];
27+
if (isset($data->line_items)) {
28+
$mapper = new LineItemMapper();
29+
$lineItems = $mapper->map($data->line_items);
30+
}
31+
32+
return new Order(
33+
$data->uuid,
34+
$data->external_identifier,
35+
$data->currency,
36+
$data->reference ?? null,
37+
$data->status,
38+
$data->payment_status,
39+
$data->formatted_total_order_amount,
40+
isset($data->order_amount) ? (int) $data->order_amount : null,
41+
(int) $data->total_charges_amount,
42+
(int) $data->total_discount_amount,
43+
(int) $data->total_order_amount,
44+
$data->paid_at ?? null,
45+
$data->created_at,
46+
$data->updated_at,
47+
$contact,
48+
$shop,
49+
$lineItems,
50+
$data->applied_discounts,
51+
$data->charges
52+
);
53+
}
54+
}
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\Order;
6+
use stdClass;
7+
8+
class OrdersMapper
9+
{
10+
/**
11+
* @param stdClass[] $data
12+
* @return Order[]
13+
*/
14+
public function map(array $data): array
15+
{
16+
$mapper = new OrderMapper();
17+
18+
$orders = [];
19+
foreach ($data as $item) {
20+
$orders[] = $mapper->map($item);
21+
}
22+
23+
return $orders;
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Piggy\Api\Mappers\Orders;
4+
5+
use Piggy\Api\Mappers\Products\ProductMapper;
6+
use Piggy\Api\Models\Orders\SubLineItem;
7+
use stdClass;
8+
9+
class SubLineItemMapper
10+
{
11+
public static function map(stdClass $data): SubLineItem
12+
{
13+
$lineItem = null;
14+
if (isset($data->line_item)) {
15+
$mapper = new LineItemMapper();
16+
$lineItem = $mapper->map($data->line_item);
17+
}
18+
19+
$product = null;
20+
if (isset($data->product)) {
21+
$mapper = new ProductMapper();
22+
$product = $mapper->map($data->product);
23+
}
24+
25+
return new SubLineItem(
26+
$data->uuid,
27+
$data->external_identifier,
28+
$data->name,
29+
$data->quantity,
30+
$data->price,
31+
$data->discount_amount,
32+
$data->total_amount,
33+
$lineItem,
34+
$product
35+
);
36+
}
37+
}
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\SubLineItem;
6+
use stdClass;
7+
8+
class SubLineItemsMapper
9+
{
10+
/**
11+
* @param stdClass[] $data
12+
* @return SubLineItem[]
13+
*/
14+
public static function map(array $data): array
15+
{
16+
$mapper = new SubLineItemMapper();
17+
18+
$subLineItems = [];
19+
foreach ($data as $item) {
20+
$subLineItems[] = $mapper->map($item);
21+
}
22+
23+
return $subLineItems;
24+
}
25+
}

0 commit comments

Comments
 (0)