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
54 protected $allowEmptyWhenRequired = false;
55
62 public function __construct( Callbacks $callbacks, array $options = [] ) {
63 parent::__construct( $callbacks );
64
65 $this->allowEmptyWhenRequired = !empty( $options[ self::OPT_ALLOW_EMPTY ] );
66 }
67
68 public function validate( $name, $value, array $settings, array $options ) {
71
72 if ( !$allowEmptyWhenRequired && $value === '' &&
73 !empty( $settings[ParamValidator::PARAM_REQUIRED] )
74 ) {
75 $this->failure( 'missingparam', $name, $value, $settings, $options );
76 }
77
78 $this->failIfNotString( $name, $value, $settings, $options );
79
80 $len = strlen( $value );
81 if ( isset( $settings[self::PARAM_MAX_BYTES] ) && $len > $settings[self::PARAM_MAX_BYTES] ) {
82 $this->failure(
83 $this->failureMessage( 'maxbytes', [
84 'maxbytes' => $settings[self::PARAM_MAX_BYTES] ?? null,
85 'maxchars' => $settings[self::PARAM_MAX_CHARS] ?? null,
86 ] )->numParams( $settings[self::PARAM_MAX_BYTES], $len ),
87 $name, $value, $settings, $options
88 );
89 }
90 $len = mb_strlen( $value, 'UTF-8' );
91 if ( isset( $settings[self::PARAM_MAX_CHARS] ) && $len > $settings[self::PARAM_MAX_CHARS] ) {
92 $this->failure(
93 $this->failureMessage( 'maxchars', [
94 'maxbytes' => $settings[self::PARAM_MAX_BYTES] ?? null,
95 'maxchars' => $settings[self::PARAM_MAX_CHARS] ?? null,
96 ] )->numParams( $settings[self::PARAM_MAX_CHARS], $len ),
97 $name, $value, $settings, $options
98 );
99 }
100
101 return $value;
102 }
103
104 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
105 $ret = parent::checkSettings( $name, $settings, $options, $ret );
106
107 $ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
108 self::PARAM_MAX_BYTES, self::PARAM_MAX_CHARS,
109 ] );
110
111 $maxb = $settings[self::PARAM_MAX_BYTES] ?? PHP_INT_MAX;
112 if ( !is_int( $maxb ) ) {
113 $ret['issues'][self::PARAM_MAX_BYTES] = 'PARAM_MAX_BYTES must be an integer, got '
114 . gettype( $maxb );
115 } elseif ( $maxb < 0 ) {
116 $ret['issues'][self::PARAM_MAX_BYTES] = 'PARAM_MAX_BYTES must be greater than or equal to 0';
117 }
118
119 $maxc = $settings[self::PARAM_MAX_CHARS] ?? PHP_INT_MAX;
120 if ( !is_int( $maxc ) ) {
121 $ret['issues'][self::PARAM_MAX_CHARS] = 'PARAM_MAX_CHARS must be an integer, got '
122 . gettype( $maxc );
123 } elseif ( $maxc < 0 ) {
124 $ret['issues'][self::PARAM_MAX_CHARS] = 'PARAM_MAX_CHARS must be greater than or equal to 0';
125 }
126
127 if ( !$this->allowEmptyWhenRequired && !empty( $settings[ParamValidator::PARAM_REQUIRED] ) ) {
128 if ( $maxb === 0 ) {
129 $ret['issues'][] = 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
130 . 'PARAM_MAX_BYTES is 0. That\'s impossible to satisfy.';
131 }
132 if ( $maxc === 0 ) {
133 $ret['issues'][] = 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
134 . 'PARAM_MAX_CHARS is 0. That\'s impossible to satisfy.';
135 }
136 }
137
138 return $ret;
139 }
140
141 public function getParamInfo( $name, array $settings, array $options ) {
142 $info = parent::getParamInfo( $name, $settings, $options );
143
144 $info['maxbytes'] = $settings[self::PARAM_MAX_BYTES] ?? null;
145 $info['maxchars'] = $settings[self::PARAM_MAX_CHARS] ?? null;
146
147 return $info;
148 }
149
150 public function getHelpInfo( $name, array $settings, array $options ) {
151 $info = parent::getHelpInfo( $name, $settings, $options );
152
153 if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
154 $info[self::PARAM_MAX_BYTES] = MessageValue::new( 'paramvalidator-help-type-string-maxbytes' )
155 ->numParams( $settings[self::PARAM_MAX_BYTES] );
156 }
157 if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
158 $info[self::PARAM_MAX_CHARS] = MessageValue::new( 'paramvalidator-help-type-string-maxchars' )
159 ->numParams( $settings[self::PARAM_MAX_CHARS] );
160 }
161
162 return $info;
163 }
164
165}
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:62
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition StringDef.php:68
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