Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.31% |
12 / 13 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| MathValidator | |
92.31% |
12 / 13 |
|
50.00% |
1 / 2 |
4.01 | |
0.00% |
0 / 1 |
| validate | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| setOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\Math; |
| 4 | |
| 5 | use DataValues\StringValue; |
| 6 | use InvalidArgumentException; |
| 7 | use ValueValidators\Error; |
| 8 | use ValueValidators\Result; |
| 9 | use ValueValidators\ValueValidator; |
| 10 | |
| 11 | /** |
| 12 | * @author Duc Linh Tran |
| 13 | * @author Julian Hilbig |
| 14 | * @author Moritz Schubotz |
| 15 | */ |
| 16 | class MathValidator implements ValueValidator { |
| 17 | |
| 18 | /** |
| 19 | * Validates a value with MediaWiki\Extension\Math\InputCheck\RestbaseChecker |
| 20 | * |
| 21 | * @param StringValue $value The value to validate |
| 22 | * |
| 23 | * @return Result |
| 24 | * @throws InvalidArgumentException if not called with a StringValue |
| 25 | */ |
| 26 | public function validate( $value ) { |
| 27 | if ( !( $value instanceof StringValue ) ) { |
| 28 | throw new InvalidArgumentException( '$value must be a StringValue' ); |
| 29 | } |
| 30 | |
| 31 | // get input String from value |
| 32 | $tex = $value->getValue(); |
| 33 | $checker = Math::getCheckerFactory() |
| 34 | ->newLocalChecker( $tex, 'tex' ); |
| 35 | |
| 36 | if ( $checker->isValid() ) { |
| 37 | return Result::newSuccess(); |
| 38 | } |
| 39 | |
| 40 | // TeX string is not valid |
| 41 | return Result::newError( |
| 42 | [ |
| 43 | Error::newError( '', null, 'malformed-value', [ $checker->getError() ] ) |
| 44 | ] |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @see ValueValidator::setOptions() |
| 50 | * |
| 51 | * @param array $options |
| 52 | */ |
| 53 | public function setOptions( array $options ) { |
| 54 | // Do nothing. This method shouldn't even be in the interface. |
| 55 | } |
| 56 | } |