<?php

declare(strict_types=1);

namespace apexl\Io\modules\tabulator\Tabulatable\ColumnCallback;

use apexl\Io\modules\tabulator\Interfaces\HasTabulatorViewInterface;
use apexl\Io\modules\tabulator\Interfaces\TabulatableColumnCallback;

final readonly class SimpleMethod implements TabulatableColumnCallback
{
    public function __construct(private string $method, private null|array|int|float|string $default = null) {}

    public function __invoke(HasTabulatorViewInterface $entity, string $field): mixed
    {
        $value = null;
        if (method_exists($entity, $this->method)) {
            $value = $entity->{$this->method}();
        }

        return $value ?? $this->default;
    }
}
