ldc-boutique-cnc/app/Http/Resources/ProductResource.php
2022-12-19 16:20:00 +01:00

61 lines
1.6 KiB
PHP

<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\Product;
class ProductResource extends JsonResource
{
/** @var \App\Models\Product */
private $product;
// FIXME: Add more of the needed lines.
/** @var array */
private $skeleton = [
'id' => 0,
'name' => '',
'price' => 0,
'price_ttc' => 0,
'discount_value' => 0,
'discount_rate' => 0,
'discounted_price' => 0,
'undiscounted_price_ttc' => 0,
];
/**
* ProductResource
*
* @param \App\Models\Product $product
*/
public function __construct(Product $product)
{
$this->product = $product;
}
/**
* Transform the resource into an array.
*
* @param mixed $request
* @return array
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
*/
public function toArray($request)
{
// phpcs:enable
return array_intersect_key(
array_merge(
[
'price' => $this->product->price,
'price_ttc' => $this->product->price_ttc,
'discount_value' => $this->product->discount_value,
'discount_rate' => $this->product->discount_rate,
'discounted_price' => $this->product->discounted_price,
'undiscounted_price_ttc' => $this->product->undiscounted_price_ttc
],
$this->product->toArray(),
),
$this->skeleton
);
}
}