Skip to content

Commit

Permalink
Merge pull request #125 from marc-mabe/php74-serialize
Browse files Browse the repository at this point in the history
PHP 7.4 serializer and wrap old Serializable methods for BC
  • Loading branch information
marc-mabe authored Dec 10, 2019
2 parents 84bf0f6 + a2bdae8 commit c60b29b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
59 changes: 46 additions & 13 deletions src/EnumSerializableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,34 @@ trait EnumSerializableTrait
abstract public function getValue();

/**
* Serialized the value of the enumeration
* This will be called automatically on `serialize()` if the enumeration implements the `Serializable` interface
* @return string
* Returns an array of data to be serialized.
* This magic method will be called by serialize() in PHP >= 7.4
*
* @return array
*/
public function serialize(): string
public function __serialize(): array
{
return \serialize($this->getValue());
return ['value' => $this->getValue()];
}

/**
* Unserializes a given serialized value and push it into the current instance
* This will be called automatically on `unserialize()` if the enumeration implements the `Serializable` interface
* @param string $serialized
* Receives an array of data to be unserialized on a new instance without constructor.
* This magic method will be called in PHP >= 7.4 is the data where serialized with PHP >= 7.4.
*
* @throws RuntimeException On missing, unknown or invalid value
* @throws LogicException On calling this method on an already initialized enumerator
*
* @param array $data
* @return void
* @throws RuntimeException On an unknown or invalid value
* @throws LogicException On changing numeration value by calling this directly
*/
public function unserialize($serialized): void
public function __unserialize(array $data): void
{
$value = \unserialize($serialized);
$constants = static::getConstants();
if (!\array_key_exists('value', $data)) {
throw new RuntimeException('Missing array key "value"');
}

$value = $data['value'];
$constants = self::getConstants();
$name = \array_search($value, $constants, true);
if ($name === false) {
$message = \is_scalar($value)
Expand All @@ -73,4 +80,30 @@ public function unserialize($serialized): void
};
$closure->bindTo($this, Enum::class)();
}

/**
* Serialize the value of the enumeration
* This will be called automatically on `serialize()` if the enumeration implements the `Serializable` interface
*
* @return string
* @deprecated Since PHP 7.4
*/
public function serialize(): string
{
return \serialize($this->getValue());
}

/**
* Unserializes a given serialized value and push it into the current instance
* This will be called automatically on `unserialize()` if the enumeration implements the `Serializable` interface
* @param string $serialized
* @return void
* @throws RuntimeException On an unknown or invalid value
* @throws LogicException On calling this method on an already initialized enumerator
* @deprecated Since PHP 7.4
*/
public function unserialize($serialized): void
{
$this->__unserialize(['value' => \unserialize($serialized)]);
}
}
14 changes: 12 additions & 2 deletions tests/MabeEnumTest/EnumSerializableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ public function testUnserializeThrowsRuntimeExceptionOnInvalidValue()

public function testUnserializeThrowsLogicExceptionOnChangingValue()
{
$enumInt = SerializableEnum::get(SerializableEnum::INT);
$enumStrSer = SerializableEnum::STR()->__serialize();

$this->expectException(LogicException::class);
$enum = SerializableEnum::get(SerializableEnum::INT);
$enum->unserialize(serialize(SerializableEnum::STR));
$enumInt->__unserialize($enumStrSer);
}

public function testInheritence()
Expand All @@ -79,6 +81,14 @@ public function testInheritence()
$this->assertSame($enum->getValue(), $unserialized->getValue());
}

public function testUnserializeFromPhp73()
{
$serialized = 'C:39:"MabeEnumTest\TestAsset\SerializableEnum":2:{N;}';
$unserialized = unserialize($serialized);
$this->assertInstanceOf(SerializableEnum::class, $unserialized);
$this->assertNull($unserialized->getValue());
}

/**
* Clears all instantiated enumerations and detected constants of the given enumerator
* @param string $enumeration
Expand Down

0 comments on commit c60b29b

Please sign in to comment.