diff options
Diffstat (limited to 'www/wiki/vendor/param-processor/param-processor/src/ParamDefinition.php')
-rw-r--r-- | www/wiki/vendor/param-processor/param-processor/src/ParamDefinition.php | 360 |
1 files changed, 86 insertions, 274 deletions
diff --git a/www/wiki/vendor/param-processor/param-processor/src/ParamDefinition.php b/www/wiki/vendor/param-processor/param-processor/src/ParamDefinition.php index 3990e757..1f49bb4a 100644 --- a/www/wiki/vendor/param-processor/param-processor/src/ParamDefinition.php +++ b/www/wiki/vendor/param-processor/param-processor/src/ParamDefinition.php @@ -3,19 +3,17 @@ namespace ParamProcessor; use Exception; - -use ValueParsers\ValueParser; +use ParamProcessor\PackagePrivate\Param; use ValueParsers\NullParser; - -use ValueValidators\ValueValidator; +use ValueParsers\ValueParser; use ValueValidators\NullValidator; +use ValueValidators\ValueValidator; /** - * Parameter definition. * Specifies what kind of values are accepted, how they should be validated, * how they should be formatted, what their dependencies are and how they should be described. * - * Try to avoid using this interface outside of ParamProcessor for anything else then defining parameters. + * Try to avoid using this interface outside of ParamProcessor for anything else than defining parameters. * In particular, do not derive from this class to implement methods such as formatValue. * * @since 1.0 @@ -23,13 +21,13 @@ use ValueValidators\NullValidator; * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ -class ParamDefinition implements IParamDefinition { +/* final */ class ParamDefinition implements IParamDefinition { /** * Indicates whether parameters that are provided more then once should be accepted, * and use the first provided value, or not, and generate an error. * - * @since 1.0 + * @deprected since 1.7 * * @var boolean */ @@ -39,65 +37,44 @@ class ParamDefinition implements IParamDefinition { * Indicates whether parameters not found in the criteria list * should be stored in case they are not accepted. The default is false. * - * @since 1.0 + * @deprected since 1.7 * * @var boolean */ public static $accumulateParameterErrors = false; + protected $type; + protected $name; + protected $default; + protected $isList; + /** - * Indicates if the parameter value should trimmed during the clean process. - * - * @since 1.0 + * A message that acts as description for the parameter or false when there is none. + * Can be obtained via getMessage and set via setMessage. * + * @var string + */ + protected $message; + + /** + * Indicates if the parameter value should trimmed during the clean process. * @var boolean|null */ protected $trimValue = null; /** * Indicates if the parameter manipulations should be applied to the default value. - * - * @since 1.0 - * * @var boolean */ protected $applyManipulationsToDefault = true; /** * Dependency list containing parameters that need to be handled before this one. - * - * @since 1.0 - * * @var string[] */ protected $dependencies = []; /** - * The default value for the parameter, or null when the parameter is required. - * - * @since 1.0 - * - * @var mixed - */ - protected $default; - - /** - * The main name of the parameter. - * - * @since 1.0 - * - * @var string - */ - protected $name; - - /** - * @since 1.0 - * @var boolean - */ - protected $isList; - - /** - * @since 1.0 * @var string */ protected $delimiter = ','; @@ -105,75 +82,43 @@ class ParamDefinition implements IParamDefinition { /** * List of aliases for the parameter name. * - * @since 1.0 - * * @var string[] */ protected $aliases = []; /** - * A message that acts as description for the parameter or false when there is none. - * Can be obtained via getMessage and set via setMessage. - * - * @since 1.0 - * - * @var string - */ - protected $message = 'validator-message-nodesc'; - - /** * Original array definition of the parameter - * - * @since 1.0 - * * @var array */ protected $options = []; /** - * @since 1.0 - * * @var ValueParser|null */ protected $parser = null; /** - * @since 1.0 - * * @var ValueValidator|null */ protected $validator = null; /** - * @since 0.1 - * * @var callable|null */ protected $validationFunction = null; /** - * @since 0.1 - * - * @var string - */ - protected $type; - - /** - * Constructor. - * - * @since 1.0 - * * @param string $type * @param string $name * @param mixed $default Use null for no default (which makes the parameter required) * @param string $message * @param boolean $isList */ - public function __construct( $type, $name, $default = null, $message = null, $isList = false ) { + public function __construct( string $type, string $name, $default = null, string $message = null, bool $isList = false ) { $this->type = $type; $this->name = $name; $this->default = $default; - $this->message = $message; + $this->message = $message ?? 'validator-message-nodesc'; $this->isList = $isList; $this->postConstruct(); @@ -190,61 +135,39 @@ class ParamDefinition implements IParamDefinition { } /** - * @see IParamDefinition::trimDuringClean - * - * @since 1.0 - * - * @return boolean|null + * Returns if the value should be trimmed before validation and any further processing. + * - true: always trim + * - false: never trim + * - null: trim based on context settings */ - public function trimDuringClean() { + public function trimDuringClean(): ?bool { return $this->trimValue; } /** - * @see IParamDefinition::getAliases - * - * @since 1.0 - * + * Returns the parameter name aliases. * @return string[] */ - public function getAliases() { + public function getAliases(): array { return $this->aliases; } - /** - * @see IParamDefinition::hasAlias - * - * @since 1.0 - * - * @param string $alias - * - * @return boolean - */ - public function hasAlias( $alias ) { + public function hasAlias( string $alias ): bool { return in_array( $alias, $this->getAliases() ); } /** - * @see IParamDefinition::hasDependency - * - * @since 1.0 - * - * @param string $dependency - * - * @return boolean + * @deprecated since 1.7 + * Returns if the parameter has a certain dependency. */ - public function hasDependency( $dependency ) { + public function hasDependency( string $dependency ): bool { return in_array( $dependency, $this->getDependencies() ); } /** * Returns the list of allowed values, or an empty array if there is no such restriction. - * - * @since 1.0 - * - * @return array */ - public function getAllowedValues() { + public function getAllowedValues(): array { $allowedValues = []; if ( $this->validator !== null && method_exists( $this->validator, 'getWhitelistedValues' ) ) { @@ -263,9 +186,7 @@ class ParamDefinition implements IParamDefinition { } /** - * @see IParamDefinition::setDefault - * - * @since 1.0 + * @deprecated since 1.7 * * @param mixed $default * @param boolean $manipulate Should the default be manipulated or not? Since 0.4.6. @@ -276,10 +197,7 @@ class ParamDefinition implements IParamDefinition { } /** - * @see IParamDefinition::getDefault - * - * @since 1.0 - * + * Returns the default value. * @return mixed */ public function getDefault() { @@ -287,53 +205,33 @@ class ParamDefinition implements IParamDefinition { } /** - * @see IParamDefinition::getMessage - * - * @since 1.0 - * - * @return string + * Returns a message describing the parameter. */ - public function getMessage() { + public function getMessage(): string { return $this->message; } /** - * @see IParamDefinition::setMessage - * - * @since 1.0 - * - * @param string $message + * This should be a message key, ie something that can be passed + * to wfMsg. Not an actual text. */ - public function setMessage( $message ) { + public function setMessage( string $message ) { $this->message = $message; } /** - * @see IParamDefinition::setDoManipulationOfDefault - * - * @since 1.0 - * - * @param boolean $doOrDoNotThereIsNoTry + * Set if the parameter manipulations should be applied to the default value. */ - public function setDoManipulationOfDefault( $doOrDoNotThereIsNoTry ) { + public function setDoManipulationOfDefault( bool $doOrDoNotThereIsNoTry ) { $this->applyManipulationsToDefault = $doOrDoNotThereIsNoTry; } - /** - * @see IParamDefinition::shouldManipulateDefault - * - * @since 1.0 - * - * @return boolean - */ - public function shouldManipulateDefault() { + public function shouldManipulateDefault(): bool { return $this->applyManipulationsToDefault; } /** - * @see IParamDefinition::addAliases - * - * @since 1.0 + * Adds one or more aliases for the parameter name. * * @param string|string[] $aliases */ @@ -343,9 +241,8 @@ class ParamDefinition implements IParamDefinition { } /** - * @see IParamDefinition::addDependencies - * - * @since 1.0 + * Adds one or more dependencies. There are the names of parameters + * that need to be validated and formatted before this one. * * @param string|string[] $dependencies */ @@ -354,25 +251,14 @@ class ParamDefinition implements IParamDefinition { $this->dependencies = array_merge( $this->dependencies, is_array( $args[0] ) ? $args[0] : $args ); } - /** - * @see IParamDefinition::getName - * - * @since 1.0 - * - * @return string - */ - public function getName() { + public function getName(): string { return $this->name; } /** * Returns a message key for a message describing the parameter type. - * - * @since 1.0 - * - * @return string */ - public function getTypeMessage() { + public function getTypeMessage(): string { $message = 'validator-type-' . $this->getType(); if ( $this->isList() ) { @@ -383,66 +269,45 @@ class ParamDefinition implements IParamDefinition { } /** - * @see IParamDefinition::getDependencies - * - * @since 1.0 + * @deprecated since 1.7 + * Returns a list of dependencies the parameter has, in the form of + * other parameter names. * * @return string[] */ - public function getDependencies() { + public function getDependencies(): array { return $this->dependencies; } - /** - * @see IParamDefinition::isRequired - * - * @since 1.0 - * - * @return boolean - */ - public function isRequired() { + public function isRequired(): bool { return is_null( $this->default ); } - /** - * @see IParamDefinition::isList - * - * @since 1.0 - * - * @return boolean - */ - public function isList() { + public function isList(): bool { return $this->isList; } /** - * @see IParamDefinition::getDelimiter - * - * @since 1.0 - * - * @return string + * Returns the delimiter to use to split the raw value in case the + * parameter is a list. */ - public function getDelimiter() { + public function getDelimiter(): string { return $this->delimiter; } /** - * @see IParamDefinition::setDelimiter - * - * @since 1.0 - * - * @param $delimiter string + * Sets the delimiter to use to split the raw value in case the + * parameter is a list. */ - public function setDelimiter( $delimiter ) { + public function setDelimiter( string $delimiter ) { $this->delimiter = $delimiter; } /** - * @see IParamDefinition::setArrayValues + * Sets the parameter definition values contained in the provided array. * - * @since 1.0 - * - * @param array $param + * @deprecated since 1.7 + * TODO: provide alternative in ParamDefinitionFactory */ public function setArrayValues( array $param ) { if ( array_key_exists( 'aliases', $param ) ) { @@ -471,7 +336,6 @@ class ParamDefinition implements IParamDefinition { /** * @see IParamDefinition::format * - * @since 1.0 * @deprecated * * @param IParam $param @@ -479,6 +343,10 @@ class ParamDefinition implements IParamDefinition { * @param IParam[] $params */ public function format( IParam $param, array &$definitions, array $params ) { + /** + * @var Param $param + */ + if ( $this->isList() && is_array( $param->getValue() ) ) { // TODO: if isList returns true, the value should be an array. // The second check here is to avoid a mysterious error. @@ -516,7 +384,6 @@ class ParamDefinition implements IParamDefinition { * @param $params array of IParam */ protected function formatList( IParam $param, array &$definitions, array $params ) { - // TODO } /** @@ -534,7 +401,6 @@ class ParamDefinition implements IParamDefinition { */ protected function formatValue( $value, IParam $param, array &$definitions, array $params ) { return $value; - // No-op } /** @@ -543,54 +409,28 @@ class ParamDefinition implements IParamDefinition { * ParamDefinition classes and having all keys set to the names of the * corresponding parameters. * - * @since 1.0 + * @deprecated since 1.7 - use ParamDefinitionFactory * - * @param IParamDefinition[] $definitions + * @param ParamDefinition[] $definitions * - * @return IParamDefinition[] + * @return ParamDefinition[] * @throws Exception */ - public static function getCleanDefinitions( array $definitions ) { - $cleanList = []; - - foreach ( $definitions as $key => $definition ) { - if ( is_array( $definition ) ) { - if ( !array_key_exists( 'name', $definition ) && is_string( $key ) ) { - $definition['name'] = $key; - } - - $definition = ParamDefinitionFactory::singleton()->newDefinitionFromArray( $definition ); - } - - if ( !( $definition instanceof IParamDefinition ) ) { - throw new Exception( '$definition not an instance of IParamDefinition' ); - } - - $cleanList[$definition->getName()] = $definition; - } - - return $cleanList; + public static function getCleanDefinitions( array $definitions ): array { + return ParamDefinitionFactory::singleton()->newDefinitionsFromArrays( $definitions ); } /** - * @see IParamDefinition::getType - * - * @since 1.0 - * - * @return string + * Returns an identifier for the type of the parameter. */ - public function getType() { + public function getType(): string { return $this->type; } /** - * @see IParamDefinition::getValueParser - * - * @since 1.0 - * - * @return ValueParser + * Returns a ValueParser object to parse the parameters value. */ - public function getValueParser() { + public function getValueParser(): ValueParser { if ( $this->parser === null ) { $this->parser = new NullParser(); } @@ -599,13 +439,9 @@ class ParamDefinition implements IParamDefinition { } /** - * @see IParamDefinition::getValueValidator - * - * @since 1.0 - * - * @return ValueValidator + * Returns a ValueValidator that can be used to validate the parameters value. */ - public function getValueValidator() { + public function getValueValidator(): ValueValidator { if ( $this->validator === null ) { $this->validator = new NullValidator(); } @@ -613,56 +449,32 @@ class ParamDefinition implements IParamDefinition { return $this->validator; } - /** - * @see IParamDefinition::setValueParser - * - * @since 1.0 - * - * @param ValueParser $parser - */ public function setValueParser( ValueParser $parser ) { $this->parser = $parser; } - /** - * @see IParamDefinition::setValueValidator - * - * @since 1.0 - * - * @param ValueValidator $validator - */ public function setValueValidator( ValueValidator $validator ) { $this->validator = $validator; } /** - * @see IParamDefinition::setValidationCallback + * Sets a validation function that will be run before the ValueValidator. * - * @since 1.0 - * - * @param callable $validationFunction + * This can be used instead of a ValueValidator where validation is very + * trivial, ie checking if something is a boolean can be done with is_bool. */ - public function setValidationCallback( /* callable */ $validationFunction ) { + public function setValidationCallback( ?callable $validationFunction ) { $this->validationFunction = $validationFunction; } /** - * @see IParamDefinition::getValidationCallback - * - * @since 1.0 - * - * @return callable|null + * @see setValidationCallback */ - public function getValidationCallback() { + public function getValidationCallback(): ?callable { return $this->validationFunction; } - /** - * @since 0.1 - * - * @return array - */ - public function getOptions() { + public function getOptions(): array { return $this->options; } |