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 ( !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 }
Wikimedia\ParamValidator\TypeDef\IntegerDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: IntegerDef.php:25
Wikimedia\ParamValidator\TypeDef\failure
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition: TypeDef.php:43
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
Wikimedia\ParamValidator\TypeDef\IntegerDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.
Definition: IntegerDef.php:42
Wikimedia\ParamValidator\TypeDef\NumericDef
Type definition base class for numeric types.
Definition: NumericDef.php:25
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
Definition: ParamValidator.php:112
Wikimedia\ParamValidator\TypeDef\IntegerDef
Type definition for integer types.
Definition: IntegerDef.php:23
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:38
Wikimedia\ParamValidator\ParamValidator\PARAM_TYPE
const PARAM_TYPE
(string|array) Type of the parameter.
Definition: ParamValidator.php:76
Wikimedia\ParamValidator\TypeDef\NumericDef\checkRange
checkRange( $value, $name, $origValue, array $settings, array $options)
Check the range of a value.
Definition: NumericDef.php:63
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42