mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 20:44:19 +00:00
121 lines
3.2 KiB
PHP
121 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Transformers;
|
|
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\SegmentConstants;
|
|
use App\Models\Address;
|
|
use App\Models\Company;
|
|
use App\Models\Order;
|
|
use App\Models\SegmentConstant;
|
|
use League\Fractal\Resource\Item;
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
class AddressTransformer extends TransformerAbstract
|
|
{
|
|
/**
|
|
* List of resources to automatically include
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $defaultIncludes = [
|
|
'contact',
|
|
'remark'
|
|
];
|
|
|
|
/**
|
|
* List of resources possible to include
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $availableIncludes = [
|
|
'company_module',
|
|
'Owner_address',
|
|
|
|
];
|
|
|
|
/**
|
|
* A Fractal transformer.
|
|
*
|
|
* @param Address $address
|
|
* @return array
|
|
*/
|
|
public function transform(Address $address): array
|
|
{
|
|
return [
|
|
'id' => $address->id,
|
|
'reference' => $address->reference,
|
|
'street_one' => $address->street_one,
|
|
'street_two' => $address->street_two,
|
|
'district' => $address->district,
|
|
'state' => $address->state,
|
|
'postcode' => $address->postcode,
|
|
'post_code' => $address->postcode,
|
|
'post_code_area' => $this->getPostalCodeArea($address),
|
|
'country' => $address->country,
|
|
'default' => $address->default,
|
|
'status' => $address->status,
|
|
'type' => $address->type,
|
|
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* Get Postal code Area
|
|
* @param Address $address
|
|
* @return string|null
|
|
*/
|
|
private function getPostalCodeArea(Address $address): ?string
|
|
{
|
|
$centerPostcodeConstantObject = SegmentConstant::where('reference', SegmentConstants::CENTER_POSTCODE)->get();
|
|
$centerPostcodeConstant = $centerPostcodeConstantObject->isEmpty() ? [] : (array)$centerPostcodeConstantObject->first()->value;
|
|
return in_array($address->postcode, $centerPostcodeConstant) ? 'CENTER_POSTCODE' : null;
|
|
}
|
|
|
|
/**
|
|
* Include Contact
|
|
*
|
|
* @param Address $address
|
|
* @return Item
|
|
*/
|
|
public function includeContact(Address $address): Item
|
|
{
|
|
$contact = $address->contacts();
|
|
|
|
return $this->item($contact->first(), new ContactTransformer());
|
|
}
|
|
|
|
/**
|
|
* Include in Remark
|
|
* @param Address $address
|
|
* @return Item
|
|
*/
|
|
public function includeRemark(Address $address): Item
|
|
{
|
|
$remarks = $address->remarks();
|
|
return $this->item($remarks->first(), new RemarkTransformer());
|
|
|
|
}
|
|
|
|
/**
|
|
* ?Owner Address info
|
|
* @param Address $address
|
|
* @return array
|
|
*/
|
|
public function includeOwner_address(Address $address): array
|
|
{
|
|
|
|
return [
|
|
'owner' => $address->when($address->owner instanceof Order, [
|
|
'reference' => $address->owner->reference,
|
|
'company_module' => $this->item($address->owner->companyModule, new RemarkTransformer()),
|
|
'address' => $this->item($address->owner->addresses()->where('status', '=', ApprovalStatus::APPROVED)->first(), new AddressTransformer()),
|
|
])
|
|
];
|
|
|
|
}
|
|
|
|
|
|
}
|