Completed
Push — master ( 40db7f...94acd6 )
by Jeroen De
02:22
created

DataValueSerializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 37
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 10 2
A getSerializedDataValue() 0 3 1
A isSerializerFor() 0 3 2
1
<?php
2
3
namespace DataValues\Serializers;
4
5
use DataValues\DataValue;
6
use Serializers\DispatchableSerializer;
7
use Serializers\Exceptions\UnsupportedObjectException;
8
9
/**
10
 * @since 0.1
11
 *
12
 * @license GPL-2.0+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class DataValueSerializer implements DispatchableSerializer {
16
17
	/**
18
	 * @see Serializer::serialize
19
	 *
20
	 * @param DataValue $object
21
	 *
22
	 * @throws UnsupportedObjectException
23
	 * @return array
24
	 */
25 9
	public function serialize( $object ) {
26 9
		if ( $this->isSerializerFor( $object ) ) {
27 1
			return $this->getSerializedDataValue( $object );
28
		}
29
30 8
		throw new UnsupportedObjectException(
31 8
			$object,
32 8
			'DataValueSerializer can only serialize DataValue objects'
33
		);
34
	}
35
36 1
	protected function getSerializedDataValue( DataValue $dataValue ) {
37 1
		return $dataValue->toArray();
38
	}
39
40
	/**
41
	 * @see DispatchableSerializer::isSerializerFor
42
	 *
43
	 * @param mixed $object
44
	 *
45
	 * @return bool
46
	 */
47 19
	public function isSerializerFor( $object ) {
48 19
		return is_object( $object ) && $object instanceof DataValue;
49
	}
50
51
}
52