Skip to content

Commit 7d1824f

Browse files
committed
#213: use ListItemContext for ListView::renderItem related operations.
1 parent e9eec42 commit 7d1824f

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

src/ListItemContext.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Yiisoft\Yii\DataView;
4+
5+
final class ListItemContext
6+
{
7+
8+
public function __construct(
9+
public readonly array|object $data,
10+
public readonly int|string $key,
11+
public readonly int $index,
12+
public readonly ListView $widget,
13+
)
14+
{
15+
}
16+
}

src/ListView.php

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@ public function itemViewTag(?string $tag): self
173173
* signature:
174174
*
175175
* ```php
176-
* function ($data, $key, $index, $widget)
176+
* function (ListItemContext $context)
177177
* ```
178178
* Also, each attribute value can be a function too, with the same signature as in:
179179
*
180180
* ```php
181181
* [
182-
* 'class' => static fn($data, $key, $index, $widget) => "custom-class-{$data['id']}",
182+
* 'class' => static fn(ListItemContext $context) => "custom-class-{$context->data['id']}",
183183
* ]
184184
* ```
185185
* @return ListView
@@ -223,13 +223,11 @@ public function viewParams(array $viewParams): self
223223
/**
224224
* Renders a single data model.
225225
*
226-
* @param array|object $data The data to be rendered.
227-
* @param mixed $key The key value associated with the data.
228-
* @param int $index The zero-based index of the data array.
226+
* @param ListItemContext $context
229227
*
230228
* @throws ViewNotFoundException If the item view file doesn't exist.
231229
*/
232-
protected function renderItem(array|object $data, mixed $key, int $index): string
230+
protected function renderItem(ListItemContext $context): string
233231
{
234232
$content = '';
235233

@@ -242,9 +240,9 @@ protected function renderItem(array|object $data, mixed $key, int $index): strin
242240
$this->itemView,
243241
array_merge(
244242
[
245-
'data' => $data,
246-
'index' => $index,
247-
'key' => $key,
243+
'data' => $context->data,
244+
'index' => $context->index,
245+
'key' => $context->key,
248246
'widget' => $this,
249247
],
250248
$this->viewParams
@@ -253,16 +251,16 @@ protected function renderItem(array|object $data, mixed $key, int $index): strin
253251
}
254252

255253
if ($this->itemView instanceof Closure) {
256-
$content = (string)call_user_func($this->itemView, $data, $key, $index, $this);
254+
$content = (string)call_user_func($this->itemView, $context);
257255
}
258256

259257
$itemViewAttributes = is_callable($this->itemViewAttributes)
260-
? (array)call_user_func($this->itemViewAttributes, $data, $key, $index, $this)
258+
? (array)call_user_func($this->itemViewAttributes, $context)
261259
: $this->itemViewAttributes;
262260

263261
foreach ($itemViewAttributes as $i => $attribute) {
264262
if (is_callable($attribute)) {
265-
$itemViewAttributes[$i] = $attribute($data, $key, $index, $this);
263+
$itemViewAttributes[$i] = $attribute($context);
266264
}
267265
}
268266

@@ -289,13 +287,15 @@ protected function renderItems(array $items, \Yiisoft\Validator\Result $filterVa
289287
foreach (array_values($items) as $index => $value) {
290288
$key = $keys[$index];
291289

292-
if ('' !== ($before = $this->renderBeforeItem($value, $key, $index))) {
290+
$itemContext = new ListItemContext($value, $key, $index, $this);
291+
292+
if ('' !== ($before = $this->renderBeforeItem($itemContext))) {
293293
$rows[] = $before;
294294
}
295295

296-
$rows[] = $this->renderItem($value, $key, $index);
296+
$rows[] = $this->renderItem($itemContext);
297297

298-
if ('' !== ($after = $this->renderAfterItem($value, $key, $index))) {
298+
if ('' !== ($after = $this->renderAfterItem($itemContext))) {
299299
$rows[] = $after;
300300
}
301301
}
@@ -314,20 +314,18 @@ protected function renderItems(array $items, \Yiisoft\Validator\Result $filterVa
314314
*
315315
* If {@see afterItem} is not a closure, `null` will be returned.
316316
*
317-
* @param array|object $data The data to be rendered.
318-
* @param mixed $key The key value associated with the data.
319-
* @param int $index The zero-based index of the data.
317+
* @param ListItemContext $context context of the item to be rendered.
320318
*
321319
* @return string call result when {@see afterItem} is not a closure.
322320
*
323321
* {@see afterItem}
324322
*/
325-
private function renderAfterItem(array|object $data, mixed $key, int $index): string
323+
private function renderAfterItem(ListItemContext $context): string
326324
{
327325
$result = '';
328326

329327
if (!empty($this->afterItem)) {
330-
$result = (string)call_user_func($this->afterItem, $data, $key, $index, $this);
328+
$result = (string)call_user_func($this->afterItem, $context->data, $context->key, $context->index, $context->widget);
331329
}
332330

333331
return $result;
@@ -338,20 +336,18 @@ private function renderAfterItem(array|object $data, mixed $key, int $index): st
338336
*
339337
* If {@see beforeItem} is not a closure, `null` will be returned.
340338
*
341-
* @param array|object $data The data to be rendered.
342-
* @param mixed $key The key value associated with the data.
343-
* @param int $index The zero-based index of the data.
339+
* @param ListItemContext $context context of the item to be rendered.
344340
*
345341
* @return string call result or `null` when {@see beforeItem} is not a closure.
346342
*
347343
* {@see beforeItem}
348344
*/
349-
private function renderBeforeItem(array|object $data, mixed $key, int $index): string
345+
private function renderBeforeItem(ListItemContext $context): string
350346
{
351347
$result = '';
352348

353349
if (!empty($this->beforeItem)) {
354-
$result = (string)call_user_func($this->beforeItem, $data, $key, $index, $this);
350+
$result = (string)call_user_func($this->beforeItem, $context->data, $context->key, $context->index, $context->widget);
355351
}
356352

357353
return $result;

tests/ListView/BaseTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Yiisoft\Definitions\Exception\InvalidConfigException;
1010
use Yiisoft\Definitions\Exception\NotInstantiableException;
1111
use Yiisoft\Factory\NotFoundException;
12+
use Yiisoft\Yii\DataView\ListItemContext;
1213
use Yiisoft\Yii\DataView\ListView;
1314
use Yiisoft\Yii\DataView\Tests\Support\Assert;
1415
use Yiisoft\Yii\DataView\Tests\Support\TestTrait;
@@ -122,7 +123,7 @@ public function testItemViewAsCallable(): void
122123
HTML,
123124
ListView::widget()
124125
->itemView(
125-
fn (array $data) => '<div>' . $data['id'] . '</div><div>' . $data['name'] . '</div>' . PHP_EOL
126+
fn (ListItemContext $context) => '<div>' . $context->data['id'] . '</div><div>' . $context->data['name'] . '</div>' . PHP_EOL
126127
)
127128
->dataReader($this->createOffsetPaginator($this->data, 10))
128129
->render(),
@@ -426,10 +427,10 @@ public function testClosureForItemViewAttributes(): void
426427
HTML,
427428
ListView::widget()
428429
->itemView(dirname(__DIR__) . '/Support/view/_listview.php')
429-
->itemViewAttributes(static fn (array $data, $key, $index, $widget) => [
430-
'data-item-id' => $data['id'],
431-
'data-item-key' => $key,
432-
'data-item-index' => $index,
430+
->itemViewAttributes(static fn (ListItemContext $context) => [
431+
'data-item-id' => $context->data['id'],
432+
'data-item-key' => $context->key,
433+
'data-item-index' => $context->index,
433434
])
434435
->dataReader($this->createOffsetPaginator($this->data, 10))
435436
->separator(PHP_EOL)
@@ -456,7 +457,7 @@ public function testItemViewAttributesWithClosure(): void
456457
ListView::widget()
457458
->itemView(dirname(__DIR__) . '/Support/view/_listview.php')
458459
->itemViewAttributes([
459-
'class' => static fn(array $data, $key, $index) => "id-{$data['id']}-key-{$key}-index-{$index}",
460+
'class' => static fn(ListItemContext $context) => "id-{$context->data['id']}-key-{$context->key}-index-{$context->index}",
460461
])
461462
->dataReader($this->createOffsetPaginator($this->data, 10))
462463
->separator(PHP_EOL)
@@ -482,8 +483,8 @@ public function testClosureForItemViewAttributesWithClosure(): void
482483
HTML,
483484
ListView::widget()
484485
->itemView(dirname(__DIR__) . '/Support/view/_listview.php')
485-
->itemViewAttributes(static fn (array $data, $key, $index) => [
486-
'class' => static fn(array $data, $key, $index) => "id-{$data['id']}-key-{$key}-index-{$index}",
486+
->itemViewAttributes(static fn (ListItemContext $context) => [
487+
'class' => static fn(ListItemContext $context) => "id-{$context->data['id']}-key-{$context->key}-index-{$context->index}",
487488
])
488489
->dataReader($this->createOffsetPaginator($this->data, 10))
489490
->separator(PHP_EOL)

0 commit comments

Comments
 (0)