Skip to content

Commit 0c76ee6

Browse files
committed
Prepare interfaces for lazy result sets
1 parent 87e1ee3 commit 0c76ee6

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "omines/datatables-bundle",
33
"type": "symfony-bundle",
44
"description": "Symfony DataTables Bundle",
5-
"keywords": ["symfony","jquery","datatable", "datatables", "bundle"],
5+
"keywords": ["symfony","jquery","datatable", "datatables", "bundle", "doctrine", "orm"],
66
"license": "MIT",
77
"authors": [
88
{

src/Adapter/ArrayAdapter.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Omines\DataTablesBundle\DataTableState;
1616
use Symfony\Component\PropertyAccess\PropertyAccess;
17+
use Symfony\Component\PropertyAccess\PropertyAccessor;
1718

1819
/**
1920
* ArrayAdapter.
@@ -25,12 +26,16 @@ class ArrayAdapter implements AdapterInterface
2526
/** @var array */
2627
private $data = [];
2728

29+
/** @var PropertyAccessor */
30+
private $accessor;
31+
2832
/**
2933
* {@inheritdoc}
3034
*/
3135
public function configure(array $options)
3236
{
3337
$this->data = $options;
38+
$this->accessor = PropertyAccess::createPropertyAccessor();
3439
}
3540

3641
/**
@@ -65,26 +70,37 @@ public function getData(DataTableState $state): ResultSetInterface
6570
protected function processData(DataTableState $state, array $data, array $map)
6671
{
6772
$transformer = $state->getDataTable()->getTransformer();
68-
$search = $state->getGlobalSearch() ?: null;
69-
$accessor = PropertyAccess::createPropertyAccessor();
73+
$search = $state->getGlobalSearch() ?: '';
7074
foreach ($data as $result) {
71-
$row = [];
72-
$match = (null === $search);
73-
foreach ($state->getDataTable()->getColumns() as $column) {
74-
$value = (!empty($propertyPath = $map[$column->getName()]) && $accessor->isReadable($result, $propertyPath)) ? $accessor->getValue($result, $propertyPath) : null;
75-
$value = $column->transform($value, $result);
76-
if (!$match) {
77-
$match = (false !== mb_stripos($value, $search));
75+
if ($row = $this->processRow($state, $result, $map, $search)) {
76+
if ($transformer) {
77+
$row = call_user_func($transformer, $row, $result);
7878
}
79-
$row[$column->getName()] = $column->transform($value, $result);
79+
yield $row;
8080
}
81+
}
82+
}
83+
84+
/**
85+
* @param DataTableState $state
86+
* @param array $result
87+
* @param array $map
88+
* @param string $search
89+
* @return array|null
90+
*/
91+
protected function processRow(DataTableState $state, array $result, array $map, string $search)
92+
{
93+
$row = [];
94+
$match = empty($search);
95+
foreach ($state->getDataTable()->getColumns() as $column) {
96+
$value = (!empty($propertyPath = $map[$column->getName()]) && $this->accessor->isReadable($result, $propertyPath)) ? $this->accessor->getValue($result, $propertyPath) : null;
97+
$value = $column->transform($value, $result);
8198
if (!$match) {
82-
continue;
83-
}
84-
if ($transformer) {
85-
$row = call_user_func($transformer, $row, $result);
99+
$match = (false !== mb_stripos($value, $search));
86100
}
87-
yield $row;
101+
$row[$column->getName()] = $column->transform($value, $result);
88102
}
103+
104+
return $match ? $row : null;
89105
}
90106
}

src/Adapter/ArrayResultSet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public function getTotalDisplayRecords(): int
6161
/**
6262
* {@inheritdoc}
6363
*/
64-
public function getData(): array
64+
public function getData(): \Iterator
6565
{
66-
return $this->data;
66+
return new \ArrayIterator($this->data);
6767
}
6868
}

src/Adapter/ResultSetInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public function getTotalDisplayRecords(): int;
3535

3636
/**
3737
* Returns the raw data in the result set.
38-
* @return array
38+
*
39+
* @return \Iterator
3940
*/
40-
public function getData(): array;
41+
public function getData(): \Iterator;
4142
}

src/DataTable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class DataTable
9797
/** @var DataTableState */
9898
private $state;
9999

100-
/** @var null|Instantiator */
100+
/** @var Instantiator */
101101
private $instantiator;
102102

103103
/**
@@ -299,7 +299,7 @@ public function getResponse(): JsonResponse
299299
'draw' => $this->state->getDraw(),
300300
'recordsTotal' => $resultSet->getTotalRecords(),
301301
'recordsFiltered' => $resultSet->getTotalDisplayRecords(),
302-
'data' => $resultSet->getData(),
302+
'data' => iterator_to_array($resultSet->getData()),
303303
];
304304
if ($this->state->isInitial()) {
305305
$response['options'] = $this->getInitialResponse();

0 commit comments

Comments
 (0)