Passed
Push — currentLimits ( af2764...193e29 )
by no
03:56
created

PropertyId::newFromNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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 PropertyId extends EntityId implements Int32EntityId {
13
14
	/**
15
	 * @since 0.5
16
	 */
17
	const PATTERN = '/^P[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 'property';
65
	}
66
67
	/**
68
	 * @see Serializable::serialize
69
	 *
70
	 * @since 7.0 serialization format changed in an incompatible way
71
	 *
72
	 * @return string
73
	 */
74
	public function serialize() {
75
		return $this->serialization;
76
	}
77
78
	/**
79
	 * @see Serializable::unserialize
80
	 *
81
	 * @param string $serialized
82
	 */
83
	public function unserialize( $serialized ) {
84
		$array = json_decode( $serialized );
85
		$this->serialization = is_array( $array ) ? $array[1] : $serialized;
86
	}
87
88
	/**
89
	 * Construct a PropertyId given the numeric part of its serialization.
90
	 *
91
	 * CAUTION: new usages of this method are discouraged. Typically you
92
	 * should avoid dealing with just the numeric part, and use the whole
93
	 * serialization. Not doing so in new code requires special justification.
94
	 *
95
	 * @param int|float|string $numericId
96
	 *
97
	 * @return self
98
	 * @throws InvalidArgumentException
99
	 */
100
	public static function newFromNumber( $numericId ) {
101
		if ( !is_numeric( $numericId ) ) {
102
			throw new InvalidArgumentException( '$numericId must be numeric' );
103
		}
104
105
		return new self( 'P' . $numericId );
106
	}
107
108
	/**
109
	 * CAUTION: Use the full string serialization whenever you can and avoid using numeric IDs.
110
	 *
111
	 * @since 7.0
112
	 *
113
	 * @param string $repositoryName
114
	 * @param int|float|string $numericId
115
	 *
116
	 * @return self
117
	 * @throws InvalidArgumentException
118
	 */
119
	public static function newFromRepositoryAndNumber( $repositoryName, $numericId ) {
120
		if ( !is_numeric( $numericId ) ) {
121
			throw new InvalidArgumentException( '$numericId must be numeric' );
122
		}
123
124
		return new self( self::joinSerialization( [ $repositoryName, '', 'P' . $numericId ] ) );
125
	}
126
127
}
128