| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base Validation class for REST API parameters |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.19.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\REST_API; |
| 12 |
|
| 13 |
use WP_REST_Request; |
| 14 |
use WP_Error; |
| 15 |
|
| 16 |
/** |
| 17 |
* Base class for validating REST API parameters. |
| 18 |
* |
| 19 |
* @since 3.19.0 |
| 20 |
*/ |
| 21 |
abstract class Base_Validation { |
| 22 |
/** |
| 23 |
* Validates a parameter. |
| 24 |
* |
| 25 |
* @since 3.19.0 |
| 26 |
* |
| 27 |
* @param mixed $param The parameter value. |
| 28 |
* @param WP_REST_Request $request The request object. |
| 29 |
* @return true|WP_Error Whether the parameter is valid. |
| 30 |
*/ |
| 31 |
abstract public static function validate( $param, WP_REST_Request $request ); |
| 32 |
|
| 33 |
/** |
| 34 |
* Sanitizes a parameter. |
| 35 |
* |
| 36 |
* If a sanitize method is not implemented, it will throw an exception. |
| 37 |
* |
| 38 |
* @since 3.19.0 |
| 39 |
* |
| 40 |
* @throws \Exception The sanitize method is not implemented. |
| 41 |
* |
| 42 |
* @param mixed $value The value to sanitize. |
| 43 |
* @param WP_REST_Request $request The request object. |
| 44 |
* @param string $param The parameter name. |
| 45 |
* @return mixed The sanitized value. |
| 46 |
*/ |
| 47 |
public static function sanitize( $value, $request, $param ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 48 |
throw new \Exception( 'Trying to sanitize ' . esc_html( $param ) . ' parameter without a sanitize method.' ); |
| 49 |
} |
| 50 |
} |
| 51 |
|