Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
47.62% |
10 / 21 |
|
20.00% |
2 / 10 |
CRAP | |
0.00% |
0 / 1 |
| EntitySchemaValue | |
47.62% |
10 / 21 |
|
20.00% |
2 / 10 |
42.17 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __serialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| serialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __unserialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| unserialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| newFromArray | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| getArrayValue | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getSchemaId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php declare( strict_types=1 ); |
| 2 | |
| 3 | namespace EntitySchema\Wikibase\DataValues; |
| 4 | |
| 5 | use DataValues\DataValueObject; |
| 6 | use DataValues\IllegalValueException; |
| 7 | use EntitySchema\Domain\Model\EntitySchemaId; |
| 8 | use InvalidArgumentException; |
| 9 | use Wikibase\DataModel\Entity\EntityIdValue; |
| 10 | |
| 11 | /** |
| 12 | * @license GPL-2.0-or-later |
| 13 | */ |
| 14 | class EntitySchemaValue extends DataValueObject { |
| 15 | |
| 16 | public const TYPE = 'entity-schema'; |
| 17 | |
| 18 | private EntitySchemaId $id; |
| 19 | |
| 20 | public function __construct( EntitySchemaId $id ) { |
| 21 | $this->id = $id; |
| 22 | } |
| 23 | |
| 24 | public function __serialize(): array { |
| 25 | return [ 'entityId' => $this->id ]; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @inheritDoc |
| 30 | * Serialization is required by SnakList to compare two snak values |
| 31 | * by the hash of their serialization. These values are not saved anywhere |
| 32 | * so Unserialize is never required. |
| 33 | */ |
| 34 | public function serialize() { |
| 35 | return serialize( $this->id ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param array $data The array representation of the object |
| 40 | * @return never-returns |
| 41 | */ |
| 42 | public function __unserialize( array $data ) { |
| 43 | throw new \LogicException( 'Method not implemented' ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param string $data The serialized representation of the object |
| 48 | * @return never-returns |
| 49 | */ |
| 50 | public function unserialize( $data ): void { |
| 51 | throw new \LogicException( 'Method not implemented' ); |
| 52 | } |
| 53 | |
| 54 | /** @inheritDoc */ |
| 55 | public static function getType() { |
| 56 | return EntityIdValue::getType(); |
| 57 | } |
| 58 | |
| 59 | /** @inheritDoc */ |
| 60 | public function getValue() { |
| 61 | return $this; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Constructs a new instance from the provided data. Required for @see DataValueDeserializer. |
| 66 | * This is expected to round-trip with @see getArrayValue. |
| 67 | * |
| 68 | * @param array $value |
| 69 | * @return self |
| 70 | */ |
| 71 | public static function newFromArray( $value ): self { |
| 72 | if ( !is_array( $value ) ) { |
| 73 | throw new IllegalValueException( 'The value supplied must be an array' ); |
| 74 | } |
| 75 | if ( !array_key_exists( 'id', $value ) ) { |
| 76 | throw new IllegalValueException( 'The value must contain an "id" key' ); |
| 77 | } |
| 78 | if ( !is_string( $value['id'] ) ) { |
| 79 | throw new IllegalValueException( 'The "id" element must be a string' ); |
| 80 | } |
| 81 | |
| 82 | try { |
| 83 | return new self( new EntitySchemaId( $value['id'] ) ); |
| 84 | } catch ( InvalidArgumentException $e ) { |
| 85 | throw new IllegalValueException( $e->getMessage(), 0, $e ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** @inheritDoc */ |
| 90 | public function getArrayValue(): array { |
| 91 | // similar to EntityIdValue::getArrayValue() but without deprecated numeric-id |
| 92 | return [ |
| 93 | 'id' => $this->id->getId(), |
| 94 | 'entity-type' => self::TYPE, |
| 95 | ]; |
| 96 | } |
| 97 | |
| 98 | public function getSchemaId(): string { |
| 99 | return $this->id->getId(); |
| 100 | } |
| 101 | } |