Skip to content

Commit e62f217

Browse files
committed
Merge branch '5.2' into 6.0
2 parents 2cde2c3 + d43817e commit e62f217

File tree

4 files changed

+113
-26
lines changed

4 files changed

+113
-26
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2020
- [Bakery] The default sub commands in `AssetsBuildCommand` are now in `AssetsBuildCommandListener`
2121
- [Bakery] Added the server option to `assets:webpack` to run HMR server (`npm run webpack:server`) plus use new npm command syntax.
2222
- [Bakery] `AbstractAggregateCommandEvent` construction is now optional. Added `addCommands` and `prependCommands`. All setters methods return `$this`.
23+
- [Sprunje] The sprunje toArray now returns the `sortable` and `filterable` keys. These will can be used by the frontend to dynamically display which columns is filterable/sortable.
2324

2425
## [5.1.2](https://github.com/userfrosting/sprinkle-core/compare/5.1.1...5.1.2)
2526
- Replace `LocaleMiddleware` with `ServerRequestMiddleware`. A new class, `RequestContainer`, can be injected or retrieved from the container to get the server request. It will be `null` if the request is not defined (called before it is injected into the container by Middleware or if there's no request, e.g., a Bakery command).

app/src/Sprunje/Sprunje.php

+60-26
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ abstract class Sprunje
107107
*/
108108
protected $listableKey = 'listable';
109109

110+
/**
111+
* @var string Array key for the list of sortable columns.
112+
*/
113+
protected $sortableKey = 'sortable';
114+
115+
/**
116+
* @var string Array key for the list of filterable columns.
117+
*/
118+
protected $filterableKey = 'filterable';
119+
110120
/**
111121
* @var int CSV export split the request into multiple chunk to avoid memory overflow.
112122
* Lower this value if you encounter memory issues when exporting large data sets.
@@ -208,6 +218,7 @@ public function toResponse(ResponseInterface $response): ResponseInterface
208218
{
209219
$format = $this->options['format'];
210220

221+
// TODO : This should be split into two methods, one for JSON and one for CSV. This would allow to add more format later.
211222
if ($format == 'csv') {
212223
// Prepare response
213224
$response = $response->withHeader('Content-Disposition', "attachment;filename={$this->name}.csv");
@@ -242,6 +253,8 @@ public function getArray(): array
242253
$this->countFilteredKey => $countFiltered,
243254
$this->rowsKey => $rows->values()->toArray(),
244255
$this->listableKey => $this->getListable(),
256+
$this->sortableKey => $this->getSortable(),
257+
$this->filterableKey => $this->getFilterable(),
245258
];
246259
}
247260

@@ -347,29 +360,6 @@ public function getModels(): array
347360
return [$count, $countFiltered, $collection];
348361
}
349362

350-
/**
351-
* Get lists of values for specified fields in 'listable' option, calling a custom lister callback when appropriate.
352-
*
353-
* @return array<string,mixed>
354-
*/
355-
public function getListable(): array
356-
{
357-
$result = [];
358-
foreach ($this->listable as $name) {
359-
// Determine if a custom filter method has been defined
360-
$methodName = 'list' . Str::studly($name);
361-
362-
if (method_exists($this, $methodName)) {
363-
// @phpstan-ignore-next-line Allow variable method call, since we know it exists
364-
$result[$name] = $this->$methodName();
365-
} else {
366-
$result[$name] = $this->getColumnValues($name);
367-
}
368-
}
369-
370-
return $result;
371-
}
372-
373363
/**
374364
* Get the underlying queryable object in its current state.
375365
*
@@ -405,7 +395,7 @@ public function applyFilters(EloquentBuilderContract|QueryBuilderContract $query
405395
{
406396
foreach ($this->options['filters'] as $name => $value) {
407397
// Check that this filter is allowed
408-
if (($name != '_all') && !in_array($name, $this->filterable, true)) {
398+
if (($name != '_all') && !in_array($name, $this->getFilterable(), true)) {
409399
$e = new SprunjeException("Bad filter: $name");
410400
$message = new UserMessage('VALIDATE.SPRUNJE.BAD_FILTER', ['name' => $name]);
411401
$e->setDescription($message);
@@ -432,7 +422,7 @@ public function applySorts(EloquentBuilderContract|QueryBuilderContract $query):
432422
{
433423
foreach ($this->options['sorts'] as $name => $direction) {
434424
// Check that this sort is allowed
435-
if (!in_array($name, $this->sortable, true)) {
425+
if (!in_array($name, $this->getSortable(), true)) {
436426
$e = new SprunjeException("Bad sort: $name");
437427
$message = new UserMessage('VALIDATE.SPRUNJE.BAD_SORT', ['name' => $name]);
438428
$e->setDescription($message);
@@ -488,6 +478,16 @@ public function setFilterable(array $filterable): static
488478
return $this;
489479
}
490480

481+
/**
482+
* Returns fields to allow filtering upon.
483+
*
484+
* @return string[]
485+
*/
486+
public function getFilterable(): array
487+
{
488+
return $this->filterable;
489+
}
490+
491491
/**
492492
* Set fields to allow listing (enumeration) upon.
493493
*
@@ -502,6 +502,30 @@ public function setListable(array $listable): static
502502
return $this;
503503
}
504504

505+
/**
506+
* Get lists of values for specified fields in 'listable' option, calling a
507+
* custom lister callback when appropriate.
508+
*
509+
* @return array<string,mixed>
510+
*/
511+
public function getListable(): array
512+
{
513+
$result = [];
514+
foreach ($this->listable as $name) {
515+
// Determine if a custom filter method has been defined
516+
$methodName = 'list' . Str::studly($name);
517+
518+
if (method_exists($this, $methodName)) {
519+
// @phpstan-ignore-next-line Allow variable method call, since we know it exists
520+
$result[$name] = $this->$methodName();
521+
} else {
522+
$result[$name] = $this->getColumnValues($name);
523+
}
524+
}
525+
526+
return $result;
527+
}
528+
505529
/**
506530
* Set fields to allow sorting upon.
507531
*
@@ -516,6 +540,16 @@ public function setSortable(array $sortable): static
516540
return $this;
517541
}
518542

543+
/**
544+
* Returns fields to allow sorting upon.
545+
*
546+
* @return string[]
547+
*/
548+
public function getSortable(): array
549+
{
550+
return $this->sortable;
551+
}
552+
519553
/**
520554
* Set fields to show in output.
521555
*
@@ -554,7 +588,7 @@ public function setCsvChunk(int $csvChunk): static
554588
*/
555589
protected function filterAll(EloquentBuilderContract|QueryBuilderContract $query, mixed $value): static
556590
{
557-
foreach ($this->filterable as $name) {
591+
foreach ($this->getFilterable() as $name) {
558592
if (Str::studly($name) != 'all' && !in_array($name, $this->excludeForAll, true)) {
559593
// Since we want to match _any_ of the fields, we wrap the field callback in a 'orWhere' callback
560594
$query->orWhere(function ($fieldQuery) use ($name, $value) {

app/tests/Integration/Sprunje/SprunjeBelongsToManyThroughTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ public function testBaseSprunje(): void
117117
],
118118
],
119119
'listable' => [],
120+
'sortable' => [],
121+
'filterable' => [],
120122
], $data);
121123
}
122124
}

app/tests/Integration/Sprunje/SprunjeTest.php

+50
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ class SprunjeTest extends CoreTestCase
4949
]
5050
];
5151

