Skip to content

Commit 4977fa1

Browse files
authored
Change AbstractAdapter to use iterator instead of array (#371)
1 parent 56c3f34 commit 4977fa1

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

src/Adapter/AbstractAdapter.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,33 @@ final public function getData(DataTableState $state): ResultSetInterface
3838
$this->prepareQuery($query);
3939
$propertyMap = $this->getPropertyMap($query);
4040

41-
$rows = [];
4241
$transformer = $state->getDataTable()->getTransformer();
4342
$identifier = $query->getIdentifierPropertyPath();
44-
foreach ($this->getResults($query) as $result) {
45-
$row = [];
46-
if (!empty($identifier)) {
47-
$row['DT_RowId'] = $this->accessor->getValue($result, $identifier);
48-
}
4943

50-
/** @var AbstractColumn $column */
51-
foreach ($propertyMap as list($column, $mapping)) {
52-
$value = ($mapping && $this->accessor->isReadable($result, $mapping)) ? $this->accessor->getValue($result, $mapping) : null;
53-
$row[$column->getName()] = $column->transform($value, $result);
54-
}
55-
if (null !== $transformer) {
56-
$row = call_user_func($transformer, $row, $result);
44+
$data = (function () use ($query, $identifier, $transformer, $propertyMap) {
45+
foreach ($this->getResults($query) as $result) {
46+
$row = [];
47+
if (!empty($identifier)) {
48+
$row['DT_RowId'] = $this->accessor->getValue($result, $identifier);
49+
}
50+
51+
/** @var AbstractColumn $column */
52+
foreach ($propertyMap as list($column, $mapping)) {
53+
$value = ($mapping && $this->accessor->isReadable($result, $mapping)) ? $this->accessor->getValue($result, $mapping) : null;
54+
$row[$column->getName()] = $column->transform($value, $result);
55+
}
56+
if (null !== $transformer) {
57+
$row = call_user_func($transformer, $row, $result);
58+
}
59+
yield $row;
5760
}
58-
$rows[] = $row;
61+
})();
62+
63+
if (null === $query->getTotalRows() || null === $query->getFilteredRows()) {
64+
throw new \LogicException('Adapter did not set row counts');
5965
}
6066

61-
return new ArrayResultSet($rows, $query->getTotalRows(), $query->getFilteredRows());
67+
return new ResultSet($data, $query->getTotalRows(), $query->getFilteredRows());
6268
}
6369

6470
/**

src/Adapter/ArrayAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getData(DataTableState $state): ResultSetInterface
6969
$length = $state->getLength() ?? 0;
7070
$page = $length > 0 ? array_slice($data, $state->getStart(), $state->getLength()) : $data;
7171

72-
return new ArrayResultSet($page, count($this->data), count($data));
72+
return new ResultSet(new \ArrayIterator($page), count($this->data), count($data));
7373
}
7474

7575
/**

src/Adapter/ArrayResultSet.php renamed to src/Adapter/ResultSet.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,22 @@
1313
namespace Omines\DataTablesBundle\Adapter;
1414

1515
/**
16-
* ArrayResultSet.
17-
*
1816
* @author Niels Keurentjes <[email protected]>
1917
*
20-
* @phpstan-type Row array<string, mixed>
18+
* @phpstan-type Row array<(int|string), mixed>
19+
*
20+
* (Note: when using ArrayAdapter, the Row keys may be integers instead of strings.)
2121
*/
22-
class ArrayResultSet implements ResultSetInterface
22+
class ResultSet implements ResultSetInterface
2323
{
24-
/** @var Row[] */
25-
private array $data;
26-
private int $totalRows;
27-
private int $totalFilteredRows;
28-
2924
/**
30-
* @param Row[] $data
25+
* @param \Iterator<Row> $data
3126
*/
32-
public function __construct(array $data, ?int $totalRows = null, ?int $totalFilteredRows = null)
33-
{
34-
$this->data = $data;
35-
$this->totalRows = $totalRows ?? count($data);
36-
$this->totalFilteredRows = $totalFilteredRows ?? $this->totalRows;
27+
public function __construct(
28+
private readonly \Iterator $data,
29+
private readonly int $totalRows,
30+
private readonly int $totalFilteredRows,
31+
) {
3732
}
3833

3934
public function getTotalRecords(): int
@@ -48,6 +43,6 @@ public function getTotalDisplayRecords(): int
4843

4944
public function getData(): \Iterator
5045
{
51-
return new \ArrayIterator($this->data);
46+
return $this->data;
5247
}
5348
}

tests/Unit/Adapter/ORMAdapterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function testCountGroupedDataTable(): void
4141
/** @var ORMAdapter $adapter */
4242
$adapter = $datatable->getAdapter();
4343
$data = $adapter->getData(new DataTableState($datatable));
44+
iterator_to_array($data->getData()); // Evaluate the iterator to trigger the exception
4445
}
4546

4647
public function testORMAdapterQueryEvent(): void

0 commit comments

Comments
 (0)