MediaWiki master
IntegerDef.php
Go to the documentation of this file.
1<?php
2
4
7
23class 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.
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.
validate( $name, $value, array $settings, array $options)
Validate the value.
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
Type definition base class for numeric types.
checkRange( $value, $name, $origValue, array $settings, array $options)
Check the range of a value.
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition TypeDef.php:61