52+
/**
53+
* @var string[]
54+
*/
55+
protected array $filterable = [
56+
'name',
57+
'description',
58+
'type',
59+
'active',
60+
];
61+
62+
/**
63+
* @var string[]
64+
*/
65+
protected array $sortable = [
66+
'id',
67+
'name',
68+
'description',
69+
'type',
70+
];
71+
5272
public function setUp(): void
5373
{
5474
parent::setUp();
@@ -122,6 +142,8 @@ public function testBaseSprunje(): void
122142
['id' => 3, 'name' => 'The foobar', 'description' => 'Le Foo et le Bar', 'type' => 1, 'active' => true],
123143
],
124144
'listable' => $this->listable,
145+
'sortable' => $this->sortable,
146+
'filterable' => $this->filterable,
125147
], $sprunje->getArray());
126148
}
127149

@@ -139,6 +161,8 @@ public function testWithPagination(): void
139161
['id' => 2, 'name' => 'The bar', 'description' => 'Le Bar', 'type' => 2, 'active' => false],
140162
],
141163
'listable' => $this->listable,
164+
'sortable' => $this->sortable,
165+
'filterable' => $this->filterable,
142166
], $sprunje->getArray());
143167
}
144168

@@ -161,6 +185,8 @@ public function testWithPaginationOnStringOptions(): void
161185
['id' => 2, 'name' => 'The bar', 'description' => 'Le Bar', 'type' => 2, 'active' => false],
162186
],
163187
'listable' => $this->listable,
188+
'sortable' => $this->sortable,
189+
'filterable' => $this->filterable,
164190
], $sprunje->getArray());
165191
}
166192

