MediaWiki  master
IntegerDef.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
23 class IntegerDef extends NumericDef {
24 
25  public function validate( $name, $value, array $settings, array $options ) {
26  if ( is_array( $value ) || !preg_match( '/^[+-]?\d+$/D', $value ) ) {
27  $this->failure( 'badinteger', $name, $value, $settings, $options );
28  }
29  $ret = intval( $value, 10 );
30 
31  // intval() returns min/max on overflow, so check that
32  if ( $ret === PHP_INT_MAX || $ret === PHP_INT_MIN ) {
33  $tmp = ( $ret < 0 ? '-' : '' ) . ltrim( $value, '-0' );
34  if ( $tmp !== (string)$ret ) {
35  $this->failure( 'badinteger', $name, $value, $settings, $options );
36  }
37  }
38 
39  return $this->checkRange( $ret, $name, $value, $settings, $options );
40  }
41 
42  public function getHelpInfo( $name, array $settings, array $options ) {
43  $info = parent::getHelpInfo( $name, $settings, $options );
44 
45  $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-integer' )
46  ->params( empty( $settings[ParamValidator::PARAM_ISMULTI] ) ? 1 : 2 );
47 
48  return $info;
49  }
50 
51  public function stringifyValue( $name, $value, array $settings, array $options ) {
52  if ( !is_array( $value ) ) {
53  return parent::stringifyValue( $name, $value, $settings, $options );
54  }
55 
56  return ParamValidator::implodeMultiValue( $value );
57  }
58 
59 }
Value object representing a message for i18n.
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Service for formatting and validating API parameters.
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
static implodeMultiValue(array $value)
Implode an array as a multi-valued parameter string, like implode()
const PARAM_TYPE
(string|array) Type of the parameter.
Type definition for integer types.
Definition: IntegerDef.php:23
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: IntegerDef.php:25
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
Definition: IntegerDef.php:51
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
Definition: IntegerDef.php:42
Type definition base class for numeric types.
Definition: NumericDef.php:26
checkRange( $value, $name, $origValue, array $settings, array $options)
Check the range of a value.
Definition: NumericDef.php:64
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition: TypeDef.php:49