/home/bdqbpbxa/api-uniferx.goodface.com.ua/app/Http/Resources/Pages/Blog/BlogResource.php
<?php

namespace App\Http\Resources\Pages\Blog;

use App\Helpers\ResourceHelper;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;

/**
 * @OA\Schema(title="BlogResource", description="Blog resource",
 *     @OA\Xml(name="BlogResource")
 * )
 */
class BlogResource extends JsonResource
{
    /**
     * @OA\Property(property="title", type="string", example="Research, News & Events"),
     * @OA\Property(property="title_background", type="string", example="https://uniferx-backend.test/image.png"),
     * @OA\Property(property="description", type="string", example="<p>We're constantly <em>publishing fresh news and research</em>, that will help expand your knowledge of the current <em>challenges in the agricultural industry</em>. (UK)</p>"),
     * @OA\Property(property="subscribe_title", type="string", example="Subscribe for a newsletter"),
     * @OA\Property(property="subscribe_description", type="string", example="Share your email so we can send you latest UniferX news & events"),
     * @OA\Property(property="subscribe_policy_text", type="string", example=""),
     * @OA\Property(property="show_more_button", type="string", example="Show more"),
     * @OA\Property(property="posts", type="object", ref="#/components/schemas/PostResource"),
     */

    public string|null $name;
    public string|null $language;
    public string|null $country;
    public array|null $attributes;

    public function __construct($resource, $language, $country)
    {
        parent::__construct($resource);
        $this->name = $resource->name;
        $this->language = $language;
        $this->country = $country;
        $this->attributes = $resource->data;
    }

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $posts = Post::query()->where('is_enabled', true)->limit(6)->get();
        $keys = $posts->collect()->mapWithKeys(function ($item, $key) {
            return [$key => $item['type']];
        })->unique();
//        dd($keys);
        return [
            'title' => ResourceHelper::getValue($this->attributes['title'] ?? null, $this->country, $this->language),
            'title_background' => ResourceHelper::getImage($this->attributes['fs_title_background'] ?? null),
            'fs_button_text' => ResourceHelper::getValue($this->attributes['subscribe_button'] ?? null, $this->country, $this->language),
            'description' => ResourceHelper::getValue($this->attributes['description'] ?? null, $this->country, $this->language),
            'show_all_button' => ResourceHelper::getValue($this->attributes['show_all_button'] ?? null, $this->country, $this->language),
            'subscribe_tag' => ResourceHelper::getValue($this->attributes['subscribe_tag'] ?? null, $this->country, $this->language),
            'subscribe_title' => ResourceHelper::getValue($this->attributes['subscribe_title'] ?? null, $this->country, $this->language),
            'subscribe_thanks_title' => ResourceHelper::getValue($this->attributes['subscribe_thanks_title'] ?? null, $this->country, $this->language),
            'subscribe_description' => ResourceHelper::getValue($this->attributes['subscribe_description'] ?? null, $this->country, $this->language),
            'subscribe_thanks_description' => ResourceHelper::getValue($this->attributes['subscribe_thanks_description'] ?? null, $this->country, $this->language),
            'subscribe_policy_text' => ResourceHelper::getValue($this->attributes['subscribe_policy_text'] ?? null, $this->country, $this->language),
            'show_more_button' => ResourceHelper::getValue($this->attributes['show_more_button'] ?? null, $this->country, $this->language),
//            'posts' => $posts ? PostCollection::make($posts->collect())->country($this->country)->language($this->language)->full(false) : null,
            'tags' => $keys
        ];
    }

}