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