wikimedia/mediawiki-extensions-Wikibase

View on GitHub
lib/packages/wikibase/internal-serialization/src/Deserializers/StatementDeserializer.php

Summary

Maintainability
A
3 hrs
Test Coverage
<?php

namespace Wikibase\InternalSerialization\Deserializers;

use Deserializers\DispatchableDeserializer;
use Deserializers\Exceptions\DeserializationException;
use Wikibase\DataModel\Statement\Statement;

/**
 * @license GPL-2.0-or-later
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 * @author Thiemo Kreuz
 */
class StatementDeserializer implements DispatchableDeserializer {

    /**
     * @var DispatchableDeserializer
     */
    private $legacyDeserializer;

    /**
     * @var DispatchableDeserializer
     */
    private $currentDeserializer;

    public function __construct(
        DispatchableDeserializer $legacyDeserializer,
        DispatchableDeserializer $currentDeserializer
    ) {
        $this->legacyDeserializer = $legacyDeserializer;
        $this->currentDeserializer = $currentDeserializer;
    }

    /**
     * @param array $serialization
     *
     * @return Statement
     * @throws DeserializationException
     */
    public function deserialize( $serialization ) {
        if ( $this->currentDeserializer->isDeserializerFor( $serialization ) ) {
            return $this->currentDeserializer->deserialize( $serialization );
        } elseif ( $this->legacyDeserializer->isDeserializerFor( $serialization ) ) {
            return $this->legacyDeserializer->deserialize( $serialization );
        }

        throw new DeserializationException(
            'The provided claim serialization is neither legacy nor current'
        );
    }

    /**
     * @see DispatchableDeserializer::isDeserializerFor
     *
     * @since 2.2
     *
     * @param mixed $serialization
     *
     * @return bool
     */
    public function isDeserializerFor( $serialization ) {
        return $this->currentDeserializer->isDeserializerFor( $serialization )
            || $this->legacyDeserializer->isDeserializerFor( $serialization );
    }

}