MediaWiki  1.34.0
GadgetDefinitionValidator.php
Go to the documentation of this file.
1 <?php
2 
14  protected static $propertyValidation = [
15  'settings' => [ 'is_array', 'array' ],
16  'settings.rights' => [ 'is_array', 'array' , 'is_string', 'string' ],
17  'settings.default' => [ 'is_bool', 'boolean' ],
18  'settings.hidden' => [ 'is_bool', 'boolean' ],
19  'settings.skins' => [ [ __CLASS__, 'isArrayOrTrue' ], 'array or true', 'is_string', 'string' ],
20  'settings.category' => [ 'is_string', 'string' ],
21  'module' => [ 'is_array', 'array' ],
22  'module.scripts' => [ 'is_array', 'array', 'is_string', 'string' ],
23  'module.styles' => [ 'is_array', 'array', 'is_string', 'string' ],
24  'module.dependencies' => [ 'is_array', 'array', 'is_string', 'string' ],
25  'module.peers' => [ 'is_array', 'array', 'is_string', 'string' ],
26  'module.messages' => [ 'is_array', 'array', 'is_string', 'string' ],
27  'module.type' => [ 'is_string', 'string' ],
28  ];
29 
34  public static function isArrayOrTrue( $value ) {
35  return is_array( $value ) || $value === true;
36  }
37 
44  public function validate( array $properties, $tolerateMissing = false ) {
45  foreach ( self::$propertyValidation as $property => $validation ) {
46  $path = explode( '.', $property );
47  $val = $properties;
48 
49  // Walk down and verify that the path from the root to this property exists
50  foreach ( $path as $p ) {
51  if ( !array_key_exists( $p, $val ) ) {
52  if ( $tolerateMissing ) {
53  // Skip validation of this property altogether
54  continue 2;
55  } else {
56  return Status::newFatal( 'gadgets-validate-notset', $property );
57  }
58  }
59  $val = $val[$p];
60  }
61 
62  // Do the actual validation of this property
63  $func = $validation[0];
64  if ( !call_user_func( $func, $val ) ) {
65  return Status::newFatal(
66  'gadgets-validate-wrongtype',
67  $property,
68  $validation[1],
69  gettype( $val )
70  );
71  }
72 
73  if ( isset( $validation[2] ) && is_array( $val ) ) {
74  // Descend into the array and check the type of each element
75  $func = $validation[2];
76  foreach ( $val as $i => $v ) {
77  if ( !call_user_func( $func, $v ) ) {
78  return Status::newFatal(
79  'gadgets-validate-wrongtype',
80  "{$property}[{$i}]",
81  $validation[3],
82  gettype( $v )
83  );
84  }
85  }
86  }
87  }
88 
89  return Status::newGood();
90  }
91 }
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
GadgetDefinitionValidator\isArrayOrTrue
static isArrayOrTrue( $value)
Definition: GadgetDefinitionValidator.php:34
GadgetDefinitionValidator\$propertyValidation
static $propertyValidation
Validation metadata.
Definition: GadgetDefinitionValidator.php:14
GadgetDefinitionValidator\validate
validate(array $properties, $tolerateMissing=false)
Check the validity of the given properties array.
Definition: GadgetDefinitionValidator.php:44
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
GadgetDefinitionValidator
Class responsible for validating Gadget definition contents.
Definition: GadgetDefinitionValidator.php:8
$path
$path
Definition: NoLocalSettings.php:25