6use InvalidArgumentException;
7use Wikimedia\Assert\Assert;
8use Wikimedia\ObjectFactory;
177 'boolean' => [
'class' => TypeDef\BooleanDef::class ],
178 'checkbox' => [
'class' => TypeDef\PresenceBooleanDef::class ],
179 'integer' => [
'class' => TypeDef\IntegerDef::class ],
180 'limit' => [
'class' => TypeDef\LimitDef::class ],
181 'float' => [
'class' => TypeDef\FloatDef::class ],
182 'double' => [
'class' => TypeDef\FloatDef::class ],
183 'string' => [
'class' => TypeDef\StringDef::class ],
184 'password' => [
'class' => TypeDef\PasswordDef::class ],
186 'class' => TypeDef\StringDef::class,
188 'allowEmptyWhenRequired' =>
true,
191 'timestamp' => [
'class' => TypeDef\TimestampDef::class ],
192 'upload' => [
'class' => TypeDef\UploadDef::class ],
193 'enum' => [
'class' => TypeDef\EnumDef::class ],
228 $this->
addTypeDefs( $options[
'typeDefs'] ?? self::$STANDARD_TYPES );
229 $this->ismultiLimit1 = $options[
'ismultiLimits'][0] ?? 50;
230 $this->ismultiLimit2 = $options[
'ismultiLimits'][1] ?? 500;
238 return array_keys( $this->typeDefs );
267 Assert::parameterType(
268 implode(
'|', [ TypeDef::class,
'array' ] ),
273 if ( isset( $this->typeDefs[$name] ) ) {
274 throw new InvalidArgumentException(
"Type '$name' is already registered" );
276 $this->typeDefs[$name] = $typeDef;
286 Assert::parameterType(
287 implode(
'|', [ TypeDef::class,
'array',
'null' ] ),
292 if ( $typeDef ===
null ) {
293 unset( $this->typeDefs[$name] );
295 $this->typeDefs[$name] = $typeDef;
305 return isset( $this->typeDefs[$name] );
314 if ( is_array(
$type ) ) {
318 if ( !isset( $this->typeDefs[
$type] ) ) {
322 $def = $this->typeDefs[
$type];
323 if ( !$def instanceof
TypeDef ) {
324 $def = $this->objectFactory->createObject( $def, [
325 'extraArgs' => [ $this->callbacks ],
326 'assertClass' => TypeDef::class,
328 $this->typeDefs[
$type] = $def;
342 if ( !is_array( $settings ) ) {
344 self::PARAM_DEFAULT => $settings,
349 if ( !isset( $settings[self::PARAM_TYPE] ) ) {
350 $settings[
self::PARAM_TYPE] = gettype( $settings[self::PARAM_DEFAULT] ??
null );
353 $typeDef = $this->
getTypeDef( $settings[self::PARAM_TYPE] );
355 $settings = $typeDef->normalizeSettings( $settings );
371 public function getValue( $name, $settings, array $options = [] ) {
374 $typeDef = $this->
getTypeDef( $settings[self::PARAM_TYPE] );
376 throw new DomainException(
377 "Param $name's type is unknown - {$settings[self::PARAM_TYPE]}"
381 $value = $typeDef->getValue( $name, $settings, $options );
383 if ( $value !==
null ) {
384 if ( !empty( $settings[self::PARAM_SENSITIVE] ) ) {
385 $this->callbacks->recordCondition(
392 if ( !empty( $settings[self::PARAM_DEPRECATED] ) ) {
393 $this->callbacks->recordCondition(
398 } elseif ( isset( $settings[self::PARAM_DEFAULT] ) ) {
402 return $this->
validateValue( $name, $value, $settings, $options );
418 public function validateValue( $name, $value, $settings, array $options = [] ) {
421 $typeDef = $this->
getTypeDef( $settings[self::PARAM_TYPE] );
423 throw new DomainException(
424 "Param $name's type is unknown - {$settings[self::PARAM_TYPE]}"
428 if ( $value ===
null ) {
429 if ( !empty( $settings[self::PARAM_REQUIRED] ) ) {
436 if ( empty( $settings[self::PARAM_ISMULTI] ) ) {
437 return $typeDef->validate( $name, $value, $settings, $options );
446 $enumValues = $typeDef->getEnumValues( $name, $settings, $options );
447 if ( is_array( $enumValues ) && isset( $settings[self::PARAM_ALL] ) &&
448 count( $valuesList ) === 1
450 $allValue = is_string( $settings[self::PARAM_ALL] )
453 if ( $valuesList[0] === $allValue ) {
459 $sizeLimit = count( $valuesList ) > $limit1 && $this->callbacks->useHighLimits( $options )
462 if ( count( $valuesList ) > $sizeLimit ) {
464 'limit' => $sizeLimit
468 $options[
'values-list'] = $valuesList;
471 foreach ( $valuesList as $v ) {
473 $validValues[] = $typeDef->validate( $name, $v, $settings, $options );
475 if ( empty( $settings[self::PARAM_IGNORE_INVALID_VALUES] ) ) {
478 $invalidValues[] = $v;
481 if ( $invalidValues ) {
482 $this->callbacks->recordCondition(
484 'values' => $invalidValues,
491 if ( empty( $settings[self::PARAM_ALLOW_DUPLICATES] ) ) {
492 $validValues = array_values( array_unique( $validValues ) );
509 if ( $value ===
'' || $value ===
"\x1f" ) {
513 if ( substr( $value, 0, 1 ) ===
"\x1f" ) {
515 $value = substr( $value, 1 );
520 return explode( $sep, $value, $limit );