mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
synced 2024-12-21 03:42:46 +00:00
b3e57cbfc2
Otherwise IDEs and other tools don't know what's going on. ...->getService() returns mixed. Change-Id: If0429485941c19eb0e88896cb1ada66a5320bafe
57 lines
1.3 KiB
PHP
57 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\Math;
|
|
|
|
use DataValues\StringValue;
|
|
use InvalidArgumentException;
|
|
use ValueValidators\Error;
|
|
use ValueValidators\Result;
|
|
use ValueValidators\ValueValidator;
|
|
|
|
/**
|
|
* @author Duc Linh Tran
|
|
* @author Julian Hilbig
|
|
* @author Moritz Schubotz
|
|
*/
|
|
class MathValidator implements ValueValidator {
|
|
|
|
/**
|
|
* Validates a value with MediaWiki\Extension\Math\InputCheck\RestbaseChecker
|
|
*
|
|
* @param StringValue $value The value to validate
|
|
*
|
|
* @return Result
|
|
* @throws InvalidArgumentException if not called with a StringValue
|
|
*/
|
|
public function validate( $value ) {
|
|
if ( !( $value instanceof StringValue ) ) {
|
|
throw new InvalidArgumentException( '$value must be a StringValue' );
|
|
}
|
|
|
|
// get input String from value
|
|
$tex = $value->getValue();
|
|
$checker = Math::getCheckerFactory()
|
|
->newLocalChecker( $tex, 'tex' );
|
|
|
|
if ( $checker->isValid() ) {
|
|
return Result::newSuccess();
|
|
}
|
|
|
|
// TeX string is not valid
|
|
return Result::newError(
|
|
[
|
|
Error::newError( '', null, 'malformed-value', [ $checker->getError() ] )
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @see ValueValidator::setOptions()
|
|
*
|
|
* @param array $options
|
|
*/
|
|
public function setOptions( array $options ) {
|
|
// Do nothing. This method shouldn't even be in the interface.
|
|
}
|
|
}
|