Skip to content

Commit

Permalink
Merge pull request #92 from frankdejonge/master
Browse files Browse the repository at this point in the history
Fixes #91: Made Uuid's serializable.
  • Loading branch information
ramsey committed Oct 25, 2015
2 parents f77767e + 91d0c6d commit 823f8bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/Uuid.php
Expand Up @@ -187,6 +187,32 @@ public function jsonSerialize()
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;
Expand Down
2 changes: 1 addition & 1 deletion src/UuidInterface.php
Expand Up @@ -21,7 +21,7 @@
* 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.
Expand Down
8 changes: 8 additions & 0 deletions tests/src/UuidTest.php
Expand Up @@ -1840,4 +1840,12 @@ public function testJsonSerialize()

$this->assertEquals('"' . $uuid->toString() . '"', json_encode($uuid));
}

public function testSerialize()
{
$uuid = Uuid::uuid4();
$serialized = serialize($uuid);
$unserializedUuid = unserialize($serialized);
$this->assertTrue($uuid->equals($unserializedUuid));
}
}

0 comments on commit 823f8bc

Please sign in to comment.