mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-16 16:17:43 +03:00
Fixes #91: Made Uuid's serializable.
This commit is contained in:
@@ -187,6 +187,32 @@ class Uuid implements UuidInterface
|
||||
return $this->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this UUID object to a string when the object is serialized
|
||||
* with `serialize()`
|
||||
*
|
||||
* @return string
|
||||
* @link http://php.net/manual/en/class.serializable.php
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-constructs the object from its serialized form.
|
||||
*
|
||||
* @param string $serialized
|
||||
* @link http://php.net/manual/en/class.serializable.php
|
||||
*/
|
||||
public function unserialize($serialized)
|
||||
{
|
||||
$uuid = self::fromString($serialized);
|
||||
$this->codec = $uuid->codec;
|
||||
$this->converter = $uuid->converter;
|
||||
$this->fields = $uuid->fields;
|
||||
}
|
||||
|
||||
public function compareTo(UuidInterface $other)
|
||||
{
|
||||
$comparison = 0;
|
||||
|
||||
@@ -21,7 +21,7 @@ use Ramsey\Uuid\Exception\UnsupportedOperationException;
|
||||
* UuidInterface defines common functionality for all universally unique
|
||||
* identifiers (UUIDs)
|
||||
*/
|
||||
interface UuidInterface extends \JsonSerializable
|
||||
interface UuidInterface extends \JsonSerializable, \Serializable
|
||||
{
|
||||
/**
|
||||
* Compares this UUID to the specified UUID.
|
||||
|
||||
@@ -1840,4 +1840,12 @@ class UuidTest extends TestCase
|
||||
|
||||
$this->assertEquals('"' . $uuid->toString() . '"', json_encode($uuid));
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$uuid = Uuid::uuid4();
|
||||
$serialized = serialize($uuid);
|
||||
$unserializedUuid = unserialize($serialized);
|
||||
$this->assertTrue($uuid->equals($unserializedUuid));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user