@@ -180,6 +206,8 @@ public function testWithSort(): void
180206
['id' => 1, 'name' => 'The foo', 'description' => 'Le Foo', 'type' => 1, 'active' => true],
181207
],
182208
'listable' => $this->listable,
209+
'sortable' => $this->sortable,
210+
'filterable' => $this->filterable,
183211
], $sprunje->getArray());
184212
}
185213

@@ -201,6 +229,8 @@ public function testWithCustomSort(): void
201229
['id' => 3, 'name' => 'The foobar', 'description' => 'Le Foo et le Bar', 'type' => 1, 'active' => true],
202230
],
203231
'listable' => $this->listable,
232+
'sortable' => $this->sortable,
233+
'filterable' => $this->filterable,
204234
], $sprunje->getArray());
205235
}
206236

@@ -229,6 +259,8 @@ public function testWithFilter(): void
229259
['id' => 3, 'name' => 'The foobar', 'description' => 'Le Foo et le Bar', 'type' => 1, 'active' => true],
230260
],
231261
'listable' => $this->listable,
262+
'sortable' => $this->sortable,
263+
'filterable' => $this->filterable,
232264
], $sprunje->getArray());
233265
}
234266

@@ -243,6 +275,8 @@ public function testWithEmptyFilter(): void
243275
'count_filtered' => 0,
244276
'rows' => [],
245277
'listable' => $this->listable,
278+
'sortable' => $this->sortable,
279+
'filterable' => $this->filterable,
246280
], $sprunje->getArray());
247281
}
248282

@@ -264,6 +298,8 @@ public function testWithCustomFilter(): void
264298
['id' => 3, 'name' => 'The foobar', 'description' => 'Le Foo et le Bar', 'type' => 1, 'active' => true],
265299
],
266300
'listable' => $this->listable,
301+
'sortable' => $this->sortable,
302+
'filterable' => $this->filterable,
267303
], $sprunje->getArray());
268304
}
269305

@@ -285,6 +321,8 @@ public function testWithAllFilter(): void
285321
['id' => 3, 'name' => 'The foobar', 'description' => 'Le Foo et le Bar', 'type' => 1, 'active' => true],
286322
],
287323
'listable' => $this->listable,
324+
'sortable' => $this->sortable,
325+
'filterable' => $this->filterable,
288326
], $sprunje->getArray());
289327
}
290328

@@ -304,6 +342,8 @@ public function testWithTrueBooleanFilter(): void
304342
['id' => 3, 'name' => 'The foobar', 'description' => 'Le Foo et le Bar', 'type' => 1, 'active' => true],
305343
],
306344
'listable' => $this->listable,
345+
'sortable' => $this->sortable,
346+
'filterable' => $this->filterable,
307347
], $sprunje->getArray());
308348
}
309349

@@ -323,6 +363,8 @@ public function testWithFalseBooleanFilter(): void
323363
// ['id' => 3, 'name' => 'The foobar', 'description' => 'Le Foo et le Bar', 'type' => 1, 'active' => true],
324364
],
325365
'listable' => $this->listable,
366+
'sortable' => $this->sortable,
367+
'filterable' => $this->filterable,
326368
], $sprunje->getArray());
327369
}
328370

@@ -359,6 +401,8 @@ public function testWithColumns(): void
359401
['id' => 3, 'name' => 'The foobar'],
360402
],
361403
'listable' => $this->listable,
404+
'sortable' => $this->sortable,
405+
'filterable' => $this->filterable,
362406
], $sprunje->getArray());
363407
}
364408

@@ -381,6 +425,8 @@ public function testForSetters(): void
381425
['id' => 1, 'description' => 'Le Foo'],
382426
],
383427
'listable' => [],
428+
'sortable' => ['description'],
429+
'filterable' => ['description'],
384430
], $sprunje->getArray());
385431
}
386432

@@ -463,6 +509,8 @@ public function testToResponseWithJson(): void
463509
['id' => 3, 'name' => 'The foobar', 'description' => 'Le Foo et le Bar', 'type' => 1, 'active' => true],
464510
],
465511
'listable' => $this->listable,
512+
'sortable' => $this->sortable,
513+
'filterable' => $this->filterable,
466514
], $response);
467515
$this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
468516
}
@@ -479,6 +527,8 @@ public function testRelationSprunje(): void
479527
['id' => 2, 'name' => 'bar', 'description' => 'Le Bar', 'type' => 2, 'active' => false],
480528
],
481529
'listable' => [],
530+
'sortable' => [],
531+
'filterable' => [],
482532
], $sprunje->getArray());
483533
}
484534
}

0 commit comments

Comments
 (0)