PropertySerializer::serialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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