wikimedia/mediawiki-extensions-Wikibase

View on GitHub
repo/rest-api/src/Application/UseCaseRequestValidation/PropertyIdValidatingDeserializer.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\UseCaseRequestValidation;

use Wikibase\DataModel\Entity\NumericPropertyId;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
use Wikibase\Repo\RestApi\Application\Validation\PropertyIdValidator;

/**
 * @license GPL-2.0-or-later
 */
class PropertyIdValidatingDeserializer {
    private PropertyIdValidator $propertyIdValidator;

    public function __construct( PropertyIdValidator $validator ) {
        $this->propertyIdValidator = $validator;
    }

    /**
     * @throws UseCaseError
     */
    public function validateAndDeserialize( string $propertyId ): NumericPropertyId {
        $validationError = $this->propertyIdValidator->validate( $propertyId );
        if ( $validationError ) {
            throw new UseCaseError(
                UseCaseError::INVALID_PATH_PARAMETER,
                "Invalid path parameter: 'property_id'",
                [ UseCaseError::CONTEXT_PARAMETER => 'property_id' ]
            );
        }
        return new NumericPropertyId( $propertyId );
    }

}