59 $this->allowEmptyWhenRequired = !empty( $options[
'allowEmptyWhenRequired'] );
62 public function validate( $name, $value, array $settings, array $options ) {
63 if ( !$this->allowEmptyWhenRequired && $value ===
'' &&
66 $this->
failure(
'missingparam', $name, $value, $settings, $options );
69 $len = strlen( $value );
70 if ( isset( $settings[self::PARAM_MAX_BYTES] ) && $len > $settings[self::PARAM_MAX_BYTES] ) {
73 'maxbytes' => $settings[self::PARAM_MAX_BYTES] ??
null,
74 'maxchars' => $settings[self::PARAM_MAX_CHARS] ??
null,
75 ] )->numParams( $settings[self::PARAM_MAX_BYTES], $len ),
76 $name, $value, $settings, $options
79 $len = mb_strlen( $value,
'UTF-8' );
80 if ( isset( $settings[self::PARAM_MAX_CHARS] ) && $len > $settings[self::PARAM_MAX_CHARS] ) {
83 'maxbytes' => $settings[self::PARAM_MAX_BYTES] ??
null,
84 'maxchars' => $settings[self::PARAM_MAX_CHARS] ??
null,
85 ] )->numParams( $settings[self::PARAM_MAX_CHARS], $len ),
86 $name, $value, $settings, $options
93 public function checkSettings(
string $name, $settings, array $options, array $ret ) : array {
94 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
96 $ret[
'allowedKeys'] = array_merge( $ret[
'allowedKeys'], [
97 self::PARAM_MAX_BYTES, self::PARAM_MAX_CHARS,
101 if ( !is_int( $maxb ) ) {
104 } elseif ( $maxb < 0 ) {
109 if ( !is_int( $maxc ) ) {
112 } elseif ( $maxc < 0 ) {
118 $ret[
'issues'][] =
'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
119 .
'PARAM_MAX_BYTES is 0. That\'s impossible to satisfy.';
122 $ret[
'issues'][] =
'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
123 .
'PARAM_MAX_CHARS is 0. That\'s impossible to satisfy.';
130 public function getParamInfo( $name, array $settings, array $options ) {
131 $info = parent::getParamInfo( $name, $settings, $options );
133 $info[
'maxbytes'] = $settings[self::PARAM_MAX_BYTES] ??
null;
134 $info[
'maxchars'] = $settings[self::PARAM_MAX_CHARS] ??
null;
139 public function getHelpInfo( $name, array $settings, array $options ) {
140 $info = parent::getHelpInfo( $name, $settings, $options );
142 if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
143 $info[self::PARAM_MAX_BYTES] = MessageValue::new(
'paramvalidator-help-type-string-maxbytes' )
144 ->numParams( $settings[self::PARAM_MAX_BYTES] );
146 if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
147 $info[self::PARAM_MAX_CHARS] = MessageValue::new(
'paramvalidator-help-type-string-maxchars' )
148 ->numParams( $settings[self::PARAM_MAX_CHARS] );