summaryrefslogtreecommitdiff
path: root/src/Toolbar/Concerns/WithAction.php
blob: a1c33f21930127a5838b58d0a96c8007c77d6e26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

declare(strict_types=1);

namespace Lightscale\LaralightTables\Toolbar\Concerns;

trait WithAction
{
    protected ?string $action = null;

    /**
     * @param  string|array{0: string, ...<int, string|numeric|bool|null>}  $action
     */
    public function action(string|array $action): static
    {
        if (is_array($action)) {
            $fn = array_shift($action);
            $params = collect($action)->map(
                fn ($v) => match (strtolower(gettype($v))) {
                    'string' => "'{$v}'",
                    'boolean' => $v ? 'true' : 'false',
                    'null' => 'null',
                    default => (string) $v,
                }
            )->join(', ');
            $this->action = "{$fn}({$params})";
        } else {
            $this->action = $action;
        }

        return $this;
    }
}