/home/bdqbpbxa/api-uniferx.goodface.com.ua/app/Http/Resources/Pages/Banners/BannerResource.php
<?php
namespace App\Http\Resources\Pages\Banners;

use App\Helpers\ResourceHelper;
use App\Http\Resources\Pages\Contacts\RegionCollection;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @OA\Schema(title="BannerResource",description="Banner resource",
 *     @OA\Xml(name="BannerResource")
 * )
 */
class BannerResource extends JsonResource
{

    /**
     * @OA\Property(property="title", type="string", example="Kalyna farming enterprise  Ternopil region, 2023"),
     * @OA\Property(property="file", type="string", example="https://uniferx-backend.test/storage/crops/trials/X2YZbZ7anmN5W3QdUn3iJEgacNxdXNKP1KTQakt8.txt"),
     */
    public string|null $language;
    public string|null $country;
    public array|null $data;

    public function language($value): static
    {
        $this->language = $value;
        return $this;
    }

    public function country($value): static
    {
        $this->country = $value;
        return $this;
    }

    public function __construct($resource)
    {
        parent::__construct($resource);
        $this->data = $resource['attributes'];
    }



    public function toArray(Request $request): array
    {
        return [
            'title' => ResourceHelper::getValue($this->data['title'] ?? null, $this->country, $this->language),
            'text' => ResourceHelper::getValue($this->data['text'] ?? null, $this->country, $this->language),
            'icon' => ResourceHelper::getImage($this->data['icon'] ?? null),
        ];
    }

    public static function collection($resource): BannerCollection
    {
        return new BannerCollection($resource);
    }
}