MediaWiki master
StringDef.php
Go to the documentation of this file.
1<?php
2
4
9
24class StringDef extends TypeDef {
25
29 public const OPT_ALLOW_EMPTY = 'allowEmptyWhenRequired';
30
39 public const PARAM_MAX_BYTES = 'param-max-bytes';
40
51 public const PARAM_MAX_CHARS = 'param-max-chars';
52
53 protected $allowEmptyWhenRequired = false;
54
61 public function __construct( Callbacks $callbacks, array $options = [] ) {
62 parent::__construct( $callbacks );
63
64 $this->allowEmptyWhenRequired = !empty( $options[ self::OPT_ALLOW_EMPTY ] );
65 }
66
67 public function validate( $name, $value, array $settings, array $options ) {
70
71 if ( !$allowEmptyWhenRequired && $value === '' &&
72 !empty( $settings[ParamValidator::PARAM_REQUIRED] )
73 ) {
74 $this->failure( 'missingparam', $name, $value, $settings, $options );
75 }
76
77 $this->failIfNotString( $name, $value, $settings, $options );
78
79 $len = strlen( $value );
80 if ( isset( $settings[self::PARAM_MAX_BYTES] ) && $len > $settings[self::PARAM_MAX_BYTES] ) {
81 $this->failure(
82 $this->failureMessage( 'maxbytes', [
83 'maxbytes' => $settings[self::PARAM_MAX_BYTES] ?? null,
84 'maxchars' => $settings[self::PARAM_MAX_CHARS] ?? null,
85 ] )->numParams( $settings[self::PARAM_MAX_BYTES], $len ),
86 $name, $value, $settings, $options
87 );
88 }
89 $len = mb_strlen( $value, 'UTF-8' );
90 if ( isset( $settings[self::PARAM_MAX_CHARS] ) && $len > $settings[self::PARAM_MAX_CHARS] ) {
91 $this->failure(
92 $this->failureMessage( 'maxchars', [
93 'maxbytes' => $settings[self::PARAM_MAX_BYTES] ?? null,
94 'maxchars' => $settings[self::PARAM_MAX_CHARS] ?? null,
95 ] )->numParams( $settings[self::PARAM_MAX_CHARS], $len ),
96 $name, $value, $settings, $options
97 );
98 }
99
100 return $value;
101 }
102
103 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
104 $ret = parent::checkSettings( $name, $settings, $options, $ret );
105
106 $ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
107 self::PARAM_MAX_BYTES, self::PARAM_MAX_CHARS,
108 ] );
109
110 $maxb = $settings[self::PARAM_MAX_BYTES] ?? PHP_INT_MAX;
111 if ( !is_int( $maxb ) ) {
112 $ret['issues'][self::PARAM_MAX_BYTES] = 'PARAM_MAX_BYTES must be an integer, got '
113 . gettype( $maxb );
114 } elseif ( $maxb < 0 ) {
115 $ret['issues'][self::PARAM_MAX_BYTES] = 'PARAM_MAX_BYTES must be greater than or equal to 0';
116 }
117
118 $maxc = $settings[self::PARAM_MAX_CHARS] ?? PHP_INT_MAX;
119 if ( !is_int( $maxc ) ) {
120 $ret['issues'][self::PARAM_MAX_CHARS] = 'PARAM_MAX_CHARS must be an integer, got '
121 . gettype( $maxc );
122 } elseif ( $maxc < 0 ) {
123 $ret['issues'][self::PARAM_MAX_CHARS] = 'PARAM_MAX_CHARS must be greater than or equal to 0';
124 }
125
126 if ( !$this->allowEmptyWhenRequired && !empty( $settings[ParamValidator::PARAM_REQUIRED] ) ) {
127 if ( $maxb === 0 ) {
128 $ret['issues'][] = 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
129 . 'PARAM_MAX_BYTES is 0. That\'s impossible to satisfy.';
130 }
131 if ( $maxc === 0 ) {
132 $ret['issues'][] = 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
133 . 'PARAM_MAX_CHARS is 0. That\'s impossible to satisfy.';
134 }
135 }
136
137 return $ret;
138 }
139
140 public function getParamInfo( $name, array $settings, array $options ) {
141 $info = parent::getParamInfo( $name, $settings, $options );
142
143 $info['maxbytes'] = $settings[self::PARAM_MAX_BYTES] ?? null;
144 $info['maxchars'] = $settings[self::PARAM_MAX_CHARS] ?? null;
145
146 return $info;
147 }
148
149 public function getHelpInfo( $name, array $settings, array $options ) {
150 $info = parent::getHelpInfo( $name, $settings, $options );
151
152 if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
153 $info[self::PARAM_MAX_BYTES] = MessageValue::new( 'paramvalidator-help-type-string-maxbytes' )
154 ->numParams( $settings[self::PARAM_MAX_BYTES] );
155 }
156 if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
157 $info[self::PARAM_MAX_CHARS] = MessageValue::new( 'paramvalidator-help-type-string-maxchars' )
158 ->numParams( $settings[self::PARAM_MAX_CHARS] );
159 }
160
161 return $info;
162 }
163
164}
Value object representing a message for i18n.
Service for formatting and validating API parameters.
const PARAM_REQUIRED
(bool) Indicate that the parameter is required.
Type definition for string types.
Definition StringDef.php:24
__construct(Callbacks $callbacks, array $options=[])
Definition StringDef.php:61
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition StringDef.php:67
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.
const PARAM_MAX_BYTES
(integer) Maximum length of a string in bytes.
Definition StringDef.php:39
const OPT_ALLOW_EMPTY
When this option is set, the empty string is considered a proper value.
Definition StringDef.php:29
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.
const PARAM_MAX_CHARS
(integer) Maximum length of a string in characters (Unicode codepoints).
Definition StringDef.php:51
Base definition for ParamValidator types.
Definition TypeDef.php:19
failIfNotString(string $name, $value, array $settings, array $options)
Fails if $value is not a string.
Definition TypeDef.php:68
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
Interface defining callbacks needed by ParamValidator.
Definition Callbacks.php:21