wikimedia/mediawiki-extensions-Wikibase

View on GitHub
lib/packages/wikibase/data-model/src/Entity/ItemId.php

Summary

Maintainability
C
1 day
Test Coverage
<?php

namespace Wikibase\DataModel\Entity;

use InvalidArgumentException;

/**
 * @since 0.5
 *
 * @license GPL-2.0-or-later
 */
class ItemId extends SerializableEntityId implements Int32EntityId {

    /**
     * @since 0.5
     */
    public const PATTERN = '/^Q[1-9]\d{0,9}\z/i';

    /**
     * @param string $idSerialization
     *
     * @throws InvalidArgumentException
     */
    public function __construct( $idSerialization ) {
        $this->assertValidIdFormat( $idSerialization );
        parent::__construct( strtoupper( $idSerialization ) );
    }

    private function assertValidIdFormat( $idSerialization ) {
        if ( !is_string( $idSerialization ) ) {
            throw new InvalidArgumentException( '$idSerialization must be a string' );
        }

        if ( !preg_match( self::PATTERN, $idSerialization ) ) {
            throw new InvalidArgumentException( '$idSerialization must match ' . self::PATTERN );
        }

        if ( strlen( $idSerialization ) > 10
            && substr( $idSerialization, 1 ) > Int32EntityId::MAX
        ) {
            throw new InvalidArgumentException( '$idSerialization can not exceed '
                . Int32EntityId::MAX );
        }
    }

    /**
     * @see Int32EntityId::getNumericId
     *
     * @return int Guaranteed to be a distinct integer in the range [1..2147483647].
     */
    public function getNumericId() {
        return (int)substr( $this->serialization, 1 );
    }

    /**
     * @return string
     */
    public function getEntityType() {
        return 'item';
    }

    public function __serialize(): array {
        return [ 'serialization' => $this->serialization ];
    }

    public function __unserialize( array $data ): void {
        $this->__construct( $data['serialization'] );
        if ( $this->serialization !== $data['serialization'] ) {
            throw new InvalidArgumentException( '$data contained invalid serialization' );
        }
    }

    /**
     * Construct an ItemId given the numeric part of its serialization.
     *
     * CAUTION: new usages of this method are discouraged. Typically you
     * should avoid dealing with just the numeric part, and use the whole
     * serialization. Not doing so in new code requires special justification.
     *
     * @param int|float|string $numericId
     *
     * @return self
     * @throws InvalidArgumentException
     */
    public static function newFromNumber( $numericId ) {
        if ( !is_numeric( $numericId ) ) {
            throw new InvalidArgumentException( '$numericId must be numeric' );
        }

        return new self( 'Q' . $numericId );
    }

}