| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace IvyForms\Infrastructure\WP\REST; |
| 11 |
|
| 12 |
use WP_REST_Request; |
| 13 |
|
| 14 |
/** |
| 15 |
* Reads positive integer route/body params from a WP REST request. |
| 16 |
*/ |
| 17 |
final class RestRouteParamReader |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @param WP_REST_Request<array<string, mixed>> $request |
| 21 |
*/ |
| 22 |
public static function getPositiveIntParam(WP_REST_Request $request, string $key = 'id'): ?int |
| 23 |
{ |
| 24 |
$raw = $request->get_param($key); |
| 25 |
if ($raw === null || $raw === '' || !is_scalar($raw)) { |
| 26 |
return null; |
| 27 |
} |
| 28 |
$id = (int) $raw; |
| 29 |
|
| 30 |
return $id > 0 ? $id : null; |
| 31 |
} |
| 32 |
} |
| 33 |
|