ClaimQuerySerializer::serialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
1
<?php
2
3
namespace WikidataQueryApi\Query\Serializers;
4
5
use Serializers\DispatchableSerializer;
6
use Serializers\Exceptions\UnsupportedObjectException;
7
use WikidataQueryApi\Query\ClaimQuery;
8
9
/**
10
 * @licence GPLv2+
11
 * @author Thomas Pellissier Tanon
12
 */
13
class ClaimQuerySerializer implements DispatchableSerializer {
14
15
	/**
16
	 * @see DispatchableSerializer::isSerializerFor
17
	 */
18 10
	public function isSerializerFor( $object ) {
19 10
		return is_object( $object ) && $object instanceof ClaimQuery;
20
	}
21
22
	/**
23
	 * @see Serializer::serialize
24
	 */
25 5
	public function serialize( $object ) {
26 5
		if ( !$this->isSerializerFor( $object ) ) {
27 3
			throw new UnsupportedObjectException(
28 3
				$object,
29
				'ClaimQuerySerializer can only serialize ClaimQuery objects'
30 3
			);
31
		}
32
33 2
		return $this->getSerialized( $object );
34
	}
35
36 2
	private function getSerialized( ClaimQuery $query ) {
37 2
		$text = 'claim[' . $query->getPropertyId()->getNumericId();
38
39 2
		if ( $query->getItemId() !== null ) {
40 1
			$text .= ':' . $query->getItemId()->getNumericId();
41 1
		}
42
43 2
		return $text . ']';
44
	}
45
}
46