Fixes #91: Made Uuid's serializable.

This commit is contained in:
frankdejonge
2015-10-25 22:15:32 +01:00
parent f77767e6f0
commit 91d0c6da31
3 changed files with 35 additions and 1 deletions
+26
View File
@@ -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;
+1 -1
View File
@@ -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.
+8
View File
@@ -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));
}
}