Skip to content

Commit 356f4b2

Browse files
NMe84curry684
andauthored
Move logic to makes queries without limits to the relevant adapters (#257)
* Update documentation to include info on the Exporter * Refactor exporter and adapters to support unlimited results * Make setStart sanitation more intuitive * Update MongoDBAdapter.php Make MongoAdapter behavior for length NULL consistent with other adapters Co-authored-by: Niels Keurentjes <[email protected]>
1 parent 2608312 commit 356f4b2

File tree

8 files changed

+30
-43
lines changed

8 files changed

+30
-43
lines changed

src/Adapter/ArrayAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function getData(DataTableState $state): ResultSetInterface
7272

7373
$data = iterator_to_array($this->processData($state, $this->data, $map));
7474

75-
$length = $state->getLength();
75+
$length = $state->getLength() ?? 0;
7676
$page = $length > 0 ? array_slice($data, $state->getStart(), $state->getLength()) : $data;
7777

7878
return new ArrayResultSet($page, count($this->data), count($data));

src/Adapter/Doctrine/FetchJoinORMAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function getResults(AdapterQuery $query): \Traversable
100100
$builder->addOrderBy($column->getOrderField(), $direction);
101101
}
102102
}
103-
if ($state->getLength() > 0) {
103+
if (null !== $state->getLength()) {
104104
$builder
105105
->setFirstResult($state->getStart())
106106
->setMaxResults($state->getLength());

src/Adapter/Doctrine/ORMAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected function getResults(AdapterQuery $query): \Traversable
169169
$builder->addOrderBy($column->getOrderField(), $direction);
170170
}
171171
}
172-
if ($state->getLength() > 0) {
172+
if (null !== $state->getLength()) {
173173
$builder
174174
->setFirstResult($state->getStart())
175175
->setMaxResults($state->getLength())

src/Adapter/Elasticsearch/ElasticaAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected function getResults(AdapterQuery $query): \Traversable
8080
$search->addIndices($this->indices);
8181

8282
$q = $this->buildQuery($state);
83-
if ($state->getLength() > 0) {
83+
if (null !== $state->getLength()) {
8484
$q->setFrom($state->getStart())->setSize($state->getLength());
8585
}
8686
$this->applyOrdering($q, $state);

src/Adapter/MongoDB/MongoDBAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ private function buildFilter(DataTableState $state): array
119119
private function buildOptions(DataTableState $state): array
120120
{
121121
$options = [
122-
'limit' => $state->getLength(),
123-
'skip' => $state->getStart(),
122+
'limit' => $state->getLength() ?? 0,
123+
'skip' => $state->getLength() ? $state->getStart() : 0,
124124
'sort' => [],
125125
];
126126

src/DataTableState.php

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class DataTableState
3131
/** @var int */
3232
private $start = 0;
3333

34-
/** @var int */
35-
private $length = -1;
34+
/** @var ?int */
35+
private $length = null;
3636

3737
/** @var string */
3838
private $globalSearch = '';
@@ -49,7 +49,7 @@ class DataTableState
4949
/** @var bool */
5050
private $isCallback = false;
5151

52-
/** @var string */
52+
/** @var ?string */
5353
private $exporterName = null;
5454

5555
/**
@@ -62,10 +62,8 @@ public function __construct(DataTable $dataTable)
6262

6363
/**
6464
* Constructs a state based on the default options.
65-
*
66-
* @return DataTableState
6765
*/
68-
public static function fromDefaults(DataTable $dataTable)
66+
public static function fromDefaults(DataTable $dataTable): self
6967
{
7068
$state = new self($dataTable);
7169
$state->start = (int) $dataTable->getOption('start');
@@ -81,7 +79,7 @@ public static function fromDefaults(DataTable $dataTable)
8179
/**
8280
* Loads datatables state from a parameter bag on top of any existing settings.
8381
*/
84-
public function applyParameters(ParameterBag $parameters)
82+
public function applyParameters(ParameterBag $parameters): void
8583
{
8684
$this->draw = $parameters->getInt('draw');
8785
$this->isCallback = true;
@@ -98,7 +96,7 @@ public function applyParameters(ParameterBag $parameters)
9896
$this->handleSearch($parameters);
9997
}
10098

101-
private function handleOrderBy(ParameterBag $parameters)
99+
private function handleOrderBy(ParameterBag $parameters): void
102100
{
103101
if ($parameters->has('order')) {
104102
$this->orderBy = [];
@@ -109,7 +107,7 @@ private function handleOrderBy(ParameterBag $parameters)
109107
}
110108
}
111109

112-
private function handleSearch(ParameterBag $parameters)
110+
private function handleSearch(ParameterBag $parameters): void
113111
{
114112
foreach ($parameters->all()['columns'] ?? [] as $key => $search) {
115113
$column = $this->dataTable->getColumn((int) $key);
@@ -146,11 +144,13 @@ public function getStart(): int
146144
return $this->start;
147145
}
148146

149-
/**
150-
* @return $this
151-
*/
152-
public function setStart(int $start)
147+
public function setStart(int $start): self
153148
{
149+
if ($start < 0) {
150+
@trigger_error(sprintf('Passing a negative value to the "%s::setStart()" method makes no logical sense, defaulting to 0 as the most sane default.', self::class), \E_USER_DEPRECATED);
151+
$start = 0;
152+
}
153+
154154
$this->start = $start;
155155

156156
return $this;
@@ -161,11 +161,13 @@ public function getLength(): int
161161
return $this->length;
162162
}
163163

164-
/**
165-
* @return $this
166-
*/
167-
public function setLength(int $length)
164+
public function setLength(?int $length): self
168165
{
166+
if ($length < 1) {
167+
@trigger_error(sprintf('Calling the "%s::setLength()" method with a length less than 1 is deprecated since version 0.7 of this bundle. If you need to unrestrict the amount of records returned, pass null instead.', self::class), \E_USER_DEPRECATED);
168+
$length = null;
169+
}
170+
169171
$this->length = $length;
170172

171173
return $this;
@@ -176,20 +178,14 @@ public function getGlobalSearch(): string
176178
return $this->globalSearch;
177179
}
178180

179-
/**
180-
* @return $this
181-
*/
182-
public function setGlobalSearch(string $globalSearch)
181+
public function setGlobalSearch(string $globalSearch): self
183182
{
184183
$this->globalSearch = $globalSearch;
185184

186185
return $this;
187186
}
188187

189-
/**
190-
* @return $this
191-
*/
192-
public function addOrderBy(AbstractColumn $column, string $direction = DataTable::SORT_ASCENDING)
188+
public function addOrderBy(AbstractColumn $column, string $direction = DataTable::SORT_ASCENDING): self
193189
{
194190
$this->orderBy[] = [$column, $direction];
195191

@@ -201,9 +197,6 @@ public function getOrderBy(): array
201197
return $this->orderBy;
202198
}
203199

204-
/**
205-
* @return $this
206-
*/
207200
public function setOrderBy(array $orderBy = []): self
208201
{
209202
$this->orderBy = $orderBy;
@@ -219,20 +212,14 @@ public function getSearchColumns(): array
219212
return $this->searchColumns;
220213
}
221214

222-
/**
223-
* @return $this
224-
*/
225215
public function setColumnSearch(AbstractColumn $column, string $search, bool $isRegex = false): self
226216
{
227217
$this->searchColumns[$column->getName()] = ['column' => $column, 'search' => $search, 'regex' => $isRegex];
228218

229219
return $this;
230220
}
231221

232-
/**
233-
* @return string
234-
*/
235-
public function getExporterName()
222+
public function getExporterName(): string
236223
{
237224
return $this->exporterName;
238225
}

src/Exporter/DataTableExporterManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private function getAllData(): \Iterator
118118
{
119119
$data = $this->dataTable
120120
->getAdapter()
121-
->getData($this->dataTable->getState()->setStart(0)->setLength(-1))
121+
->getData($this->dataTable->getState()->setStart(0)->setLength(null))
122122
->getData();
123123

124124
foreach ($data as $row) {

tests/Fixtures/AppBundle/DataTable/Adapter/CustomORMAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function getResults(AdapterQuery $query): \Traversable
4040
$builder->addOrderBy($column->getOrderField(), $direction);
4141
}
4242
}
43-
if ($state->getLength() > 0) {
43+
if (null !== $state->getLength()) {
4444
$builder
4545
->setFirstResult($state->getStart())
4646
->setMaxResults($state->getLength());

0 commit comments

Comments
 (0)