File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Orders/Resources/Order.php
<?php
namespace App\Orders\Resources;
use App\Users\Resources\UserResource;
use Illuminate\Http\Resources\Json\Resource;
use Komma\KMS\Globalization\RegionInfoInterface;
/**
* Resource for transforming an order into json
* @see https://laravel.com/docs/5.5/eloquent-resources
* @see \App\Orders\Models\Order
*/
class Order extends Resource
{
/** @var RegionInfoInterface */
private $regionInfo;
/**
* Order constructor.
* @param $resource
*/
public function __construct($resource)
{
$this->regionInfo = app(RegionInfoInterface::class);
parent::__construct($resource);
}
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
* @mixin \App\Orders\Models\Order
*/
public function toArray($request)
{
return [
'id' => $this->id,
'status' => $this->status,
$this->mergeWhen($this->total_price, fn() => [
'total_price' => $this->total_price,
'total_price_ex' => $this->total_price,
'total_price_formatted' => $this->regionInfo->getNumberFormat()->centsToCurrency($this->total_price, true, false),
'total_price_ex_formatted' => $this->regionInfo->getNumberFormat()->centsToCurrency($this->total_price, true, false),
]),
'discount' => $this->discount,
'order_number' => $this->order_number,
'invoice_number' => $this->invoice_number,
'invoice_id' => $this->invoice_id,
'invoice_gender' => $this->invoice_gender,
'invoice_title' => $this->invoice_title,
'invoice_first_name' => $this->invoice_first_name,
'invoice_last_name' => $this->invoice_last_name,
'invoice_email' => $this->invoice_email,
'invoice_company' => $this->invoice_company,
'invoice_country' => $this->invoice_country,
'invoice_postal_code' => $this->invoice_postal_code,
'invoice_city' => $this->invoice_city,
'invoice_street' => $this->invoice_street,
'invoice_house_number' => $this->invoice_house_number,
'invoice_telephone' => $this->invoice_telephone,
'shipping_gender' => $this->shipping_gender,
'shipping_title' => $this->shipping_title,
'shipping_first_name' => $this->shipping_first_name,
'shipping_last_name' => $this->shipping_last_name,
'shipping_email' => $this->shipping_email,
'shipping_company' => $this->shipping_company,
'shipping_country' => $this->shipping_country,
'shipping_postal_code' => $this->shipping_postal_code,
'shipping_city' => $this->shipping_city,
'shipping_street' => $this->shipping_street,
'shipping_house_number' => $this->shipping_house_number,
'shipping_telephone' => $this->shipping_telephone,
'customer' => $this->whenLoaded('customer', new UserResource($this->customer)),
'orderedProducts' => OrderedProduct::collection($this->whenLoaded('orderedProducts')),
'orderedProductGroups' => OrderedProductGroup::collection($this->whenLoaded('orderedGroups')),
'orderedProductComposites' => OrderedProductComposite::collection($this->whenLoaded('orderedProductComposites'))
];
}
}