1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AppUtils; |
6
|
|
|
|
7
|
|
|
class ConvertHelper_ThrowableInfo_Serializer |
8
|
|
|
{ |
9
|
|
|
const SERIALIZED_CODE = 'code'; |
10
|
|
|
const SERIALIZED_MESSAGE = 'message'; |
11
|
|
|
const SERIALIZED_REFERER = 'referer'; |
12
|
|
|
const SERIALIZED_CONTEXT = 'context'; |
13
|
|
|
const SERIALIZED_AMOUNT_CALLS = 'amountCalls'; |
14
|
|
|
const SERIALIZED_DATE = 'date'; |
15
|
|
|
const SERIALIZED_PREVIOUS = 'previous'; |
16
|
|
|
const SERIALIZED_CALLS = 'calls'; |
17
|
|
|
const SERIALIZED_OPTIONS = 'options'; |
18
|
|
|
const SERIALIZED_CLASS = 'class'; |
19
|
|
|
const SERIALIZED_DETAILS = 'details'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param ConvertHelper_ThrowableInfo $info |
23
|
|
|
* @return array<string,mixed> |
24
|
|
|
* @throws ConvertHelper_Exception |
25
|
|
|
*/ |
26
|
|
|
public static function serialize(ConvertHelper_ThrowableInfo $info) |
27
|
|
|
{ |
28
|
|
|
$result = array( |
29
|
|
|
self::SERIALIZED_CLASS => $info->getClass(), |
30
|
|
|
self::SERIALIZED_DETAILS => $info->getDetails(), |
31
|
|
|
self::SERIALIZED_MESSAGE => $info->getMessage(), |
32
|
|
|
self::SERIALIZED_CODE => $info->getCode(), |
33
|
|
|
self::SERIALIZED_DATE => $info->getDate()->getISODate(), |
34
|
|
|
self::SERIALIZED_REFERER => $info->getReferer(), |
35
|
|
|
self::SERIALIZED_CONTEXT => $info->getContext(), |
36
|
|
|
self::SERIALIZED_AMOUNT_CALLS => $info->countCalls(), |
37
|
|
|
self::SERIALIZED_OPTIONS => $info->getOptions(), |
38
|
|
|
self::SERIALIZED_CALLS => array(), |
39
|
|
|
self::SERIALIZED_PREVIOUS => null, |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
if($info->hasPrevious()) |
43
|
|
|
{ |
44
|
|
|
$result[self::SERIALIZED_PREVIOUS] = $info->getPrevious()->serialize(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$calls = $info->getCalls(); |
48
|
|
|
foreach($calls as $call) |
49
|
|
|
{ |
50
|
|
|
$result[self::SERIALIZED_CALLS][] = $call->serialize(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array<string,mixed> $serialized |
58
|
|
|
* @return array<string,mixed> |
59
|
|
|
* @throws ConvertHelper_Exception |
60
|
|
|
*/ |
61
|
|
|
public static function unserialize(array $serialized) : array |
62
|
|
|
{ |
63
|
|
|
$data = self::validateSerializedData($serialized); |
64
|
|
|
$data[self::SERIALIZED_PREVIOUS] = self::unserializePrevious($data[self::SERIALIZED_PREVIOUS]); |
65
|
|
|
|
66
|
|
|
if(!isset($data[self::SERIALIZED_CLASS])) |
67
|
|
|
{ |
68
|
|
|
$data[self::SERIALIZED_CLASS] = ''; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if(!isset($data[self::SERIALIZED_DETAILS])) |
72
|
|
|
{ |
73
|
|
|
$data[self::SERIALIZED_DETAILS] = ''; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $data; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array<string,mixed> $serialized |
81
|
|
|
* @return array<string,mixed> |
82
|
|
|
* @throws ConvertHelper_Exception |
83
|
|
|
*/ |
84
|
|
|
private static function validateSerializedData(array $serialized) : array |
85
|
|
|
{ |
86
|
|
|
$keys = array( |
87
|
|
|
self::SERIALIZED_CODE => 'integer', |
88
|
|
|
self::SERIALIZED_MESSAGE => 'string', |
89
|
|
|
self::SERIALIZED_DATE => 'string', |
90
|
|
|
self::SERIALIZED_REFERER => 'string', |
91
|
|
|
self::SERIALIZED_CONTEXT => 'string', |
92
|
|
|
self::SERIALIZED_AMOUNT_CALLS => 'integer', |
93
|
|
|
self::SERIALIZED_OPTIONS => 'array', |
94
|
|
|
self::SERIALIZED_CALLS => 'array' |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
foreach($keys as $key => $type) |
98
|
|
|
{ |
99
|
|
|
if(!isset($serialized[$key]) || gettype($serialized[$key]) !== $type) |
100
|
|
|
{ |
101
|
|
|
throw self::createTypeException($key, $type); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $serialized; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private static function createTypeException(string $keyName, string $expectedType) : ConvertHelper_Exception |
109
|
|
|
{ |
110
|
|
|
return new ConvertHelper_Exception( |
111
|
|
|
'Invalid serialized throwable key', |
112
|
|
|
sprintf( |
113
|
|
|
'The key [%s] does not have the expected data type [%s].', |
114
|
|
|
$keyName, |
115
|
|
|
$expectedType |
116
|
|
|
), |
117
|
|
|
ConvertHelper_ThrowableInfo::ERROR_INVALID_SERIALIZED_DATA_TYPE |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private static function unserializePrevious(?array $previous) : ?ConvertHelper_ThrowableInfo |
122
|
|
|
{ |
123
|
|
|
if(!empty($previous)) |
124
|
|
|
{ |
125
|
|
|
return ConvertHelper_ThrowableInfo::fromSerialized($previous); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|