ldc-cocotte/app/Http/Resources/ProductResource.php
2022-10-10 10:37:40 +02:00

58 lines
1.3 KiB
PHP

<?php
namespace App\Http\Resources;
use App\Models\Product;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
/** @var \App\Models\Product */
private $product;
/** @var array */
private static $skeleton = [
'id' => 0,
'slug' => '',
'name' => '',
'description' => '',
'price' => 0,
'discountedPriceHt' => 0,
'picture' => '',
'orderable_start' => '',
'orderable_end' => ''
];
/**
* ProductResource
*
* @param \App\Models\Product $product
*/
public function __construct(Product $product)
{
$this->product = $product;
}
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request|null $request
* @return array
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
*/
public function toArray($request = null)
{
// @phpcs:enable
$data = array_merge(
['discountedPriceHt' => $this->product->discountedPriceHt],
\array_intersect_key(
$this->product->toArray(),
self::$skeleton
)
);
$data['description'] = clean($data['description']);
return $data;
}
}