Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ParamDefinitions | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
| getDefinitionsForContext | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | /** |
| 5 | * ParamDefinitions.php |
| 6 | * |
| 7 | * This file is part of the Codex design system, which provides a standard interface |
| 8 | * for parameter validation across various components. |
| 9 | * |
| 10 | * The `ParamDefinitions` class offers a centralized place to define validation rules |
| 11 | * for parameters used in different contexts, such as "table" or "tabs". This helps keep |
| 12 | * parameter handling consistent and maintainable throughout the codebase. |
| 13 | * |
| 14 | * @category ParamValidator |
| 15 | * @package Codex\ParamValidator |
| 16 | * @since 0.3.0 |
| 17 | * @author Doğu Abaris <abaris@null.net> |
| 18 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
| 19 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
| 20 | */ |
| 21 | |
| 22 | namespace Wikimedia\Codex\ParamValidator; |
| 23 | |
| 24 | use InvalidArgumentException; |
| 25 | |
| 26 | /** |
| 27 | * ParamDefinitions provides a centralized mapping of parameter validation rules for different contexts. |
| 28 | * |
| 29 | * This class returns an associative array of parameter definitions keyed by context. Each context maps to |
| 30 | * an array of parameters, each with its own type, default, and requirement flags. |
| 31 | * |
| 32 | * By separating parameter definitions from the validation logic, this class allows for a more maintainable |
| 33 | * and flexible parameter handling system. Developers can add new contexts or modify existing parameter rules |
| 34 | * without altering the underlying validation system. |
| 35 | * |
| 36 | * Example usage: |
| 37 | * |
| 38 | * $definitions = ParamDefinitions::getDefinitionsForContext( 'tabs' ); |
| 39 | * // Returns an array of parameter definitions applicable to the 'tabs' context. |
| 40 | * |
| 41 | * @category ParamValidator |
| 42 | * @package Codex\ParamValidator |
| 43 | * @since 0.3.0 |
| 44 | * @author Doğu Abaris <abaris@null.net> |
| 45 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
| 46 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
| 47 | */ |
| 48 | class ParamDefinitions { |
| 49 | /** |
| 50 | * Retrieve validation rules for a given context. |
| 51 | * |
| 52 | * This method looks up an associative array of parameter definitions based on the provided context. |
| 53 | * Each parameter definition specifies details like the parameter type, default value, and whether it |
| 54 | * is required. These rules are used in conjunction with the ParamValidator class to ensure consistent |
| 55 | * and reliable parameter handling. |
| 56 | * |
| 57 | * @since 0.3.0 |
| 58 | * @param string $context The name of the context to fetch definitions for. |
| 59 | * For example: "pager", "table", "tabs". |
| 60 | * |
| 61 | * @return array<string, mixed> An associative array of parameter definitions. |
| 62 | * Keys are parameter names, and values are settings arrays. |
| 63 | * |
| 64 | * @throws InvalidArgumentException If no parameter definitions are found for the requested context. |
| 65 | */ |
| 66 | public static function getDefinitionsForContext( string $context ): array { |
| 67 | $allDefinitions = [ |
| 68 | 'table' => [ |
| 69 | 'limit' => [ |
| 70 | ParamValidator::PARAM_TYPE => 'integer', |
| 71 | ParamValidator::PARAM_DEFAULT => 10, |
| 72 | ParamValidator::PARAM_REQUIRED => false, |
| 73 | ], |
| 74 | 'offset' => [ |
| 75 | ParamValidator::PARAM_TYPE => 'string', |
| 76 | ParamValidator::PARAM_DEFAULT => '', |
| 77 | ParamValidator::PARAM_REQUIRED => false, |
| 78 | ], |
| 79 | 'sort' => [ |
| 80 | ParamValidator::PARAM_TYPE => 'string', |
| 81 | ParamValidator::PARAM_DEFAULT => '', |
| 82 | ParamValidator::PARAM_REQUIRED => false, |
| 83 | ], |
| 84 | 'desc' => [ |
| 85 | ParamValidator::PARAM_TYPE => 'string', |
| 86 | ParamValidator::PARAM_DEFAULT => '1', |
| 87 | ParamValidator::PARAM_REQUIRED => false, |
| 88 | ], |
| 89 | 'asc' => [ |
| 90 | ParamValidator::PARAM_TYPE => 'string', |
| 91 | ParamValidator::PARAM_DEFAULT => '1', |
| 92 | ParamValidator::PARAM_REQUIRED => false, |
| 93 | ], |
| 94 | ], |
| 95 | 'tabs' => [ |
| 96 | 'tab' => [ |
| 97 | ParamValidator::PARAM_TYPE => 'string', |
| 98 | ParamValidator::PARAM_DEFAULT => '', |
| 99 | ParamValidator::PARAM_REQUIRED => false, |
| 100 | ], |
| 101 | ], |
| 102 | ]; |
| 103 | |
| 104 | if ( !isset( $allDefinitions[$context] ) ) { |
| 105 | throw new InvalidArgumentException( "No parameter definitions found for context: $context" ); |
| 106 | } |
| 107 | |
| 108 | return $allDefinitions[$context]; |
| 109 | } |
| 110 | } |