StableItemSerializer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 63
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A serialize() 0 9 2
A serializeItem() 0 7 1
A getDataSection() 0 13 3
1
<?php
2
3
namespace Queryr\Serialization;
4
5
use Queryr\Resources\SimpleItem;
6
use Serializers\Exceptions\UnsupportedObjectException;
7
use Serializers\Serializer;
8
9
/**
10
 * @access private
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class StableItemSerializer implements Serializer {
15
16
	private $propertyMap;
17
18
	/**
19
	 * @var Serializer
20
	 */
21
	private $foundationalSerializer;
22
23
	/**
24
	 * @var Serializer
25
	 */
26
	private $statementSerializer;
27
28
	/**
29
	 * @var SimpleItem
30
	 */
31
	private $item;
32
33
	/**
34
	 * @param Serializer $foundationalSerializer
35
	 * @param Serializer $statementSerializer
36
	 * @param string[] $propertyMap Maps property id (string) to stable property name
37
	 */
38 2
	public function __construct( Serializer $foundationalSerializer, Serializer $statementSerializer, array $propertyMap ) {
39 2
		$this->propertyMap = $propertyMap;
40 2
		$this->foundationalSerializer = $foundationalSerializer;
41 2
		$this->statementSerializer = $statementSerializer;
42 2
	}
43
44 2
	public function serialize( $object ) {
45 2
		if ( !( $object instanceof SimpleItem ) ) {
46 1
			throw new UnsupportedObjectException( $object, 'Can only serialize instances of SimpleItem' );
47
		}
48
49 1
		$this->item = $object;
50
51 1
		return $this->serializeItem();
52
	}
53
54 1
	private function serializeItem() {
55 1
		$serialization = $this->foundationalSerializer->serialize( $this->item );
56
57 1
		$serialization['data'] = $this->getDataSection();
58
59 1
		return $serialization;
60
	}
61
62 1
	private function getDataSection() {
63 1
		$data = [];
64
65 1
		foreach ( $this->item->statements as $simpleStatement ) {
66 1
			$propertyId = $simpleStatement->propertyId->getSerialization();
67
68 1
			if ( array_key_exists( $propertyId, $this->propertyMap ) ) {
69 1
				$data[$this->propertyMap[$propertyId]] = $this->statementSerializer->serialize( $simpleStatement );
70 1
			}
71 1
		}
72
73 1
		return $data;
74
	}
75
76
}
77