Skip to content

Commit

Permalink
#213: use the ListItemContext for ListView::beforeItem and `ListV…
Browse files Browse the repository at this point in the history
…iew::afterItem` too.
  • Loading branch information
glpzzz committed Sep 10, 2024
1 parent 776dbc6 commit 1a20763
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/ListItemContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

final class ListItemContext
{

/**
* @param array|object $data The current data being rendered.
* @param int|string $key The key value associated with the current data.
* @param int $index The zero-based index of the data in the array.
* @param ListView $widget The list view object.
*/
public function __construct(
public readonly array|object $data,
public readonly int|string $key,
Expand Down
11 changes: 3 additions & 8 deletions src/ListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,9 @@ public function afterItem(Closure $value): self
* It should have the following signature:
*
* ```php
* function ($data, $key, $index, $widget)
* function (ListItemContext $context)
* ```
*
* - `$data`: The current data being rendered.
* - `$key`: The key value associated with the current data.
* - `$index`: The zero-based index of the data in the array.
* - `$widget`: The list view object.
*
* The return result of the function will be rendered directly.
*
* Note: If the function returns `null`, nothing will be rendered before the item.
Expand Down Expand Up @@ -325,7 +320,7 @@ private function renderAfterItem(ListItemContext $context): string
$result = '';

if (!empty($this->afterItem)) {
$result = (string)call_user_func($this->afterItem, $context->data, $context->key, $context->index, $context->widget);
$result = (string)call_user_func($this->afterItem, $context);
}

return $result;
Expand All @@ -347,7 +342,7 @@ private function renderBeforeItem(ListItemContext $context): string
$result = '';

if (!empty($this->beforeItem)) {
$result = (string)call_user_func($this->beforeItem, $context->data, $context->key, $context->index, $context->widget);
$result = (string)call_user_func($this->beforeItem, $context);
}

return $result;
Expand Down

0 comments on commit 1a20763

Please sign in to comment.