Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| TypeDefinitions | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
110 | |
0.00% |
0 / 1 |
| getDefaultTypeDefs | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
110 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | /** |
| 5 | * TypeDefinitions.php |
| 6 | * |
| 7 | * This file is part of the Codex design system, providing built-in type definitions |
| 8 | * for parameter validation. The `TypeDefinitions` class includes validation logic for standard |
| 9 | * types like integer, string, boolean, float, and array. |
| 10 | * |
| 11 | * @category ParamValidator |
| 12 | * @package Codex\ParamValidator |
| 13 | * @since 0.3.0 |
| 14 | * @author Doğu Abaris <abaris@null.net> |
| 15 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
| 16 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
| 17 | */ |
| 18 | |
| 19 | namespace Wikimedia\Codex\ParamValidator; |
| 20 | |
| 21 | use UnexpectedValueException; |
| 22 | |
| 23 | /** |
| 24 | * TypeDefinitions provides built-in type definitions for parameter validation. |
| 25 | * |
| 26 | * This class defines a set of standard type validation functions that are used by |
| 27 | * the `ParamValidator` to validate request parameters. Developers can also extend |
| 28 | * these definitions by adding custom types. |
| 29 | * |
| 30 | * @category ParamValidator |
| 31 | * @package Codex\ParamValidator |
| 32 | * @since 0.3.0 |
| 33 | * @author Doğu Abaris <abaris@null.net> |
| 34 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
| 35 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
| 36 | * TODO: Once MediaWiki's ParamValidator is split into a standalone library,this class will no longer be necessary. |
| 37 | */ |
| 38 | class TypeDefinitions { |
| 39 | |
| 40 | /** |
| 41 | * Get default type definitions. |
| 42 | * |
| 43 | * This method returns an array of callable functions for validating standard |
| 44 | * parameter types, such as integer, string, boolean, float, and array. |
| 45 | * |
| 46 | * @since 0.3.0 |
| 47 | * @return array<string, callable> An array of type definitions. |
| 48 | */ |
| 49 | public static function getDefaultTypeDefs(): array { |
| 50 | return [ |
| 51 | 'integer' => static function ( string $name, $value ): int { |
| 52 | if ( !is_numeric( $value ) || (int)$value != $value ) { |
| 53 | throw new UnexpectedValueException( "$name must be an integer." ); |
| 54 | } |
| 55 | return (int)$value; |
| 56 | }, |
| 57 | 'string' => static function ( string $name, $value ): string { |
| 58 | if ( !is_string( $value ) ) { |
| 59 | throw new UnexpectedValueException( "$name must be a string." ); |
| 60 | } |
| 61 | return $value; |
| 62 | }, |
| 63 | 'boolean' => static function ( string $name, $value ): bool { |
| 64 | if ( !is_bool( $value ) && $value !== 'true' && $value !== 'false' ) { |
| 65 | throw new UnexpectedValueException( "$name must be a boolean." ); |
| 66 | } |
| 67 | return filter_var( $value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) ?? false; |
| 68 | }, |
| 69 | 'float' => static function ( string $name, $value ): float { |
| 70 | if ( !is_numeric( $value ) || (float)$value != $value ) { |
| 71 | throw new UnexpectedValueException( "$name must be a float." ); |
| 72 | } |
| 73 | return (float)$value; |
| 74 | }, |
| 75 | 'array' => static function ( string $name, $value ): array { |
| 76 | if ( !is_array( $value ) ) { |
| 77 | throw new UnexpectedValueException( "$name must be an array." ); |
| 78 | } |
| 79 | return $value; |
| 80 | }, |
| 81 | ]; |
| 82 | } |
| 83 | } |