-
-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(laravel): eloquent filters date range (#6606)
* feat(laravel): eloquent filters date range * cs * parameter * fix(laravel): eloquent test * fix test * fix(laravel): corrections * fix(larevel): eloquent test filters * fix(laravel): date filter const * fix(laravel): range filter const --------- Co-authored-by: Nathan <[email protected]> Co-authored-by: soyuka <[email protected]>
- Loading branch information
1 parent
0f89f3f
commit fd50f8b
Showing
14 changed files
with
495 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Laravel\Eloquent\Filter; | ||
|
||
use ApiPlatform\Metadata\JsonSchemaFilterInterface; | ||
use ApiPlatform\Metadata\OpenApiParameterFilterInterface; | ||
use ApiPlatform\Metadata\Parameter; | ||
use ApiPlatform\Metadata\QueryParameter; | ||
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
final class RangeFilter implements FilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface | ||
{ | ||
use QueryPropertyTrait; | ||
|
||
private const OPERATOR_VALUE = [ | ||
'lt' => '<', | ||
'gt' => '>', | ||
'lte' => '<=', | ||
'gte' => '>=', | ||
]; | ||
|
||
/** | ||
* @param Builder<Model> $builder | ||
* @param array<string, mixed> $context | ||
*/ | ||
public function apply(Builder $builder, mixed $values, Parameter $parameter, array $context = []): Builder | ||
{ | ||
$queryProperty = $this->getQueryProperty($parameter); | ||
|
||
foreach ($values as $key => $value) { | ||
$builder = $builder->{$context['whereClause'] ?? 'where'}($queryProperty, self::OPERATOR_VALUE[$key], $value); | ||
} | ||
|
||
return $builder; | ||
} | ||
|
||
public function getSchema(Parameter $parameter): array | ||
{ | ||
return ['type' => 'number']; | ||
} | ||
|
||
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null | ||
{ | ||
$in = $parameter instanceof QueryParameter ? 'query' : 'header'; | ||
$key = $parameter->getKey(); | ||
|
||
return [ | ||
new OpenApiParameter(name: $key.'[gt]', in: $in), | ||
new OpenApiParameter(name: $key.'[lt]', in: $in), | ||
new OpenApiParameter(name: $key.'[gte]', in: $in), | ||
new OpenApiParameter(name: $key.'[lte]', in: $in), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Laravel\Tests\Eloquent\Filter; | ||
|
||
use ApiPlatform\Laravel\Eloquent\Filter\DateFilter; | ||
use ApiPlatform\Metadata\QueryParameter; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DateFilterTest extends TestCase | ||
{ | ||
public function testOperator(): void | ||
{ | ||
$f = new DateFilter(); | ||
$builder = $this->createStub(Builder::class); | ||
$this->assertEquals($builder, $f->apply($builder, ['neq' => '2020-02-02'], new QueryParameter(key: 'date', property: 'date'))); | ||
} | ||
} |
Oops, something went wrong.