<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Entities\Zaznam\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\DruhSubjektuZaznamuEnum;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\Subjekt;

/**
 * Class SubjektCast
 *
 * @package Seka\Portal\Modul\Statistiky\Entities\Zaznam\Casts
 */
final class SubjektCast implements CastsAttributes
{
    /**
     * @var string
     */
    protected string $druh;

    /**
     * @var string
     */
    protected string $nazov;

    /**
     * SubjektCast constructor.
     *
     * @param string $druh
     * @param string $nazov
     */
    public function __construct(string $druh, string $nazov)
    {
        $this->druh = $druh;
        $this->nazov = $nazov;
    }

    /**
     * @param \Illuminate\Database\Eloquent\Model $model
     * @param string                              $key
     * @param mixed                               $value
     * @param array                               $attributes
     *
     * @return \Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\Subjekt
     */
    public function get($model, string $key, $value, array $attributes): Subjekt
    {
        return new Subjekt(
            DruhSubjektuZaznamuEnum::from($attributes[$this->druh]),
            $attributes[$this->nazov]
        );
    }

    /**
     * @param \Illuminate\Database\Eloquent\Model $model
     * @param string                              $key
     * @param mixed                               $value
     * @param array                               $attributes
     *
     * @return array
     */
    public function set($model, string $key, $value, array $attributes): array
    {
        return [
            $this->druh => $value->druh()->value,
            $this->nazov => $value->nazov(),
        ];
    }
}
