SimpleFoundationSerializer::serialize()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Queryr\Serialization;
4
5
use Queryr\Resources\SimpleItem;
6
use Queryr\Resources\SimpleProperty;
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 SimpleFoundationSerializer implements Serializer {
16
17
	/**
18
	 * @var SimpleItem|SimpleProperty
19
	 */
20
	private $entity;
21
22
	public function serialize( $object ) {
23
		if ( !( $object instanceof SimpleItem ) && !( $object instanceof SimpleProperty ) ) {
24
			throw new UnsupportedObjectException( $object, 'Can only serialize instances of SimpleItem or SimpleProperty' );
25
		}
26
27
		$this->entity = $object;
28
29
		return $this->serializeItem();
30
	}
31
32
	private function serializeItem() {
33
		$serialization = [ 'id' => $this->entity->ids ];
34
35
		if ( $this->entity->label !== '' ) {
36
			$serialization['label'] = $this->entity->label;
37
		}
38
39
		if ( $this->entity->description !== '' ) {
40
			$serialization['description'] = $this->entity->description;
41
		}
42
43
		if ( !empty( $this->entity->aliases ) ) {
44
			$serialization['aliases'] = $this->entity->aliases;
45
		}
46
47
		return $serialization;
48
	}
49
50
}