<?php

namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\Carbon;

class DocDataCast implements CastsAttributes
{
    /**
     * Cast the stored value to a PHP array.
     *
     * @param mixed $value
     * @return array|null
     */
    public function get($model, string $key, $value, array $attributes)
    {
        // If the value is null, return null
        if ($value === null) {
            return null;
        }

        // Remove curly braces and return as string
        return trim($value, '{}');
    }

    /**
     * Prepare the given value for storage.
     *
     * @param mixed $value
     * @return string|null
     */
    public function set($model, string $key, $value, array $attributes)
    {
        // If the value is already null, return null
        if ($value === null) {
            return null;
        }

        // Assuming $value is a string, you might need to process or validate it here
        // For example, validate format or parse if necessary

        return '{' . $value . '}';
    }
}
