BetweenQuerySerializer::isSerializerFor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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