-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathCustomerResource.php
62 lines (55 loc) · 1.95 KB
/
CustomerResource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace App\Http\Resources;
use App\Enums\CustomerStatus;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
class CustomerResource extends JsonResource
{
public static $wrap = false;
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
$shipping = $this->shippingAddress;
$billing = $this->billingAddress;
if($shipping) {
$shipping = [
'id' => $shipping->id,
'address1' => $shipping->address1,
'address2' => $shipping->address2,
'city' => $shipping->city,
'state' => $shipping->state,
'zipcode' => $shipping->zipcode,
'country_code' => $shipping->country->code,
];
}
if($billing) {
$billing = [
'id' => $billing->id,
'address1' => $billing->address1,
'address2' => $billing->address2,
'city' => $billing->city,
'state' => $billing->state,
'zipcode' => $billing->zipcode,
'country_code' => $billing->country->code,
];
}
return [
'id' => $this->user_id,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'email' => $this->user->email,
'phone' => $this->phone,
'status' => $this->status === CustomerStatus::Active->value,
'created_at' => (new \DateTime($this->created_at))->format('Y-m-d H:i:s'),
'updated_at' => (new \DateTime($this->updated_at))->format('Y-m-d H:i:s'),
'shippingAddress' => $shipping,
'billingAddress' => $billing
];
}
}