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, 'specified_claim' => 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'), '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(', '), 'specified_claim' => $this->product->specified_claim, '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; } }