PropertyListSerializer::serialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Queryr\Serialization;
4
5
use Queryr\Resources\PropertyList;
6
use Queryr\Resources\PropertyListElement;
7
use Serializers\Exceptions\UnsupportedObjectException;
8
use Serializers\Serializer;
9
10
/**
11
 * @access private
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class PropertyListSerializer implements Serializer {
16
17 2
	public function serialize( $object ) {
18 2
		if ( !( $object instanceof PropertyList ) ) {
19 1
			throw new UnsupportedObjectException( $object, 'Can only serialize instances of PropertyList' );
20
		}
21
22 1
		return $this->serializeList( $object );
23
	}
24
25 1
	private function serializeList( PropertyList $list ) {
26 1
		$serialization = [];
27
28 1
		foreach ( $list->getElements() as $element ) {
29 1
			$serialization[] = $this->serializeElement( $element );
30 1
		}
31
32 1
		return $serialization;
33
	}
34
35 1
	private function serializeElement( PropertyListElement $element ) {
36
		return [
37 1
			'id' => $element->getPropertyId()->getSerialization(),
38 1
			'type' => $element->getPropertyType(),
39 1
			'url' => $element->getApiUrl(),
40 1
			'wikidata_url' => $element->getWikidataUrl(),
41 1
		];
42
	}
43
44
}
45