Skip to content

Commit

Permalink
(refactor): use WordPress builtin API params validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike van den Hoek committed Jul 15, 2024
1 parent 984259c commit 889cae7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
43 changes: 4 additions & 39 deletions src/Leges/RestAPI/Controllers/LegesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function __construct()
public function getLeges(WP_REST_Request $request): WP_REST_Response
{
$this->repository->addQueryArguments([
'posts_per_page' => $this->handleRequestParam($request, 'limit', 'int', 10),
'paged' => $this->handleRequestParam($request, 'page', 'int', 1),
'posts_per_page' => $request->get_param('limit'),
'paged' => $request->get_param('page'),
]);

$data = $this->repository->all();
Expand All @@ -35,14 +35,7 @@ public function getLeges(WP_REST_Request $request): WP_REST_Response
*/
public function getLege(WP_REST_Request $request)
{
$id = $this->handleRequestParam($request, 'id', 'int', 0);

if (empty($id)) {
return new WP_Error('bad_request', 'Invalid ID', [
'status' => 400,
]);
}

$id = $request->get_param('id');
$data = $this->repository->find($id);

if (! $data) {
Expand All @@ -59,7 +52,7 @@ public function getLege(WP_REST_Request $request)
*/
public function getLegeBySlug(WP_REST_Request $request)
{
$slug = $this->handleRequestParam($request, 'slug', 'string', '');
$slug = $request->get_param('slug');

if (strlen($slug) === 0) {
return new WP_Error('bad_request', 'Invalid slug', [
Expand Down Expand Up @@ -97,32 +90,4 @@ protected function addPaginator(array $data, WP_Query $query): array
],
]);
}

/**
* Handle and sanitize a request parameter to make sure the value is of the expected type.
*
* @return mixed
*/
public function handleRequestParam(WP_REST_Request $request, string $param, string $type, $default = null)
{
$value = $request->get_param($param);

if (null === $value) {
return gettype($default) === $type ? $default : null;
}

switch ($type) {
case 'int':
return (int) $value;
case 'float':
return (float) $value;
case 'bool':
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
case 'array':
return is_array($value) ? $value : explode(',', $value);
case 'string':
default:
return (string) $value;
}
}
}
28 changes: 28 additions & 0 deletions src/Leges/RestAPI/RestAPIServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,46 @@ public function registerRoutes(): void
'methods' => WP_REST_Server::READABLE,
'callback' => [new LegesController, 'getLeges'],
'permission_callback' => '__return_true',
'args' => [
'limit' => [
'description' => 'Number of posts per page.',
'type' => 'integer',
'default' => 10,
'minimum' => -1,
'maximum' => 100,
],
'page' => [
'description' => 'Current page number.',
'type' => 'integer',
'default' => 1
]
]
]);

register_rest_route($this->namespace, 'leges/(?P<id>\d+)', [
'methods' => WP_REST_Server::READABLE,
'callback' => [new LegesController, 'getLege'],
'permission_callback' => '__return_true',
'args' => [
'id' => [
'description' => 'ID of the post.',
'type' => 'integer',
'default' => 0
]
]
]);

register_rest_route($this->namespace, 'leges/(?P<slug>[\w-]+)', [
'methods' => WP_REST_Server::READABLE,
'callback' => [new LegesController, 'getLegeBySlug'],
'permission_callback' => '__return_true',
'args' => [
'slug' => [
'description' => 'Slug of the post.',
'type' => 'string',
'default' => ''
]
]
]);
}
}

0 comments on commit 889cae7

Please sign in to comment.