SimpleFoundationSerializer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 36
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 9 3
A serializeItem() 0 17 4
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
}