@@ -29,27 +29,34 @@ trait EnumSerializableTrait
2929 abstract public function getValue ();
3030
3131 /**
32- * Serialized the value of the enumeration
33- * This will be called automatically on `serialize()` if the enumeration implements the `Serializable` interface
34- * @return string
32+ * Returns an array of data to be serialized.
33+ * This magic method will be called by serialize() in PHP >= 7.4
34+ *
35+ * @return array
3536 */
36- public function serialize (): string
37+ public function __serialize (): array
3738 {
38- return \serialize ( $ this ->getValue ()) ;
39+ return [ ' value ' => $ this ->getValue ()] ;
3940 }
4041
4142 /**
42- * Unserializes a given serialized value and push it into the current instance
43- * This will be called automatically on `unserialize()` if the enumeration implements the `Serializable` interface
44- * @param string $serialized
43+ * Receives an array of data to be unserialized on a new instance without constructor.
44+ * This magic method will be called in PHP >= 7.4 is the data where serialized with PHP >= 7.4.
45+ *
46+ * @throws RuntimeException On missing, unknown or invalid value
47+ * @throws LogicException On calling this method on an already initialized enumerator
48+ *
49+ * @param array $data
4550 * @return void
46- * @throws RuntimeException On an unknown or invalid value
47- * @throws LogicException On changing numeration value by calling this directly
4851 */
49- public function unserialize ( $ serialized ): void
52+ public function __unserialize ( array $ data ): void
5053 {
51- $ value = \unserialize ($ serialized );
52- $ constants = static ::getConstants ();
54+ if (!\array_key_exists ('value ' , $ data )) {
55+ throw new RuntimeException ('Missing array key "value" ' );
56+ }
57+
58+ $ value = $ data ['value ' ];
59+ $ constants = self ::getConstants ();
5360 $ name = \array_search ($ value , $ constants , true );
5461 if ($ name === false ) {
5562 $ message = \is_scalar ($ value )
@@ -73,4 +80,30 @@ public function unserialize($serialized): void
7380 };
7481 $ closure ->bindTo ($ this , Enum::class)();
7582 }
83+
84+ /**
85+ * Serialize the value of the enumeration
86+ * This will be called automatically on `serialize()` if the enumeration implements the `Serializable` interface
87+ *
88+ * @return string
89+ * @deprecated Since PHP 7.4
90+ */
91+ public function serialize (): string
92+ {
93+ return \serialize ($ this ->getValue ());
94+ }
95+
96+ /**
97+ * Unserializes a given serialized value and push it into the current instance
98+ * This will be called automatically on `unserialize()` if the enumeration implements the `Serializable` interface
99+ * @param string $serialized
100+ * @return void
101+ * @throws RuntimeException On an unknown or invalid value
102+ * @throws LogicException On calling this method on an already initialized enumerator
103+ * @deprecated Since PHP 7.4
104+ */
105+ public function unserialize ($ serialized ): void
106+ {
107+ $ this ->__unserialize (['value ' => \unserialize ($ serialized )]);
108+ }
76109}
0 commit comments