PropertySerializer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 93
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A isSerializerFor() 0 3 1
A serialize() 0 10 2
A getSerialized() 0 12 1
A addIdToSerialization() 0 7 2
A addTermsToSerialization() 0 9 1
A addStatementListToSerialization() 0 3 1
1
<?php
2
3
namespace Wikibase\DataModel\Serializers;
4
5
use Serializers\DispatchableSerializer;
6
use Serializers\Exceptions\SerializationException;
7
use Serializers\Exceptions\UnsupportedObjectException;
8
use Serializers\Serializer;
9
use Wikibase\DataModel\Entity\Property;
10
11
/**
12
 * Package private
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Thomas Pellissier Tanon
16
 * @author Jan Zerebecki < [email protected] >
17
 * @author Bene* < [email protected] >
18
 */
19
class PropertySerializer implements DispatchableSerializer {
20
21
	/**
22
	 * @var Serializer
23
	 */
24
	private $termListSerializer;
25
26
	/**
27
	 * @var Serializer
28
	 */
29
	private $aliasGroupListSerializer;
30
31
	/**
32
	 * @var Serializer
33
	 */
34
	private $statementListSerializer;
35
36 17
	public function __construct(
37
		Serializer $termListSerializer,
38
		Serializer $aliasGroupListSerializer,
39
		Serializer $statementListSerializer
40
	) {
41 17
		$this->termListSerializer = $termListSerializer;
42 17
		$this->aliasGroupListSerializer = $aliasGroupListSerializer;
43 17
		$this->statementListSerializer = $statementListSerializer;
44 17
	}
45
46
	/**
47
	 * @see Serializer::isSerializerFor
48
	 *
49
	 * @param mixed $object
50
	 *
51
	 * @return bool
52
	 */
53 16
	public function isSerializerFor( $object ) {
54 16
		return $object instanceof Property;
55
	}
56
57
	/**
58
	 * @see Serializer::serialize
59
	 *
60
	 * @param Property $object
61
	 *
62
	 * @throws SerializationException
63
	 * @return array
64
	 */
65 12
	public function serialize( $object ) {
66 12
		if ( !$this->isSerializerFor( $object ) ) {
67 3
			throw new UnsupportedObjectException(
68 3
				$object,
69 3
				'PropertySerializer can only serialize Property objects.'
70
			);
71
		}
72
73 9
		return $this->getSerialized( $object );
74
	}
75
76 9
	private function getSerialized( Property $property ) {
77
		$serialization = [
78 9
			'type' => $property->getType(),
79 9
			'datatype' => $property->getDataTypeId(),
80
		];
81
82 9
		$this->addIdToSerialization( $property, $serialization );
83 9
		$this->addTermsToSerialization( $property, $serialization );
84 9
		$this->addStatementListToSerialization( $property, $serialization );
85
86 9
		return $serialization;
87
	}
88
89 9
	private function addIdToSerialization( Property $property, array &$serialization ) {
90 9
		$id = $property->getId();
91
92 9
		if ( $id !== null ) {
93 1
			$serialization['id'] = $id->getSerialization();
94
		}
95 9
	}
96
97 9
	private function addTermsToSerialization( Property $property, array &$serialization ) {
98 9
		$fingerprint = $property->getFingerprint();
99
100 9
		$serialization['labels'] = $this->termListSerializer->serialize( $fingerprint->getLabels() );
101 9
		$serialization['descriptions'] =
102 9
			$this->termListSerializer->serialize( $fingerprint->getDescriptions() );
103 9
		$serialization['aliases'] =
104 9
			$this->aliasGroupListSerializer->serialize( $fingerprint->getAliasGroups() );
105 9
	}
106
107 9
	private function addStatementListToSerialization( Property $property, array &$serialization ) {
108 9
		$serialization['claims'] = $this->statementListSerializer->serialize( $property->getStatements() );
109 9
	}
110
111
}
112