1 | <?php |
||
16 | class EntityIdValue extends DataValueObject { |
||
17 | |||
18 | private $entityId; |
||
19 | |||
20 | public function __construct( EntityId $entityId ) { |
||
23 | |||
24 | /** |
||
25 | * @see Serializable::serialize |
||
26 | * |
||
27 | * @since 0.5 |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | public function serialize() { |
||
32 | return json_encode( array( |
||
33 | $this->entityId->getEntityType(), |
||
34 | $this->getNumericId() |
||
35 | ) ); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * This method gets the numeric id from the serialization. |
||
40 | * It makes assumptions we do not want to make about the id format, |
||
41 | * though cannot be removed until we ditch the "numeric id" part |
||
42 | * from the serialization. |
||
43 | * |
||
44 | * @return float Numeric id as a whole number. Can not be int because of 32-bit PHP. |
||
45 | */ |
||
46 | protected function getNumericId() { |
||
49 | |||
50 | /** |
||
51 | * @see Serializable::unserialize |
||
52 | * |
||
53 | * @since 0.5 |
||
54 | * |
||
55 | * @param string $serialized |
||
56 | * |
||
57 | * @throws IllegalValueException |
||
58 | */ |
||
59 | public function unserialize( $serialized ) { |
||
60 | list( $entityType, $numericId ) = json_decode( $serialized ); |
||
61 | |||
62 | try { |
||
63 | $entityId = LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId ); |
||
64 | } catch ( InvalidArgumentException $ex ) { |
||
65 | throw new IllegalValueException( 'Invalid EntityIdValue serialization.' ); |
||
66 | } |
||
67 | |||
68 | $this->__construct( $entityId ); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @see DataValue::getType |
||
73 | * |
||
74 | * @since 0.5 |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public static function getType() { |
||
81 | |||
82 | /** |
||
83 | * @see DataValue::getSortKey |
||
84 | * |
||
85 | * @since 0.5 |
||
86 | * |
||
87 | * @return string|float|int |
||
88 | */ |
||
89 | public function getSortKey() { |
||
92 | |||
93 | /** |
||
94 | * @see DataValue::getValue |
||
95 | * |
||
96 | * @since 0.5 |
||
97 | * |
||
98 | * @return self |
||
99 | */ |
||
100 | public function getValue() { |
||
103 | |||
104 | /** |
||
105 | * @since 0.5 |
||
106 | * |
||
107 | * @return EntityId |
||
108 | */ |
||
109 | public function getEntityId() { |
||
112 | |||
113 | /** |
||
114 | * @see DataValue::getArrayValue |
||
115 | * |
||
116 | * @since 0.5 |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | public function getArrayValue() { |
||
126 | |||
127 | /** |
||
128 | * Constructs a new instance of the DataValue from the provided data. |
||
129 | * This can round-trip with |
||
130 | * @see getArrayValue |
||
131 | * |
||
132 | * @since 0.5 |
||
133 | * |
||
134 | * @param mixed $data |
||
135 | * |
||
136 | * @throws IllegalValueException |
||
137 | * @return self |
||
138 | */ |
||
139 | public static function newFromArray( $data ) { |
||
163 | |||
164 | } |
||
165 |