1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui\Table\Column;
6:
7: use Atk4\Data\Field;
8: use Atk4\Data\Model;
9: use Atk4\Ui\Table;
10:
11: class CountryFlag extends Table\Column
12: {
13: /** Name of country code model field (in ISO 3166-1 alpha-2 format) */
14: public ?string $codeField = null;
15:
16: /** Optional name of model field which contains full country name. */
17: public ?string $nameField = null;
18:
19: #[\Override]
20: public function getHtmlTags(Model $row, ?Field $field): array
21: {
22: $countryCode = $row->get($this->codeField ?? $field->shortName);
23: $countryName = $this->nameField ? $row->get($this->nameField) : null;
24:
25: return [
26: $field->shortName => $countryCode === null
27: ? ''
28: : $this->getApp()->getTag('i', [
29: 'class' => strtolower($countryCode) . ' flag',
30: 'title' => strtoupper($countryCode) . ($countryName === null ? '' : ' - ' . $countryName),
31: ]),
32: ];
33: }
34: }
35: