@@ -40,18 +40,67 @@ public function setToken(TokenInterface $token)
40
40
41
41
/**
42
42
* {@inheritdoc}
43
+ *
44
+ * @final since Symfony 4.3, use getState() instead
45
+ *
46
+ * @internal since Symfony 4.3, use getState() instead
43
47
*/
44
48
public function serialize ()
45
49
{
46
- $ serialized = [
47
- $ this ->token ,
48
- $ this ->code ,
49
- $ this ->message ,
50
- $ this ->file ,
51
- $ this ->line ,
52
- ];
50
+ return $ this ->doSerialize ($ this ->getState (), \func_num_args () ? \func_get_arg (0 ) : null );
51
+ }
52
+
53
+ /**
54
+ * {@inheritdoc}
55
+ *
56
+ * @final since Symfony 4.3, use setState() instead
57
+ *
58
+ * @internal since Symfony 4.3, use setState() instead
59
+ */
60
+ public function unserialize ($ serialized )
61
+ {
62
+ $ this ->setState (\is_array ($ serialized ) ? $ serialized : unserialize ($ serialized ));
63
+ }
64
+
65
+ /**
66
+ * Returns all the necessary state of the object for serialization purposes.
67
+ *
68
+ * There is no need to serialize any entry, they should be returned as-is.
69
+ * If you extend this method, keep in mind you MUST guarantee parent data is present in the state.
70
+ * Here is an example of how to extend this method:
71
+ * <code>
72
+ * protected function getState(): array
73
+ * {
74
+ * return [$this->childAttribute, parent::getState()];
75
+ * }
76
+ * </code>
77
+ *
78
+ * @see setState()
79
+ */
80
+ protected function getState (): array
81
+ {
82
+ return [$ this ->token , $ this ->code , $ this ->message , $ this ->file , $ this ->line ];
83
+ }
53
84
54
- return $ this ->doSerialize ($ serialized , \func_num_args () ? \func_get_arg (0 ) : null );
85
+ /**
86
+ * Restores the object state from an array given by getState().
87
+ *
88
+ * There is no need to unserialize any entry in $data, they are already ready-to-use.
89
+ * If you extend this method, keep in mind you MUST pass the parent data to its respective class.
90
+ * Here is an example of how to extend this method:
91
+ * <code>
92
+ * protected function setState(array $data)
93
+ * {
F438
94
+ * [$this->childAttribute, $parentData] = $data;
95
+ * parent::setState($parentData);
96
+ * }
97
+ * </code>
98
+ *
99
+ * @see getState()
100
+ */
101
+ protected function setState (array $ data )
102
+ {
103
+ [$ this ->token , $ this ->code , $ this ->message , $ this ->file , $ this ->line ] = $ data ;
55
104
}
56
105
57
106
/**
@@ -67,17 +116,6 @@ protected function doSerialize($serialized, $isCalledFromOverridingMethod)
67
116
return $ isCalledFromOverridingMethod ? $ serialized : serialize ($ serialized );
68
117
}
69
118
70
- public function unserialize ($ str )
71
- {
72
- list (
73
- $ this ->token ,
74
- $ this ->code ,
75
- $ this ->message ,
76
- $ this ->file ,
77
- $ this ->line
78
- ) = \is_array ($ str ) ? $ str : unserialize ($ str );
79
- }
80
-
81
119
/**
82
120
* Message key to be used by the translation component.
83
121
*
0 commit comments