MediaWiki  master
TypeDef.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
19 abstract class TypeDef {
20 
22  protected $callbacks;
23 
29  public function __construct( Callbacks $callbacks ) {
30  $this->callbacks = $callbacks;
31  }
32 
49  protected function failure(
50  $failure, $name, $value, array $settings, array $options, $fatal = true
51  ) {
52  if ( !is_string( $value ) ) {
53  $value = (string)$this->stringifyValue( $name, $value, $settings, $options );
54  }
55 
56  if ( is_string( $failure ) ) {
57  $mv = $this->failureMessage( $failure )
58  ->plaintextParams( $name, $value );
59  } else {
60  $mv = DataMessageValue::new( $failure->getKey(), [], $failure->getCode(), $failure->getData() )
61  ->plaintextParams( $name, $value )
62  ->params( ...$failure->getParams() );
63  }
64 
65  if ( $fatal ) {
66  throw new ValidationException( $mv, $name, $value, $settings );
67  }
68  $this->callbacks->recordCondition( $mv, $name, $value, $settings, $options );
69  }
70 
84  protected function failureMessage( $code, array $data = null, $suffix = null ) : DataMessageValue {
85  return DataMessageValue::new(
86  "paramvalidator-$code" . ( $suffix !== null ? "-$suffix" : '' ),
87  [], $code, $data
88  );
89  }
90 
107  public function getValue( $name, array $settings, array $options ) {
108  return $this->callbacks->getValue( $name, null, $options );
109  }
110 
131  abstract public function validate( $name, $value, array $settings, array $options );
132 
139  public function normalizeSettings( array $settings ) {
140  return $settings;
141  }
142 
167  public function checkSettings( string $name, $settings, array $options, array $ret ) : array {
168  return $ret;
169  }
170 
185  public function getEnumValues( $name, array $settings, array $options ) {
186  return null;
187  }
188 
204  public function stringifyValue( $name, $value, array $settings, array $options ) {
205  return (string)$value;
206  }
207 
223  public function getParamInfo( $name, array $settings, array $options ) {
224  return [];
225  }
226 
250  public function getHelpInfo( $name, array $settings, array $options ) {
251  return [];
252  }
253 
254 }
Wikimedia\ParamValidator
Definition: Callbacks.php:3
Wikimedia\ParamValidator\ValidationException
Error reporting for ParamValidator.
Definition: ValidationException.php:16
Wikimedia\ParamValidator\TypeDef\failureMessage
failureMessage( $code, array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
Definition: TypeDef.php:84
Wikimedia\ParamValidator\Callbacks
Interface defining callbacks needed by ParamValidator.
Definition: Callbacks.php:21
Wikimedia\ParamValidator\TypeDef\failure
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition: TypeDef.php:49
Wikimedia\ParamValidator\TypeDef\getValue
getValue( $name, array $settings, array $options)
Get the value from the request Stable for overriding.
Definition: TypeDef.php:107
Wikimedia\ParamValidator\TypeDef\getEnumValues
getEnumValues( $name, array $settings, array $options)
Get the values for enum-like parameters.
Definition: TypeDef.php:185
Wikimedia\ParamValidator\TypeDef\stringifyValue
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
Definition: TypeDef.php:204
Wikimedia\ParamValidator\TypeDef\__construct
__construct(Callbacks $callbacks)
Stable for calling.
Definition: TypeDef.php:29
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:19
Wikimedia\ParamValidator\TypeDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Wikimedia\ParamValidator\TypeDef\checkSettings
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.
Definition: TypeDef.php:167
Wikimedia\ParamValidator\TypeDef\normalizeSettings
normalizeSettings(array $settings)
Normalize a settings array Stable for overriding.
Definition: TypeDef.php:139
Wikimedia\Message\DataMessageValue
Value object representing a message for i18n with alternative machine-readable data.
Definition: DataMessageValue.php:23
Wikimedia\Message\DataMessageValue\new
static new( $key, $params=[], $code=null, array $data=null)
Static constructor for easier chaining of ->params() methods.
Definition: DataMessageValue.php:55
Wikimedia\ParamValidator\TypeDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.
Definition: TypeDef.php:250
Wikimedia\ParamValidator\TypeDef\getParamInfo
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.
Definition: TypeDef.php:223
Wikimedia\ParamValidator\TypeDef\$callbacks
Callbacks $callbacks
Definition: TypeDef.php:22