MediaWiki  master
StringDef.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
24 class StringDef extends TypeDef {
25 
34  public const PARAM_MAX_BYTES = 'param-max-bytes';
35 
46  public const PARAM_MAX_CHARS = 'param-max-chars';
47 
48  protected $allowEmptyWhenRequired = false;
49 
56  public function __construct( Callbacks $callbacks, array $options = [] ) {
57  parent::__construct( $callbacks );
58 
59  $this->allowEmptyWhenRequired = !empty( $options['allowEmptyWhenRequired'] );
60  }
61 
62  public function validate( $name, $value, array $settings, array $options ) {
63  if ( !$this->allowEmptyWhenRequired && $value === '' &&
64  !empty( $settings[ParamValidator::PARAM_REQUIRED] )
65  ) {
66  $this->failure( 'missingparam', $name, $value, $settings, $options );
67  }
68 
69  $len = strlen( $value );
70  if ( isset( $settings[self::PARAM_MAX_BYTES] ) && $len > $settings[self::PARAM_MAX_BYTES] ) {
71  $this->failure(
72  $this->failureMessage( 'maxbytes', [
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
77  );
78  }
79  $len = mb_strlen( $value, 'UTF-8' );
80  if ( isset( $settings[self::PARAM_MAX_CHARS] ) && $len > $settings[self::PARAM_MAX_CHARS] ) {
81  $this->failure(
82  $this->failureMessage( 'maxchars', [
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
87  );
88  }
89 
90  return $value;
91  }
92 
93  public function checkSettings( string $name, $settings, array $options, array $ret ) : array {
94  $ret = parent::checkSettings( $name, $settings, $options, $ret );
95 
96  $ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
97  self::PARAM_MAX_BYTES, self::PARAM_MAX_CHARS,
98  ] );
99 
100  $maxb = $settings[self::PARAM_MAX_BYTES] ?? PHP_INT_MAX;
101  if ( !is_int( $maxb ) ) {
102  $ret['issues'][self::PARAM_MAX_BYTES] = 'PARAM_MAX_BYTES must be an integer, got '
103  . gettype( $maxb );
104  } elseif ( $maxb < 0 ) {
105  $ret['issues'][self::PARAM_MAX_BYTES] = 'PARAM_MAX_BYTES must be greater than or equal to 0';
106  }
107 
108  $maxc = $settings[self::PARAM_MAX_CHARS] ?? PHP_INT_MAX;
109  if ( !is_int( $maxc ) ) {
110  $ret['issues'][self::PARAM_MAX_CHARS] = 'PARAM_MAX_CHARS must be an integer, got '
111  . gettype( $maxc );
112  } elseif ( $maxc < 0 ) {
113  $ret['issues'][self::PARAM_MAX_CHARS] = 'PARAM_MAX_CHARS must be greater than or equal to 0';
114  }
115 
116  if ( !$this->allowEmptyWhenRequired && !empty( $settings[ParamValidator::PARAM_REQUIRED] ) ) {
117  if ( $maxb === 0 ) {
118  $ret['issues'][] = 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
119  . 'PARAM_MAX_BYTES is 0. That\'s impossible to satisfy.';
120  }
121  if ( $maxc === 0 ) {
122  $ret['issues'][] = 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and '
123  . 'PARAM_MAX_CHARS is 0. That\'s impossible to satisfy.';
124  }
125  }
126 
127  return $ret;
128  }
129 
130  public function getParamInfo( $name, array $settings, array $options ) {
131  $info = parent::getParamInfo( $name, $settings, $options );
132 
133  $info['maxbytes'] = $settings[self::PARAM_MAX_BYTES] ?? null;
134  $info['maxchars'] = $settings[self::PARAM_MAX_CHARS] ?? null;
135 
136  return $info;
137  }
138 
139  public function getHelpInfo( $name, array $settings, array $options ) {
140  $info = parent::getHelpInfo( $name, $settings, $options );
141 
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] );
145  }
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] );
149  }
150 
151  return $info;
152  }
153 
154 }
Wikimedia\ParamValidator\TypeDef\failureMessage
failureMessage( $code, array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
Definition: TypeDef.php:84
Wikimedia\ParamValidator\Callbacks
Interface defining callbacks needed by ParamValidator.
Definition: Callbacks.php:21
Wikimedia\ParamValidator\TypeDef\failure
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition: TypeDef.php:49
Wikimedia\ParamValidator\TypeDef\StringDef\getParamInfo
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.
Definition: StringDef.php:130
Wikimedia\ParamValidator\TypeDef\StringDef\PARAM_MAX_BYTES
const PARAM_MAX_BYTES
(integer) Maximum length of a string in bytes.
Definition: StringDef.php:34
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:19
Wikimedia\ParamValidator\TypeDef\StringDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: StringDef.php:62
Wikimedia\ParamValidator\ParamValidator\PARAM_REQUIRED
const PARAM_REQUIRED
(bool) Indicate that the parameter is required.
Definition: ParamValidator.php:84
Wikimedia\ParamValidator\TypeDef\StringDef\$allowEmptyWhenRequired
$allowEmptyWhenRequired
Definition: StringDef.php:48
Wikimedia\ParamValidator\TypeDef\StringDef
Type definition for string types.
Definition: StringDef.php:24
Wikimedia\ParamValidator\TypeDef\StringDef\__construct
__construct(Callbacks $callbacks, array $options=[])
Definition: StringDef.php:56
Wikimedia\ParamValidator\TypeDef\StringDef\PARAM_MAX_CHARS
const PARAM_MAX_CHARS
(integer) Maximum length of a string in characters (Unicode codepoints).
Definition: StringDef.php:46
Wikimedia\ParamValidator\TypeDef\StringDef\checkSettings
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.
Definition: StringDef.php:93
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:42
Wikimedia\ParamValidator\TypeDef\StringDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.
Definition: StringDef.php:139
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
Wikimedia\ParamValidator\TypeDef\$callbacks
Callbacks $callbacks
Definition: TypeDef.php:22