| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Wikibase\DataModel\Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @since 0.5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license GPL-2.0+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class ItemId extends EntityId implements Int32EntityId { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 * @since 0.5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	const PATTERN = '/^Q[1-9]\d{0,9}\z/i'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * @param string $idSerialization | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	public function __construct( $idSerialization ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$serializationParts = self::splitSerialization( $idSerialization ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		$localId = strtoupper( $serializationParts[2] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$this->assertValidIdFormat( $localId ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		parent::__construct( self::joinSerialization( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			[ $serializationParts[0], $serializationParts[1], $localId ] ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	private function assertValidIdFormat( $idSerialization ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		if ( !is_string( $idSerialization ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			throw new InvalidArgumentException( '$idSerialization must be a string' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		if ( !preg_match( self::PATTERN, $idSerialization ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 			throw new InvalidArgumentException( '$idSerialization must match ' . self::PATTERN ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		if ( strlen( $idSerialization ) > 10 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			&& substr( $idSerialization, 1 ) > Int32EntityId::MAX | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 			throw new InvalidArgumentException( '$idSerialization can not exceed ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 				. Int32EntityId::MAX ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * @see Int32EntityId::getNumericId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @return int Guaranteed to be a distinct integer in the range [1..2147483647]. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	public function getNumericId() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$serializationParts = self::splitSerialization( $this->serialization ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		return (int)substr( $serializationParts[2], 1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 	public function getEntityType() { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 		return 'item'; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * @see Serializable::serialize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	public function serialize() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		return $this->serialization; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * @see Serializable::unserialize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * @param string $serialized | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	public function unserialize( $serialized ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$array = json_decode( $serialized ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$this->serialization = is_array( $array ) ? $array[1] : $serialized; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * Construct an ItemId given the numeric part of its serialization. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * CAUTION: new usages of this method are discouraged. Typically you | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * should avoid dealing with just the numeric part, and use the whole | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 * serialization. Not doing so in new code requires special justification. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * @param int|float|string $numericId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	public static function newFromNumber( $numericId ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		if ( !is_numeric( $numericId ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			throw new InvalidArgumentException( '$numericId must be numeric' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		return new self( 'Q' . $numericId ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * CAUTION: Use the full string serialization whenever you can and avoid using numeric IDs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 * @param string $repositoryName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @param int|float|string $numericId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	public static function newFromRepositoryAndNumber( $repositoryName, $numericId ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		if ( !is_numeric( $numericId ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			throw new InvalidArgumentException( '$numericId must be numeric' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		return new self( self::joinSerialization( [ $repositoryName, '', 'Q' . $numericId ] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  |