Skip to content

Commit 47d37ba

Browse files
authored
Merge pull request #250 from bscheshirwork/accept-traversable-table-renderer
Refactoring to support generators
2 parents de781d6 + 567ecb9 commit 47d37ba

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Yii2 multiple input change log
44
2.18.0 (in development)
55
=======================
66
- #246 accept `\Traversable` in model attribute for `yield` compatibility (bscheshirwork)
7+
- #250 accept `\Traversable` in TableRenderer and ListRenderer for `yield` compatibility (bscheshirwork)
78

89
2.17.0
910
======

src/renderers/ListRenderer.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,16 @@ protected function renderBody()
105105
$rows = [];
106106

107107
if ($this->data) {
108-
$cnt = count($this->data);
109-
if ($this->min === $this->max && $cnt < $this->max) {
110-
$cnt = $this->max;
108+
$j = 0;
109+
foreach ($this->data as $index => $item) {
110+
if ($j++ <= $this->max) {
111+
$rows[] = $this->renderRowContent($index, $item);
112+
} else {
113+
break;
114+
}
111115
}
112-
113-
$indices = array_keys($this->data);
114-
115-
for ($i = 0; $i < $cnt; $i++) {
116-
$index = ArrayHelper::getValue($indices, $i, $i);
117-
$item = ArrayHelper::getValue($this->data, $index, null);
118-
$rows[] = $this->renderRowContent($index, $item);
116+
for ($i = $j; $i < $this->min; $i++) {
117+
$rows[] = $this->renderRowContent($i);
119118
}
120119
} elseif ($this->min > 0) {
121120
for ($i = 0; $i < $this->min; $i++) {

src/renderers/TableRenderer.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,16 @@ protected function renderBody()
170170
$rows = [];
171171

172172
if ($this->data) {
173-
$cnt = count($this->data);
174-
if ($this->min === $this->max && $cnt < $this->max) {
175-
$cnt = $this->max;
173+
$j = 0;
174+
foreach ($this->data as $index => $item) {
175+
if ($j++ <= $this->max) {
176+
$rows[] = $this->renderRowContent($index, $item);
177+
} else {
178+
break;
179+
}
176180
}
177-
178-
$indices = array_keys($this->data);
179-
180-
for ($i = 0; $i < $cnt; $i++) {
181-
$index = ArrayHelper::getValue($indices, $i, $i);
182-
$item = ArrayHelper::getValue($this->data, $index, null);
183-
$rows[] = $this->renderRowContent($index, $item);
181+
for ($i = $j; $i < $this->min; $i++) {
182+
$rows[] = $this->renderRowContent($i);
184183
}
185184
} elseif ($this->min > 0) {
186185
for ($i = 0; $i < $this->min; $i++) {

0 commit comments

Comments
 (0)