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