Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
StringDef.php
Go to the documentation of this file.
1<?php
2
3namespaceWikimedia\ParamValidator\TypeDef;
4
5useWikimedia\Message\MessageValue;
6useWikimedia\ParamValidator\Callbacks;
7useWikimedia\ParamValidator\ParamValidator;
8useWikimedia\ParamValidator\TypeDef;
9
24classStringDefextendsTypeDef {
25
29publicconstOPT_ALLOW_EMPTY ='allowEmptyWhenRequired';
30
39publicconstPARAM_MAX_BYTES ='param-max-bytes';
40
51publicconstPARAM_MAX_CHARS ='param-max-chars';
52
54protected$allowEmptyWhenRequired =false;
55
62publicfunction__construct(Callbacks$callbacks, array $options = [] ) {
63 parent::__construct($callbacks );
64
65 $this->allowEmptyWhenRequired = !empty( $options[ self::OPT_ALLOW_EMPTY ] );
66 }
67
69publicfunctionvalidate( $name, $value, array $settings, array $options ) {
72
73if ( !$allowEmptyWhenRequired && $value ==='' &&
74 !empty( $settings[ParamValidator::PARAM_REQUIRED] )
75 ) {
76 $this->failure('missingparam', $name, $value, $settings, $options );
77 }
78
79 $this->failIfNotString( $name, $value, $settings, $options );
80
81 $len = strlen( $value );
82if ( isset( $settings[self::PARAM_MAX_BYTES] ) && $len > $settings[self::PARAM_MAX_BYTES] ) {
83 $this->failure(
84 $this->failureMessage('maxbytes', [
85'maxbytes' => $settings[self::PARAM_MAX_BYTES] ??null,
86'maxchars' => $settings[self::PARAM_MAX_CHARS] ??null,
87 ] )->numParams( $settings[self::PARAM_MAX_BYTES], $len ),
88 $name, $value, $settings, $options
89 );
90 }
91 $len = mb_strlen( $value,'UTF-8' );
92if ( isset( $settings[self::PARAM_MAX_CHARS] ) && $len > $settings[self::PARAM_MAX_CHARS] ) {
93 $this->failure(
94 $this->failureMessage('maxchars', [
95'maxbytes' => $settings[self::PARAM_MAX_BYTES] ??null,
96'maxchars' => $settings[self::PARAM_MAX_CHARS] ??null,
97 ] )->numParams( $settings[self::PARAM_MAX_CHARS], $len ),
98 $name, $value, $settings, $options
99 );
100 }
101
102return $value;
103 }
104
106publicfunctioncheckSettings(string $name, $settings, array $options, array $ret ): array {
107 $ret = parent::checkSettings( $name, $settings, $options, $ret );
108
109 $ret['allowedKeys'][] =self::PARAM_MAX_BYTES;
110 $ret['allowedKeys'][] =self::PARAM_MAX_CHARS;
111
112 $maxb = $settings[self::PARAM_MAX_BYTES] ?? PHP_INT_MAX;
113if ( !is_int( $maxb ) ) {
114 $ret['issues'][self::PARAM_MAX_BYTES] ='PARAM_MAX_BYTES must be an integer, got '
115 . gettype( $maxb );
116 } elseif ( $maxb < 0 ) {
117 $ret['issues'][self::PARAM_MAX_BYTES] ='PARAM_MAX_BYTES must be greater than or equal to 0';
118 }
119
120 $maxc = $settings[self::PARAM_MAX_CHARS] ?? PHP_INT_MAX;
121if ( !is_int( $maxc ) ) {
122 $ret['issues'][self::PARAM_MAX_CHARS] ='PARAM_MAX_CHARS must be an integer, got '
123 . gettype( $maxc );
124 } elseif ( $maxc < 0 ) {
125 $ret['issues'][self::PARAM_MAX_CHARS] ='PARAM_MAX_CHARS must be greater than or equal to 0';
126 }
127
128if ( !$this->allowEmptyWhenRequired && !empty( $settings[ParamValidator::PARAM_REQUIRED] ) ) {
129if ( $maxb === 0 ) {
130 $ret['issues'][] ='PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
131 .'PARAM_MAX_BYTES is 0. That\'s impossible to satisfy.';
132 }
133if ( $maxc === 0 ) {
134 $ret['issues'][] ='PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
135 .'PARAM_MAX_CHARS is 0. That\'s impossible to satisfy.';
136 }
137 }
138
139return $ret;
140 }
141
143publicfunctiongetParamInfo( $name, array $settings, array $options ) {
144 $info = parent::getParamInfo( $name, $settings, $options );
145
146 $info['maxbytes'] = $settings[self::PARAM_MAX_BYTES] ??null;
147 $info['maxchars'] = $settings[self::PARAM_MAX_CHARS] ??null;
148
149return $info;
150 }
151
153publicfunctiongetHelpInfo( $name, array $settings, array $options ) {
154 $info = parent::getHelpInfo( $name, $settings, $options );
155
156if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
157 $info[self::PARAM_MAX_BYTES] = MessageValue::new('paramvalidator-help-type-string-maxbytes' )
158 ->numParams( $settings[self::PARAM_MAX_BYTES] );
159 }
160if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
161 $info[self::PARAM_MAX_CHARS] = MessageValue::new('paramvalidator-help-type-string-maxchars' )
162 ->numParams( $settings[self::PARAM_MAX_CHARS] );
163 }
164
165return $info;
166 }
167
168}
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
DefinitionMessageValue.php:21
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
DefinitionParamValidator.php:42
Wikimedia\ParamValidator\ParamValidator\PARAM_REQUIRED
const PARAM_REQUIRED
(bool) Indicate that the parameter is required.
DefinitionParamValidator.php:84
Wikimedia\ParamValidator\TypeDef\StringDef
Type definition for string types.
DefinitionStringDef.php:24
Wikimedia\ParamValidator\TypeDef\StringDef\__construct
__construct(Callbacks $callbacks, array $options=[])
DefinitionStringDef.php:62
Wikimedia\ParamValidator\TypeDef\StringDef\$allowEmptyWhenRequired
bool $allowEmptyWhenRequired
DefinitionStringDef.php:54
Wikimedia\ParamValidator\TypeDef\StringDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.When ParamValidator is processing a multi-valued parameter, this will be called on...
DefinitionStringDef.php:69
Wikimedia\ParamValidator\TypeDef\StringDef\getParamInfo
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.Keys should be short strings using lowercase...
DefinitionStringDef.php:143
Wikimedia\ParamValidator\TypeDef\StringDef\checkSettings
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.This is intended for validation of parameter settings during unit...
DefinitionStringDef.php:106
Wikimedia\ParamValidator\TypeDef\StringDef\PARAM_MAX_BYTES
const PARAM_MAX_BYTES
(integer) Maximum length of a string in bytes.
DefinitionStringDef.php:39
Wikimedia\ParamValidator\TypeDef\StringDef\OPT_ALLOW_EMPTY
const OPT_ALLOW_EMPTY
When this option is set, the empty string is considered a proper value.
DefinitionStringDef.php:29
Wikimedia\ParamValidator\TypeDef\StringDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
DefinitionStringDef.php:153
Wikimedia\ParamValidator\TypeDef\StringDef\PARAM_MAX_CHARS
const PARAM_MAX_CHARS
(integer) Maximum length of a string in characters (Unicode codepoints).
DefinitionStringDef.php:51
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
DefinitionTypeDef.php:19
Wikimedia\ParamValidator\TypeDef\failIfNotString
failIfNotString(string $name, $value, array $settings, array $options)
Fails if $value is not a string.
DefinitionTypeDef.php:68
Wikimedia\ParamValidator\TypeDef\failureMessage
failureMessage( $code, ?array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
DefinitionTypeDef.php:156
Wikimedia\ParamValidator\TypeDef\failure
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
DefinitionTypeDef.php:121
Wikimedia\ParamValidator\TypeDef\$callbacks
Callbacks $callbacks
DefinitionTypeDef.php:35
Wikimedia\ParamValidator\Callbacks
Interface defining callbacks needed by ParamValidator.
DefinitionCallbacks.php:21
Wikimedia\ParamValidator\TypeDef
DefinitionBooleanDef.php:3

[8]ページ先頭

©2009-2025 Movatter.jp