1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Yiisoft\Yii\DataView\Column; |
||||
6 | |||||
7 | use InvalidArgumentException; |
||||
8 | use Yiisoft\Arrays\ArrayHelper; |
||||
9 | use Yiisoft\Data\Paginator\OffsetPaginator; |
||||
10 | use Yiisoft\Data\Paginator\PaginatorInterface; |
||||
11 | use Yiisoft\Html\Tag\Input; |
||||
12 | use Yiisoft\Html\Tag\Select; |
||||
13 | use Yiisoft\Yii\DataView\Column\Base\Cell; |
||||
14 | use Yiisoft\Yii\DataView\Column\Base\DataContext; |
||||
15 | use Yiisoft\Yii\DataView\Column\Base\GlobalContext; |
||||
16 | use Yiisoft\Yii\DataView\Helper\Attribute; |
||||
17 | use Yiisoft\Yii\DataView\LinkSorter; |
||||
18 | |||||
19 | final class DataColumnRenderer implements ColumnRendererInterface |
||||
20 | { |
||||
21 | 2 | public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell |
|||
22 | { |
||||
23 | 2 | $this->checkColumn($column); |
|||
24 | 2 | return $cell->addAttributes($column->getColumnAttributes()); |
|||
25 | } |
||||
26 | |||||
27 | 67 | public function renderHeader(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell |
|||
28 | { |
||||
29 | 67 | $this->checkColumn($column); |
|||
30 | |||||
31 | 67 | $label = $column->getHeader() ?? ($column->getProperty() === null ? '' : ucfirst($column->getProperty())); |
|||
32 | |||||
33 | 67 | if ($column->getProperty() !== null && $column->isWithSorting()) { |
|||
34 | 67 | $linkSorter = $this->renderLinkSorter($context, $column->getProperty(), $label); |
|||
35 | 67 | if (!empty($linkSorter)) { |
|||
36 | 3 | return $cell->content($linkSorter)->encode(false); |
|||
37 | } |
||||
38 | } |
||||
39 | |||||
40 | 66 | return $cell |
|||
41 | 66 | ->addAttributes($column->getHeaderAttributes()) |
|||
42 | 66 | ->content($label); |
|||
43 | } |
||||
44 | |||||
45 | 68 | public function renderFilter(ColumnInterface $column, Cell $cell, GlobalContext $context): ?Cell |
|||
46 | { |
||||
47 | 68 | $this->checkColumn($column); |
|||
48 | |||||
49 | 68 | if ($column->getFilter() !== null) { |
|||
50 | 1 | $content = $column->getFilter(); |
|||
51 | 67 | } elseif ($column->getFilterProperty() !== null) { |
|||
52 | 17 | $content = match ($column->getFilterType()) { |
|||
53 | 17 | 'select' => $this->renderFilterSelect($column, $context), |
|||
54 | 17 | default => $this->renderFilterInput($column, $context), |
|||
55 | 17 | }; |
|||
56 | } else { |
||||
57 | 62 | return null; |
|||
58 | } |
||||
59 | |||||
60 | 18 | return $cell |
|||
61 | 18 | ->content($content) |
|||
62 | 18 | ->addAttributes($column->getFilterAttributes()) |
|||
63 | 18 | ->encode(false); |
|||
64 | } |
||||
65 | |||||
66 | 63 | public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell |
|||
67 | { |
||||
68 | 63 | $this->checkColumn($column); |
|||
69 | |||||
70 | 63 | $contentSource = $column->getContent(); |
|||
71 | |||||
72 | 63 | if ($contentSource !== null) { |
|||
73 | 5 | $content = (string)(is_callable($contentSource) ? $contentSource($context) : $contentSource); |
|||
74 | 59 | } elseif ($column->getProperty() !== null) { |
|||
75 | 59 | $content = (string)ArrayHelper::getValue($context->getData(), $column->getProperty()); |
|||
76 | } else { |
||||
77 | $content = ''; |
||||
78 | } |
||||
79 | |||||
80 | 63 | return $cell |
|||
81 | 63 | ->addAttributes($column->getBodyAttributes()) |
|||
82 | 63 | ->content($content); |
|||
83 | } |
||||
84 | |||||
85 | 2 | public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell |
|||
86 | { |
||||
87 | 2 | $this->checkColumn($column); |
|||
88 | |||||
89 | 2 | if ($column->getFooter() !== null) { |
|||
90 | 1 | $cell = $cell->content($column->getFooter()); |
|||
91 | } |
||||
92 | |||||
93 | 2 | return $cell; |
|||
94 | } |
||||
95 | |||||
96 | 67 | private function renderLinkSorter(GlobalContext $context, string $property, string $label): string |
|||
97 | { |
||||
98 | 67 | $dataReader = $context->getDataReader(); |
|||
99 | 67 | if (!$dataReader instanceof PaginatorInterface) { |
|||
100 | return ''; |
||||
101 | } |
||||
102 | |||||
103 | 67 | $sort = $dataReader->getSort(); |
|||
104 | 67 | if ($sort === null) { |
|||
105 | 64 | return ''; |
|||
106 | } |
||||
107 | |||||
108 | 3 | $linkSorter = $dataReader instanceof OffsetPaginator |
|||
109 | 2 | ? LinkSorter::widget()->currentPage($dataReader->getCurrentPage()) |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
110 | 1 | : LinkSorter::widget(); |
|||
111 | |||||
112 | 3 | return $linkSorter |
|||
113 | 3 | ->attribute($property) |
|||
0 ignored issues
–
show
The method
attribute() does not exist on Yiisoft\Widget\Widget . It seems like you code against a sub-type of Yiisoft\Widget\Widget such as Yiisoft\Yii\DataView\LinkSorter .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
114 | 3 | ->attributes($sort->getCriteria()) |
|||
115 | 3 | ->directions($sort->getOrder()) |
|||
116 | 3 | ->iconAscClass('bi bi-sort-alpha-up') |
|||
117 | 3 | ->iconDescClass('bi bi-sort-alpha-down') |
|||
118 | 3 | ->label($label) |
|||
119 | 3 | ->linkAttributes($context->getSortLinkAttributes()) |
|||
120 | 3 | ->pageSize($dataReader->getPageSize()) |
|||
121 | 3 | ->urlArguments($context->getUrlArguments()) |
|||
122 | 3 | ->urlQueryParameters($context->getUrlQueryParameters()) |
|||
123 | 3 | ->render(); |
|||
124 | } |
||||
125 | |||||
126 | 16 | private function renderFilterInput(DataColumn $column, GlobalContext $context): string |
|||
127 | { |
||||
128 | 16 | $filterInputAttributes = $column->getFilterInputAttributes(); |
|||
129 | 16 | $filterInputTag = Input::tag(); |
|||
130 | |||||
131 | 16 | if (!array_key_exists('name', $filterInputAttributes)) { |
|||
132 | 16 | $filterInputTag = $filterInputTag->name( |
|||
133 | 16 | Attribute::getInputName( |
|||
134 | 16 | (string)($column->getFilterModelName() ?? $context->getFilterModelName()), |
|||
135 | 16 | $column->getFilterProperty() ?? '' |
|||
136 | 16 | ), |
|||
137 | 16 | ); |
|||
138 | } |
||||
139 | |||||
140 | 16 | if (!array_key_exists('value', $filterInputAttributes) && $column->getFilterValueDefault() !== '') { |
|||
141 | 16 | $filterInputTag = $filterInputTag->value($column->getFilterValueDefault()); |
|||
142 | } |
||||
143 | |||||
144 | 16 | return $filterInputTag |
|||
145 | 16 | ->addAttributes($filterInputAttributes) |
|||
146 | 16 | ->type($column->getFilterType()) |
|||
147 | 16 | ->render(); |
|||
148 | } |
||||
149 | |||||
150 | 1 | private function renderFilterSelect(DataColumn $column, GlobalContext $context): string |
|||
151 | { |
||||
152 | 1 | $filterInputAttributes = $column->getFilterInputAttributes(); |
|||
153 | 1 | $filterSelectTag = Select::tag(); |
|||
154 | |||||
155 | 1 | if (!array_key_exists('name', $filterInputAttributes)) { |
|||
156 | 1 | $filterSelectTag = $filterSelectTag->name( |
|||
157 | 1 | Attribute::getInputName( |
|||
158 | 1 | (string)($column->getFilterModelName() ?? $context->getFilterModelName()), |
|||
159 | 1 | $column->getFilterProperty() ?? '' |
|||
160 | 1 | ), |
|||
161 | 1 | ); |
|||
162 | } |
||||
163 | |||||
164 | 1 | if ($column->getFilterValueDefault() !== null) { |
|||
165 | 1 | $filterSelectTag = $filterSelectTag->value($column->getFilterValueDefault()); |
|||
166 | } |
||||
167 | |||||
168 | 1 | return $filterSelectTag |
|||
169 | 1 | ->addAttributes($filterInputAttributes) |
|||
170 | 1 | ->optionsData($column->getFilterInputSelectItems()) |
|||
171 | 1 | ->prompt($column->getFilterInputSelectPrompt()) |
|||
172 | 1 | ->render(); |
|||
173 | } |
||||
174 | |||||
175 | /** |
||||
176 | * @psalm-assert DataColumn $column |
||||
177 | */ |
||||
178 | 68 | private function checkColumn(ColumnInterface $column): void |
|||
179 | { |
||||
180 | 68 | if (!$column instanceof DataColumn) { |
|||
181 | throw new InvalidArgumentException( |
||||
182 | sprintf( |
||||
183 | 'Expected "%s", but "%s" given.', |
||||
184 | DataColumn::class, |
||||
185 | $column::class |
||||
186 | ) |
||||
187 | ); |
||||
188 | } |
||||
189 | } |
||||
190 | } |
||||
191 |