Throw InvalidArgumentException when validating/formatting null

Change-Id: Ia46f76a89b26c830aab70ebf5fcc222711c325d1
This commit is contained in:
Thiemo Kreuz 2018-05-18 20:01:05 +02:00
parent 7eed63d895
commit b12b4f740f
4 changed files with 14 additions and 13 deletions

View File

@ -1,7 +1,6 @@
<?php
use DataValues\StringValue;
use ValueFormatters\Exceptions\MismatchingDataValueTypeException;
use ValueFormatters\ValueFormatter;
use Wikibase\Lib\SnakFormatter;
@ -33,12 +32,12 @@ class MathFormatter implements ValueFormatter {
/**
* @param StringValue $value
*
* @throws MismatchingDataValueTypeException
* @throws InvalidArgumentException if not called with a StringValue
* @return string
*/
public function format( $value ) {
if ( !( $value instanceof StringValue ) ) {
throw new MismatchingDataValueTypeException( 'StringValue', get_class( $value ) );
throw new InvalidArgumentException( '$value must be a StringValue' );
}
$tex = $value->getValue();

View File

@ -1,26 +1,28 @@
<?php
use DataValues\StringValue;
use ValueFormatters\Exceptions\MismatchingDataValueTypeException;
use ValueValidators\Error;
use ValueValidators\Result;
use ValueValidators\ValueValidator;
// @author Duc Linh Tran, Julian Hilbig, Moritz Schubotz
/**
* @author Duc Linh Tran
* @author Julian Hilbig
* @author Moritz Schubotz
*/
class MathValidator implements ValueValidator {
/**
* Validates a value with MathInputCheckRestbase
*
* @param mixed $value The value to validate
* @param StringValue $value The value to validate
*
* @return \ValueValidators\Result
* @throws ValueFormatters\Exceptions\MismatchingDataValueTypeException
* @throws InvalidArgumentException if not called with a StringValue
*/
public function validate( $value ) {
if ( !( $value instanceof StringValue ) ) {
throw new MismatchingDataValueTypeException( 'StringValue', get_class( $value ) );
throw new InvalidArgumentException( '$value must be a StringValue' );
}
// get input String from value

View File

@ -43,7 +43,7 @@ class MathFormatterTest extends MediaWikiTestCase {
}
/**
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
* @expectedException InvalidArgumentException
*/
public function testNotStringValue() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
@ -51,7 +51,7 @@ class MathFormatterTest extends MediaWikiTestCase {
}
/**
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
* @expectedException InvalidArgumentException
*/
public function testNullValue() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );

View File

@ -38,7 +38,7 @@ class MathValidatorTest extends MediaWikiTestCase {
}
/**
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
* @expectedException InvalidArgumentException
*/
public function testNotStringValue() {
$validator = new MathValidator();
@ -46,7 +46,7 @@ class MathValidatorTest extends MediaWikiTestCase {
}
/**
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
* @expectedException InvalidArgumentException
*/
public function testNullValue() {
$validator = new MathValidator();