MediaWiki REL1_34
StringDef.php
Go to the documentation of this file.
1<?php
2
4
9
24class 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}
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:56
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition StringDef.php:62
const PARAM_MAX_BYTES
(integer) Maximum length of a string in bytes.
Definition StringDef.php:34
const PARAM_MAX_CHARS
(integer) Maximum length of a string in characters (Unicode codepoints).
Definition StringDef.php:46
Base definition for ParamValidator types.
Definition TypeDef.php:15
Interface defining callbacks needed by ParamValidator.
Definition Callbacks.php:20