ldc-startcoq/app/Http/Resources/ProductResource.php
2023-10-02 09:20:59 +02:00

115 lines
4 KiB
PHP

<?php
namespace App\Http\Resources;
use App\Enums\Products\ClaimEnum;
use App\Enums\Products\OriginEnum;
use App\Models\Keyword;
use App\Models\Product;
use App\Models\ProductComment;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
/** @var \App\Models\Product */
private $product;
/** @var array */
private static $skeleton = [
'id' => null,
'user' => null,
'brand' => null,
'name' => null,
'seen_at' => null,
'source' => null,
'fair' => null,
'shop' => null,
'catalogue' => null,
'gencod' => null,
'weight' => null,
'pvc' => null,
'picture' => null,
'picture_alt' => null,
'picture_title' => null,
'certification' => null,
'origins' => null,
'claims' => null,
'distribution_network' => null,
'ray' => null,
'product_range' => null,
'geo_area' => null,
'freeComment' => null,
'productComments' => null,
'productKeywords' => null,
];
/**
* ProductResource
*
* @param \App\Models\Product $product
* @return void
*/
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)
{
return array_intersect_key(
[
'id' => $this->product->id,
'user' => (object)$this->product->user->toArray(),
'brand' => (object)$this->product->brand->toArray(),
'name' => $this->product->name,
'seen_at' => $this->product->seen_at->isoFormat('DD\/MM\/YYYY \à HH\hmm'),
'source' => $this->product->source->label(),
'fair' => $this->product->fair,
'shop' => $this->product->shop,
'catalogue' => $this->product->catalogue,
'gencod' => $this->product->gencod,
'weight' => $this->product->weight,
'pvc' => $this->product->pvc,
'picture' => $this->product->picture,
'picture_alt' => $this->product->picture_alt,
'picture_title' => $this->product->picture_title,
'certification' => \optional($this->product->certification)->label(),
'origins' => $this->product->origins_enum->map(fn (OriginEnum $enum) => $enum->label())->implode(', '),
'claims' => $this->product->claims_enum->map(fn (ClaimEnum $enum) => $enum->label())->implode(', '),
'distribution_network' => $this->product->distribution_network->label(),
'ray' => $this->product->ray->label(),
'product_range' => $this->product->product_range->label(),
'geo_area' => $this->product->geo_area->label(),
'freeComment' => $this->product->comment,
// FIXME: Use dedicaced resources for relations.
'productComments' => $this->product->productComments->map(function (ProductComment $comment) {
return (object)\array_merge($comment->toArray(), [
'created_at' => $comment->created_at->isoFormat('DD\/MM\/YYYY'),
'updated_at' => $comment->updated_at->isoFormat('DD\/MM\/YYYY'),
]);
}),
'productKeywords' => $this->product->productKeywords
->map(fn (Keyword $keyword) => $keyword->label)
->implode(', '),
],
$this->getSkeleton()
);
}
/**
* Returns the resource skeletton
*
* @return array
*/
public static function getSkeleton(): array
{
return self::$skeleton;
}
}