150 lines
5.2 KiB
PHP
150 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use App\Enums\Products\CertificationEnum;
|
|
use App\Enums\Products\ClaimEnum;
|
|
use App\Enums\Products\OriginEnum;
|
|
use App\Models\Brand;
|
|
use App\Models\Product;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class ProductResource extends JsonResource
|
|
{
|
|
/** @var \App\Models\Product */
|
|
private $product;
|
|
|
|
/** @var array */
|
|
private static $skeleton = [
|
|
'id' => null,
|
|
'user_id' => null,
|
|
'brand_id' => 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)
|
|
{
|
|
foreach (CertificationEnum::toArray() as $enumValue) {
|
|
if ($this->product->certification->value === $enumValue->value) {
|
|
$certification = $enumValue->label;
|
|
}
|
|
}
|
|
if (count($this->product->origins)) {
|
|
$origins = "";
|
|
foreach ($this->product->origins as $originValue) {
|
|
foreach (OriginEnum::toArray() as $enumValue) {
|
|
if (intval($originValue['id']) === $enumValue->value) {
|
|
$origins = $origins . $enumValue->label . ", ";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (count($this->product->claims)) {
|
|
$claims = "";
|
|
foreach ($this->product->claims as $originValue) {
|
|
foreach (ClaimEnum::toArray() as $enumValue) {
|
|
if (intval($originValue['id']) === $enumValue->value) {
|
|
if ($enumValue->value === ClaimEnum::others->value) {
|
|
$claims = $claims . $enumValue->label . ": " . $this->product->specified_claim . ", ";
|
|
} else {
|
|
$claims = $claims . $enumValue->label . ", ";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
foreach ($this->product->productComments as $productComment) {
|
|
$productComment->date = Carbon::createFromFormat(
|
|
'Y-m-d H:i:s',
|
|
$productComment->created_at
|
|
)->format('d/m/Y');
|
|
}
|
|
if (count($this->product->productKeywords)) {
|
|
$productKeywords = "";
|
|
foreach ($this->product->productKeywords as $productKeyword) {
|
|
$productKeywords = $productKeywords . $productKeyword->label . ", ";
|
|
}
|
|
}
|
|
return array_intersect_key(
|
|
[
|
|
'id' => $this->product->id,
|
|
'user_id' => User::where('id', $this->product->user_id)->firstOrFail(),
|
|
'brand_id' => Brand::where('id', $this->product->brand_id)->firstOrFail(),
|
|
'name' => $this->product->name,
|
|
'seen_at' => Carbon::createFromFormat('Y-m-d H:i:s', $this->product->seen_at)->format('d/m/Y'),
|
|
'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' => $certification,
|
|
'origins' => $origins ?? null,
|
|
'claims' => $claims ?? null,
|
|
'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,
|
|
'productComments' => $this->product->productComments,
|
|
'productKeywords' => $productKeywords ?? null,
|
|
],
|
|
$this->getSkeleton()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Returns the resource skeletton
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getSkeleton(): array
|
|
{
|
|
return self::$skeleton;
|
|
}
|
|
}
|