Skip to content

Commit

Permalink
Make serialize(UuidInterface) more compact
Browse files Browse the repository at this point in the history
Resolves #318
  • Loading branch information
ramsey committed Jul 8, 2020
1 parent a741fc6 commit 22ee596
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/Fields/SerializableFieldsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace Ramsey\Uuid\Fields;

use function base64_decode;
use function base64_encode;
use function strlen;

/**
* Provides common serialization functionality to fields
Expand All @@ -39,7 +39,7 @@ abstract public function getBytes(): string;
*/
public function serialize(): string
{
return base64_encode($this->getBytes());
return $this->getBytes();
}

/**
Expand All @@ -51,6 +51,10 @@ public function serialize(): string
*/
public function unserialize($serialized): void
{
$this->__construct(base64_decode($serialized));
if (strlen($serialized) === 16) {
$this->__construct($serialized);
} else {
$this->__construct(base64_decode($serialized));
}
}
}
12 changes: 9 additions & 3 deletions src/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function jsonSerialize(): string
*/
public function serialize(): string
{
return $this->toString();
return $this->getBytes();
}

/**
Expand All @@ -286,8 +286,14 @@ public function serialize(): string
*/
public function unserialize($serialized): void
{
/** @var \Ramsey\Uuid\Uuid $uuid */
$uuid = self::fromString($serialized);
if (strlen($serialized) === 16) {
/** @var Uuid $uuid */
$uuid = self::fromBytes($serialized);
} else {
/** @var Uuid $uuid */
$uuid = self::fromString($serialized);
}

$this->codec = $uuid->codec;
$this->numberConverter = $uuid->numberConverter;
$this->fields = $uuid->fields;
Expand Down
10 changes: 10 additions & 0 deletions tests/Rfc4122/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,14 @@ public function testSerializingFields(): void

$this->assertEquals($fields, $unserializedFields);
}

public function testSerializingFieldsWithOldFormat(): void
{
$fields = new Fields("\xb3\xcd\x58\x6a\xe3\xca\x44\xf3\x98\x8c\xf4\xd6\x66\xc1\xbf\x4d");

$serializedFields = 'C:26:"Ramsey\Uuid\Rfc4122\Fields":24:{s81YauPKRPOYjPTWZsG/TQ==}';
$unserializedFields = unserialize($serializedFields);

$this->assertEquals($fields, $unserializedFields);
}
}
10 changes: 10 additions & 0 deletions tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,16 @@ public function testSerialize(): void
$this->assertTrue($uuid->equals($unserializedUuid));
}

public function testSerializeWithOldStringFormat(): void
{
$serialized = 'C:26:"Ramsey\Uuid\Rfc4122\UuidV4":36:{b3cd586a-e3ca-44f3-988c-f4d666c1bf4d}';

/** @var UuidInterface $unserializedUuid */
$unserializedUuid = unserialize($serialized);

$this->assertSame('b3cd586a-e3ca-44f3-988c-f4d666c1bf4d', $unserializedUuid->toString());
}

public function testUuid3WithEmptyNamespace(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down

0 comments on commit 22ee596

Please sign in to comment.