Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Column
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 format
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Grid;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * DataGrid Column Value Object.
13 *
14 * Encapsulates column properties including data key, label, sortability, filterability, and custom formatting.
15 *
16 * @package App\Core\Grid
17 */
18final readonly class Column
19{
20    /**
21     * Column constructor.
22     *
23     * @param string $key Field key in database dataset.
24     * @param string $label Human-readable column header text.
25     * @param bool $isSortable True if column supports sorting.
26     * @param bool $isFilterable True if column supports input filtering.
27     * @param callable|null $formatter Custom row value formatting closure.
28     * @param string|null $selectExpression SQL select expression (e.g. 'l.user_id' or 'u.username').
29     * @param string|null $filterExpression SQL filter expression (e.g. 'u.username LIKE :filter_key').
30     * @param FilterType $filterType UI control type for filtering (TEXT, SELECT, AUTOCOMPLETE).
31     * @param string|null $autocompleteUrl API Endpoint for Autocomplete suggestions.
32     * @param array<string|int, string>|null $filterOptions Select dropdown options [value => label].
33     */
34    public function __construct(
35        public string $key,
36        public string $label,
37        public bool $isSortable = true,
38        public bool $isFilterable = true,
39        public mixed $formatter = null,
40        public ?string $selectExpression = null,
41        public ?string $filterExpression = null,
42        public FilterType $filterType = FilterType::TEXT,
43        public ?string $autocompleteUrl = null,
44        public ?array $filterOptions = null
45    ) {
46    }
47
48    /**
49     * Formats a raw row value using custom formatter closure if available.
50     *
51     * @param mixed $value Raw database field value.
52     * @param array<string, mixed> $row Entire database row array.
53     * @return string Formatted HTML or text string.
54     */
55    public function format(mixed $value, array $row): string
56    {
57        if (is_callable($this->formatter)) {
58            return (string)call_user_func($this->formatter, $value, $row);
59        }
60
61        return (string)($value ?? '');
62    }
63}