Index: branches/RL2/extensions/Gadgets/backend/Gadget.php |
— | — | @@ -45,7 +45,7 @@ |
46 | 46 | |
47 | 47 | /** |
48 | 48 | * Validation metadata. |
49 | | - * 'foo.bar.baz' => array( 'callback', 'type name' [, 'membercallback', 'member type name'] ) |
| 49 | + * 'foo.bar.baz' => array( 'type check callback', 'type name' [, 'member type check callback', 'member type name'] ) |
50 | 50 | */ |
51 | 51 | protected static $propertyValidation = array( |
52 | 52 | 'settings' => array( 'is_array', 'array' ), |
— | — | @@ -75,33 +75,36 @@ |
76 | 76 | if ( !is_array( $properties ) ) { |
77 | 77 | return Status::newFatal( 'gadgets-validate-notanobject', gettype( $properties ) ); // Use JSON terminology |
78 | 78 | } |
79 | | - |
| 79 | + |
80 | 80 | foreach ( self::$propertyValidation as $property => $validation ) { |
81 | 81 | $path = explode( '.', $property ); |
82 | | - $var = $properties; |
| 82 | + $val = $properties; |
| 83 | + |
| 84 | + // Walk down and verify that the path from the root to this property exists |
83 | 85 | foreach ( $path as $p ) { |
84 | | - if ( !isset( $var[$p] ) ) { |
| 86 | + if ( !isset( $val[$p] ) ) { |
85 | 87 | return Status::newFatal( 'gadgets-validate-notset', $property ); |
86 | 88 | } |
87 | | - $var = $var[$p]; |
| 89 | + $val = $val[$p]; |
88 | 90 | } |
89 | | - |
| 91 | + |
| 92 | + // Do the actual validation of this property |
90 | 93 | $func = $validation[0]; |
91 | | - if ( !$func( $var ) ) { |
92 | | - return Status::newFatal( 'gadgets-validate-wrongtype', $property, $validation[1], gettype( $var ) ); |
| 94 | + if ( !$func( $val ) ) { |
| 95 | + return Status::newFatal( 'gadgets-validate-wrongtype', $property, $validation[1], gettype( $val ) ); |
93 | 96 | } |
94 | | - |
| 97 | + |
95 | 98 | if ( isset( $validation[2] ) ) { |
96 | 99 | // Descend into the array and check the type of each element |
97 | 100 | $func = $validation[2]; |
98 | | - foreach ( $var as $i => $v ) { |
| 101 | + foreach ( $val as $i => $v ) { |
99 | 102 | if ( !$func( $v ) ){ |
100 | 103 | return Status::newFatal( 'gadgets-validate-wrongtype', "{$property}[{$i}]", $validation[3], gettype( $v ) ); |
101 | 104 | } |
102 | 105 | } |
103 | 106 | } |
104 | 107 | } |
105 | | - |
| 108 | + |
106 | 109 | return Status::newGood(); |
107 | 110 | } |
108 | 111 | |