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_int( $value ) ) {
27 $ret = $value;
28 } elseif ( is_float( $value ) ) {
29 // Since JSON does not distinguish between integers and floats,
30 // floats without a fractional parts can be treated as integers.
31 // This is in line with the definition of the "integer" type in
32 // JSON Schema, see https://json-schema.org/understanding-json-schema/reference/numeric.
33 $ret = intval( $value );
34 if ( $ret - $value !== 0.0 ) {
35 $this->fatal(
36 $this->failureMessage( 'badinteger-fraction' )
37 ->params( gettype( $value ) ),
38 $name, $value, $settings, $options
39 );
40 }
41 } elseif ( $options[ self::OPT_ENFORCE_JSON_TYPES ] ?? false ) {
42 $this->fatal(
43 $this->failureMessage( 'badinteger-type' )
44 ->params( gettype( $value ) ),
45 $name, $value, $settings, $options
46 );
47 } else {
48 if ( is_array( $value ) || !preg_match( '/^[+-]?\d+$/D', $value ) ) {
49 $this->fatal( 'badinteger', $name, $value, $settings, $options );
50 } else {
51 $ret = intval( $value, 10 );
52 }
53 }
54
55 // intval() returns min/max on overflow, so check that
56 if ( $ret === PHP_INT_MAX || $ret === PHP_INT_MIN ) {
57 $tmp = ( $ret < 0 ? '-' : '' ) . ltrim( $value, '-0' );
58 if ( $tmp !== (string)$ret ) {
59 $this->failure( 'badinteger', $name, $value, $settings, $options );
60 }
61 }
62
63 return $this->checkRange( $ret, $name, $value, $settings, $options );
64 }
65
66 public function getHelpInfo( $name, array $settings, array $options ) {
67 $info = parent::getHelpInfo( $name, $settings, $options );
68
69 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-integer' )
70 ->params( empty( $settings[ParamValidator::PARAM_ISMULTI] ) ? 1 : 2 );
71
72 return $info;
73 }
74
75 public function stringifyValue( $name, $value, array $settings, array $options ) {
76 if ( !is_array( $value ) ) {
77 return parent::stringifyValue( $name, $value, $settings, $options );
78 }
79
80 return ParamValidator::implodeMultiValue( $value );
81 }
82
83}
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.
fatal( $failure, $name, $value, array $settings, array $options)
Throw a ValidationException.
Definition TypeDef.php:74
failureMessage( $code, array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
Definition TypeDef.php:131
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition TypeDef.php:96