1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PPP\DataModel\Deserializers; |
4
|
|
|
|
5
|
|
|
use Deserializers\Exceptions\UnsupportedTypeException; |
6
|
|
|
use Deserializers\TypedObjectDeserializer; |
7
|
|
|
use PPP\DataModel\ResourceNode; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @licence AGPLv3+ |
11
|
|
|
* @author Thomas Pellissier Tanon |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractResourceNodeDeserializer extends TypedObjectDeserializer { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $valueType; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $valueType |
22
|
|
|
*/ |
23
|
|
|
public function __construct($valueType) { |
24
|
|
|
$this->valueType = $valueType; |
25
|
|
|
|
26
|
|
|
parent::__construct('resource', 'type'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @see DispatchableDeserializer::isDeserializerFor |
31
|
|
|
*/ |
32
|
|
|
public function isDeserializerFor($serialization) { |
33
|
|
|
return parent::isDeserializerFor($serialization) && |
34
|
|
|
$this->getValueType($serialization) === $this->valueType; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @see TypedObjectDeserializer::assertCanDeserialize |
39
|
|
|
*/ |
40
|
|
|
protected function assertCanDeserialize($serialization) { |
41
|
|
|
parent::assertCanDeserialize($serialization); |
42
|
|
|
|
43
|
|
|
if($this->getValueType($serialization) !== $this->valueType) { |
44
|
|
|
throw new UnsupportedTypeException($serialization['value-type']); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @see Deserializer::deserialize |
50
|
|
|
*/ |
51
|
|
|
public function deserialize($serialization) { |
52
|
|
|
$this->assertCanDeserialize($serialization); |
53
|
|
|
$this->requireAttributes($serialization, 'value'); |
54
|
|
|
$this->assertAttributeInternalType($serialization, 'value', 'string'); |
55
|
|
|
|
56
|
|
|
return $this->getDeserialization($serialization['value'], $serialization); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $value |
61
|
|
|
* @param array $serialization |
62
|
|
|
* @return ResourceNode |
63
|
|
|
*/ |
64
|
|
|
protected abstract function getDeserialization($value, array $serialization); |
65
|
|
|
|
66
|
|
|
private function getValueType(array $serialization) { |
67
|
|
|
if(array_key_exists('value-type', $serialization)) { |
68
|
|
|
$this->assertAttributeInternalType($serialization, 'value-type', 'string'); |
69
|
|
|
return $serialization['value-type']; |
70
|
|
|
} else { |
71
|
|
|
return 'string'; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|