1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Deserializers; |
4
|
|
|
|
5
|
|
|
use Deserializers\Exceptions\InvalidAttributeException; |
6
|
|
|
use Deserializers\Exceptions\MissingAttributeException; |
7
|
|
|
use Deserializers\Exceptions\MissingTypeException; |
8
|
|
|
use Deserializers\Exceptions\UnsupportedTypeException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @since 1.0 |
12
|
|
|
* |
13
|
|
|
* @license GPL-2.0-or-later |
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
15
|
|
|
*/ |
16
|
|
|
abstract class TypedObjectDeserializer implements DispatchableDeserializer { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $objectType; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $typeKey; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $objectType |
30
|
|
|
* @param string $typeKey |
31
|
|
|
*/ |
32
|
3 |
|
public function __construct( $objectType, $typeKey = 'objectType' ) { |
33
|
3 |
|
$this->objectType = $objectType; |
34
|
3 |
|
$this->typeKey = $typeKey; |
35
|
3 |
|
} |
36
|
|
|
|
37
|
|
|
protected function assertCanDeserialize( $serialization ) { |
38
|
|
|
if ( !$this->hasObjectType( $serialization ) ) { |
39
|
|
|
throw new MissingTypeException(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ( !$this->hasCorrectObjectType( $serialization ) ) { |
43
|
|
|
throw new UnsupportedTypeException( $serialization[$this->typeKey] ); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
public function isDeserializerFor( $serialization ) { |
48
|
3 |
|
return $this->hasObjectType( $serialization ) && $this->hasCorrectObjectType( $serialization ); |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
private function hasCorrectObjectType( array $serialization ) { |
52
|
2 |
|
return $serialization[$this->typeKey] === $this->objectType; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
private function hasObjectType( $serialization ) { |
56
|
3 |
|
return is_array( $serialization ) |
57
|
3 |
|
&& array_key_exists( $this->typeKey, $serialization ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @deprecated since 4.0, just do your own "if ( array_key_exists( … ) )" or |
62
|
|
|
* "if ( isset( … ) )" instead |
63
|
|
|
* |
64
|
|
|
* @param array $array |
65
|
|
|
* @param string $attributeName |
66
|
|
|
*/ |
67
|
|
|
protected function requireAttribute( array $array, $attributeName ) { |
68
|
|
|
if ( !array_key_exists( $attributeName, $array ) ) { |
69
|
|
|
throw new MissingAttributeException( |
70
|
|
|
$attributeName |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @deprecated since 4.0, just do your own "if ( is_array( … ) )" instead |
77
|
|
|
* |
78
|
|
|
* @param array $array |
79
|
|
|
* @param string $attributeName |
80
|
|
|
*/ |
81
|
|
|
protected function assertAttributeIsArray( array $array, $attributeName ) { |
82
|
|
|
$this->assertAttributeInternalType( $array, $attributeName, 'array' ); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @deprecated since 4.0, just do your own "if ( is_string( … ) )" and such instead |
87
|
|
|
* |
88
|
|
|
* @param array $array |
89
|
|
|
* @param string $attributeName |
90
|
|
|
* @param string $internalType |
91
|
|
|
*/ |
92
|
|
|
protected function assertAttributeInternalType( array $array, $attributeName, $internalType ) { |
93
|
|
|
if ( gettype( $array[$attributeName] ) !== $internalType ) { |
94
|
|
|
throw new InvalidAttributeException( |
95
|
|
|
$attributeName, |
96
|
|
|
$array[$attributeName], |
97
|
|
|
"The internal type of attribute '$attributeName' needs to be '$internalType'" |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.