<?php

declare(strict_types=1);

namespace KupShop\ContentBundle\Entity;

class Placeholder implements \JsonSerializable
{
    protected string $valueCache;

    public function __construct(
        public string $code,
        public string $description,
        public \Closure $loader,
    ) {
    }

    public function getValue(): string
    {
        if (!isset($this->valueCache)) {
            $this->valueCache = (string) ($this->loader)() ?? '';
        }

        return $this->valueCache;
    }

    public function __serialize(): array
    {
        return [
            'code' => $this->code,
            'description' => $this->description,
            'value' => $this->getValue(),
        ];
    }

    public function jsonSerialize(): mixed
    {
        return $this->__serialize();
    }
}
