67 lines
1.8 KiB
PHP
67 lines
1.8 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' => '',
|
|
'picture' => '',
|
|
'orderable_start' => '',
|
|
'orderable_end' => '',
|
|
'weight_variable' => false,
|
|
'sell_unit' => '',
|
|
'is_available' => false
|
|
];
|
|
|
|
/**
|
|
* 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(
|
|
\array_intersect_key(
|
|
$this->product->toArray(),
|
|
self::$skeleton
|
|
),
|
|
['priceTtc' => $this->product->priceTtc],
|
|
['priceKgTtc' => $this->product->priceKgTtc],
|
|
['discountedPriceTtc' => $this->product->discountedPriceTtc],
|
|
['promotion' => (isset($this->product->promotions[0])) ? $this->product->promotions : null],
|
|
[
|
|
'nutriscore' => (isset($this->product->foodProduct->nutriscore)) ?
|
|
$this->product->foodProduct->nutriscore :
|
|
null
|
|
]
|
|
);
|
|
|
|
$data['description'] = clean($data['description']);
|
|
|
|
return $data;
|
|
}
|
|
}
|