wikimedia/mediawiki-extensions-Wikibase

View on GitHub
repo/includes/Localizer/ParseExceptionLocalizer.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

declare( strict_types = 1 );

namespace Wikibase\Repo\Localizer;

use Exception;
use InvalidArgumentException;
use MediaWiki\Message\Message;
use ValueParsers\ParseException;

/**
 * Provides a Message for ParseException for localized errors.
 *
 * @license GPL-2.0-or-later
 * @author Daniel Kinzler
 * @author Katie Filbert < aude.wiki@gmail.com >
 */
class ParseExceptionLocalizer implements ExceptionLocalizer {

    public function getExceptionMessage( Exception $exception ): Message {
        if ( !( $exception instanceof ParseException ) ) {
            throw new InvalidArgumentException( '$exception is not a ParseException' );
        }

        $msg = null;

        $expectedFormat = $exception->getExpectedFormat();
        if ( $expectedFormat !== null ) {
            // Messages:
            // wikibase-parse-error-coordinate
            // wikibase-parse-error-entity-id
            // wikibase-parse-error-quantity
            // wikibase-parse-error-time
            $msg = new Message( 'wikibase-parse-error-' . $expectedFormat );
        }

        if ( !( $msg instanceof Message ) || !$msg->exists() ) {
            $msg = new Message( 'wikibase-parse-error' );
        }

        return $msg;
    }

    public function hasExceptionMessage( Exception $exception ): bool {
        return $exception instanceof ParseException;
    }

}