mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-14 15:56:48 +03:00
@@ -15,7 +15,7 @@ declare(strict_types=1);
|
||||
namespace Ramsey\Uuid\Fields;
|
||||
|
||||
use function base64_decode;
|
||||
use function base64_encode;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* Provides common serialization functionality to fields
|
||||
@@ -39,7 +39,7 @@ trait SerializableFieldsTrait
|
||||
*/
|
||||
public function serialize(): string
|
||||
{
|
||||
return base64_encode($this->getBytes());
|
||||
return $this->getBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,6 +51,10 @@ trait SerializableFieldsTrait
|
||||
*/
|
||||
public function unserialize($serialized): void
|
||||
{
|
||||
$this->__construct(base64_decode($serialized));
|
||||
if (strlen($serialized) === 16) {
|
||||
$this->__construct($serialized);
|
||||
} else {
|
||||
$this->__construct(base64_decode($serialized));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+9
-3
@@ -273,7 +273,7 @@ class Uuid implements UuidInterface
|
||||
*/
|
||||
public function serialize(): string
|
||||
{
|
||||
return $this->toString();
|
||||
return $this->getBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -286,8 +286,14 @@ class Uuid implements UuidInterface
|
||||
*/
|
||||
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;
|
||||
|
||||
@@ -212,4 +212,14 @@ class FieldsTest extends TestCase
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1538,6 +1538,16 @@ class UuidTest extends TestCase
|
||||
$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);
|
||||
|
||||
Reference in New Issue
Block a user