ModuleResponseSerializer::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
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace PPP\Module\DataModel\Serializers;
4
5
use PPP\Module\DataModel\ModuleResponse;
6
use Serializers\DispatchableSerializer;
7
use Serializers\Exceptions\UnsupportedObjectException;
8
use Serializers\Serializer;
9
10
/**
11
 * @licence MIT
12
 * @author Thomas Pellissier Tanon
13
 */
14
class ModuleResponseSerializer implements DispatchableSerializer {
15
16
	/**
17
	 * @var Serializer
18
	 */
19
	private $nodeSerializer;
20
21 1
	public function __construct(Serializer $nodeSerializer) {
22 1
		$this->nodeSerializer = $nodeSerializer;
23 1
	}
24
25
	/**
26
	 * @see DispatchableSerializer::isSerializerFor
27
	 */
28 1
	public function isSerializerFor($object) {
29 1
		return is_object($object) && $object instanceof ModuleResponse;
30
	}
31
32
	/**
33
	 * @see Serializer::serialize
34
	 */
35 1
	public function serialize($object) {
36 1
		if(!$this->isSerializerFor($object)) {
37
			throw new UnsupportedObjectException($object, 'ModuleResponseSerializer can only serialize ModuleResponse objects.');
38
		}
39
40 1
		return $this->getSerialization($object);
41
	}
42
43 1
	private function getSerialization(ModuleResponse $response) {
44
		return array(
45 1
			'language' => $response->getLanguageCode(),
46 1
			'tree' => $this->nodeSerializer->serialize($response->getSentenceTree()),
47 1
			'measures' => (object) $response->getMeasures(),
48 1
			'trace' => $response->getTrace()
49 1
		);
50
	}
51
}
52