MediaWiki  1.34.0
StringDef.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
24 class StringDef extends TypeDef {
25 
34  const PARAM_MAX_BYTES = 'param-max-bytes';
35 
46  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  throw new ValidationException( $name, $value, $settings, 'missingparam', [] );
67  }
68 
69  if ( isset( $settings[self::PARAM_MAX_BYTES] )
70  && strlen( $value ) > $settings[self::PARAM_MAX_BYTES]
71  ) {
72  throw new ValidationException( $name, $value, $settings, 'maxbytes', [
73  'maxbytes' => $settings[self::PARAM_MAX_BYTES] ?? '',
74  'maxchars' => $settings[self::PARAM_MAX_CHARS] ?? '',
75  ] );
76  }
77  if ( isset( $settings[self::PARAM_MAX_CHARS] )
78  && mb_strlen( $value, 'UTF-8' ) > $settings[self::PARAM_MAX_CHARS]
79  ) {
80  throw new ValidationException( $name, $value, $settings, 'maxchars', [
81  'maxbytes' => $settings[self::PARAM_MAX_BYTES] ?? '',
82  'maxchars' => $settings[self::PARAM_MAX_CHARS] ?? '',
83  ] );
84  }
85 
86  return $value;
87  }
88 
89 }
Wikimedia\ParamValidator\ValidationException
Error reporting for ParamValidator.
Definition: ValidationException.php:15
Wikimedia\ParamValidator\Callbacks
Interface defining callbacks needed by ParamValidator.
Definition: Callbacks.php:20
Wikimedia\ParamValidator\TypeDef\StringDef\PARAM_MAX_BYTES
const PARAM_MAX_BYTES
(integer) Maximum length of a string in bytes.
Definition: StringDef.php:34
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:15
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:75
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
Definition: BooleanDef.php:3
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:38
Wikimedia\ParamValidator\TypeDef\$callbacks
Callbacks $callbacks
Definition: TypeDef.php:18