/home/bdqbpbxa/api-uniferx.goodface.com.ua/app/Http/Resources/Pages/Resource/CropsResource.php
<?php
namespace App\Http\Resources\Pages\Resource;
use App\Helpers\ResourceHelper;
use App\Http\Resources\Pages\Career\VacancyCollection;
use App\Http\Resources\Pages\Catalogs\FlyerCollection;
use App\Models\Post;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;
/**
* @OA\Schema(title="CropsResource", description="Crops resource",
* @OA\Xml(name="CropsResource")
* )
*/
class CropsResource extends JsonResource
{
/**
* @OA\Property(property="slug", type="string", example="cereals"),
* @OA\Property(property="title", type="string", example="Cereals"),
* @OA\Property(property="icon", type="string", example="https://uniferx-backend.test/storage/crops/Q77T5Cfhol6lCOtLLwMZNUBkow1gMvd1Y9UNRHfs.png"),
* @OA\Property(property="detailed_data", type="string", example="<img src='storage/uploads/XfAl0JC9lnnH9cSIdpMKQiHIHR1Ezxufsn3PXIGH.png'/>"),
* @OA\Property(property="purpose_processing", type="string", example="Purpose of processing"),
* @OA\Property(property="purposes",type="array", description="Detailed page of crop nutrition",
* @OA\Items( ref="#/components/schemas/PurposeResource"),
* ),
* @OA\Property(property="trials",type="array", description="Detailed trial results page",
* @OA\Items( ref="#/components/schemas/CropTrialsResource"),
* ),
*/
public string|null $language;
public string|null $country;
public string|null $type;
public bool $full;
public string|null $slug;
public string|null $title;
public array|null $title_image;
public array|null $icon;
public array|null $fs_background;
public array|null $fs_view_button;
public array|null $sidebar_text;
public array|object|null $detailed;
public array|object|null $trials;
public object|null $flyers;
public function language($value): static
{
$this->language = $value;
return $this;
}
public function country($value): static
{
$this->country = $value;
return $this;
}
public function full($value = true): static
{
$this->full = $value;
return $this;
}
public function type($value = false): static
{
$this->type = $value;
return $this;
}
public function __construct($resource, $language = null, $country = null, $full = true, $type = null)
{
parent::__construct($resource);
// dd($resource->detailed);
$this->slug = $resource->slug;
$this->title = $resource->title;
$this->title_image = $resource->title_image;
$this->icon = $resource->icon;
$this->fs_background = $resource->fs_background;
$this->fs_view_button = $resource->fs_view_button;
$this->sidebar_text = $resource->sidebar_text;
$this->detailed = $resource->detailed;
$this->trials = $resource->trials;
$this->country = $country;
$this->language = $language;
$this->full = $full;
$this->type = $type;
$this->flyers = $resource->flyers;
}
public function toArray(Request $request): array
{
$data = [
'slug' => $this->slug,
'title' => ResourceHelper::getValue($this->title, $this->country, $this->language),
'icon' => ResourceHelper::getImage($this->icon),
];
if ($this->full) {
$product = null;
if ($request->product) {
$product = Product::query()->where('slug', $request->product)->first();
}
if ($this->type === 'nutrition') {
$data['fs_background'] = ResourceHelper::getImage($this->fs_background['nutrition'] ?? null);
$data['fs_view_button'] = ResourceHelper::getValue($this->fs_view_button['nutrition'] ?? null, $this->country, $this->language);
foreach ($this->detailed as $detailed) {
$detailed = $detailed['attributes'] ?? null;
$cropProduct = null;
if (isset($detailed['product'])) {
$cropProduct = Product::query()->find((integer)$detailed['product']);
}
if ($product && isset($detailed['product']) && (integer)$product->id !== (integer)$detailed['product']) {
continue;
}
$data['details'][] = [
'product' => ResourceHelper::getValue($cropProduct->title ?? null, $this->country, $this->language),
'detailed_data' => ResourceHelper::getValue($detailed['detailed_data'] ?? null, $this->country, $this->language),
'purpose_processing' => ResourceHelper::getValue($detailed['purpose_processing'] ?? null, $this->country, $this->language),
'purposes' => $detailed['data']
? PurposeCollection::make($detailed['data'])
->country($this->country)
->language($this->language)
: null,
];
}
// $data['sidebar_text'] = ResourceHelper::getValue($this->sidebar_text['nutrition'] ?? null, $this->country, $this->language);
} elseif ($this->type === 'trials') {
$data['fs_background'] = ResourceHelper::getImage($this->fs_background['trials'] ?? null);
$data['fs_view_button'] = ResourceHelper::getValue($this->fs_view_button['trials'] ?? null, $this->country, $this->language);
// $data['sidebar_text'] = ResourceHelper::getValue($this->sidebar_text['trials'] ?? null, $this->country, $this->language);
$data['show_more'] = ResourceHelper::getValue($this->sidebar_text['show_more'] ?? null, $this->country, $this->language);
$data['trials'] = $this->flyers ? FlyerCollection::make($this->flyers()->where('is_enabled', true)->get()->collect())->language($this->language)->country($this->country) : null;
// $data['trials'] = $this->trials
// ? CropTrialsCollection::make($this->trials)
// ->country($this->country)
// ->language($this->language)
// : null;
}
}
return $data;
}
public static function collection($resource): VacancyCollection
{
return new VacancyCollection($resource);
}
}