formatStateUsing(static function (Column $column, $state) use ($format, $timezone): ?string { /** @var TextColumn $column */ if (blank($state)) { return null; } return Carbon::parse($state) ->setTimezone($timezone ?? $column->getTimezone()) ->translatedFormat($format); }); return $this; } public function dateTime(?string $format = null, ?string $timezone = null): static { $format ??= config('tables.date_time_format'); $this->date($format, $timezone); return $this; } public function since(?string $timezone = null): static { $this->formatStateUsing(static function (Column $column, $state) use ($timezone): ?string { /** @var TextColumn $column */ if (blank($state)) { return null; } return Carbon::parse($state) ->setTimezone($timezone ?? $column->getTimezone()) ->diffForHumans(); }); return $this; } public function enum(array | Arrayable $options, $default = null): static { $this->formatStateUsing(static fn ($state): ?string => $options[$state] ?? ($default ?? $state)); return $this; } public function limit(int $length = 100, string $end = '...'): static { $this->limit = $length; $this->formatStateUsing(static function ($state) use ($length, $end): ?string { if (blank($state)) { return null; } return Str::limit($state, $length, $end); }); return $this; } public function words(int $words = 100, string $end = '...'): static { $this->formatStateUsing(static function ($state) use ($words, $end): ?string { if (blank($state)) { return null; } return Str::words($state, $words, $end); }); return $this; } public function prefix(string | Closure $prefix): static { $this->prefix = $prefix; return $this; } public function suffix(string | Closure $suffix): static { $this->suffix = $suffix; return $this; } public function placeholder(string | Closure | null $placeholder): static { $this->placeholder = $placeholder; return $this; } public function html(): static { return $this->formatStateUsing(static fn ($state): HtmlString => $state instanceof HtmlString ? $state : Str::of($state)->sanitizeHtml()->toHtmlString()); } public function formatStateUsing(?Closure $callback): static { $this->formatStateUsing = $callback; return $this; } public function money(string | Closure | null $currency = null, bool $shouldConvert = false): static { $this->formatStateUsing(static function (Column $column, $state) use ($currency, $shouldConvert): ?string { if (blank($state)) { return null; } if (blank($currency)) { $currency = env('DEFAULT_CURRENCY', 'USD'); } return (new Money\Money( $state, (new Money\Currency(strtoupper($column->evaluate($currency)))), $shouldConvert, ))->format(); }); return $this; } public function getFormattedState() { $state = $this->getState(); if ($this->formatStateUsing) { $state = $this->evaluate($this->formatStateUsing, [ 'state' => $state, ]); } if ($this->prefix) { $state = $this->evaluate($this->prefix) . $state; } if ($this->suffix) { $state = $state . $this->evaluate($this->suffix); } if (blank($state)) { $state = $this->evaluate($this->placeholder); } return $state; } public function getLimit(): ?int { return $this->limit; } public function time(?string $format = null, ?string $timezone = null): static { $format ??= config('tables.time_format'); $this->date($format, $timezone); return $this; } public function timezone(string | Closure | null $timezone): static { $this->timezone = $timezone; return $this; } public function getTimezone(): string { return $this->evaluate($this->timezone) ?? config('app.timezone'); } }