-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filter and sort on data's worldcities.csv
- Loading branch information
Showing
7 changed files
with
213 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
# Api Platform Pagination | ||
# Api Platform Sort, Filter and Pagination on raw data | ||
|
||
Inspired from https://github.com/api-platform/demo | ||
|
||
This is an example using a custom `CityFilter` filtering on raw data from the file `Repository/City/data/worldcities.csv` | ||
|
||
CityFilter pass the `sort` and `order` params to the context and this context it's processed in the `CityCollectionDataProvider` | ||
|
||
## Install | ||
|
||
composer install | ||
|
||
## Launch | ||
|
||
symfony serve --no-tls | ||
|
||
## Usage | ||
|
||
`/api/cities?search[key]=tokyo&order[key]=desc&page=1` | ||
|
||
### Keys available | ||
|
||
- id | ||
- city | ||
- city_ascii | ||
- lat | ||
- lng | ||
- country | ||
- iso2 | ||
- iso3 | ||
- admin_name | ||
- capital | ||
- population |
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,51 @@ | ||
<?php | ||
|
||
namespace App\Filter; | ||
|
||
use ApiPlatform\Core\Serializer\Filter\FilterInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class CityFilter implements FilterInterface | ||
{ | ||
public const CITY_FILTER_SEARCH_CONTEXT = 'city_search_filter'; | ||
public const CITY_FILTER_ORDER_CONTEXT = 'city_order_filter'; | ||
private $throwOnInvalid; | ||
|
||
public function __construct(bool $throwOnInvalid = false) | ||
{ | ||
$this->throwOnInvalid = $throwOnInvalid; | ||
} | ||
|
||
public function apply(Request $request, bool $normalization, array $attributes, array &$context) | ||
{ | ||
$search = $request->query->get('search'); | ||
$order = $request->query->get('order'); | ||
|
||
if (!$search && $this->throwOnInvalid) { | ||
return; | ||
} | ||
|
||
if ($search) { | ||
$context[self::CITY_FILTER_SEARCH_CONTEXT] = $search; | ||
} | ||
|
||
if ($order) { | ||
$context[self::CITY_FILTER_ORDER_CONTEXT] = $order; | ||
} | ||
} | ||
|
||
public function getDescription(string $resourceClass): array | ||
{ | ||
return [ | ||
'search' => [ | ||
'property' => null, | ||
'type' => 'string', | ||
'required' => false, | ||
'openapi' => [ | ||
'description' => 'Search and Order by key from array', | ||
], | ||
] | ||
]; | ||
} | ||
|
||
} |
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