Completed
Push — master ( 388143...889690 )
by Thomas
05:32 queued 02:57
created

SerializedEntity::isEmpty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6667
cc 3
eloc 5
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Wikibase\EntityStore\DataModel;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\EntityDocument;
7
use Wikibase\DataModel\Entity\EntityId;
8
9
/**
10
 * An entity containing its serialization
11
 *
12
 * @licence GPLv2+
13
 * @author Thomas Pellissier Tanon
14
 */
15
class SerializedEntity implements EntityDocument {
16
17
	/**
18
	 * @var EntityId|null
19
	 */
20
	private $entityId;
21
22
	/**
23
	 * @var array
24
	 */
25
	private $serialization;
26
27
	/**
28
	 * @param EntityId|null $entityId
29
	 * @param array $serialization
30
	 */
31 8
	public function __construct( EntityId $entityId = null, array $serialization ) {
32 8
		if( !array_key_exists( 'type', $serialization ) ) {
33 1
			throw new InvalidArgumentException( 'The entity serialization does not have a type.' );
34
		}
35
36 7
		$this->entityId = $entityId;
37 7
		$this->serialization = $serialization;
38 7
	}
39
40
	/**
41
	 * @see EntityDocument::getId
42
	 */
43 3
	public function getId() {
44 3
		return $this->entityId;
45
	}
46
47
	/**
48
	 * @see EntityDocument::setId
49
	 */
50
	public function setId( $id ) {
51
		throw new InvalidArgumentException( 'Read only entity' );
52
	}
53
54
	/**
55
	 * @see EntityDocument::getType
56
	 */
57 2
	public function getType() {
58 2
		return $this->serialization['type'];
59
	}
60
61
	/**
62
	 * @see EntityDocument::isEmpty
63
	 */
64 2
	public function isEmpty() {
65 2
		foreach( array_keys( $this->serialization ) as $key ) {
66 2
			if( !in_array( $key, [ 'type', 'id' ] ) ) {
67 1
				return false;
68
			}
69 2
		}
70
71 1
		return true;
72
	}
73
74
	/**
75
	 * @return array
76
	 */
77 2
	public function getSerialization() {
78 2
		return $this->serialization;
79
	}
80
}
81