From 5eae94d6122c3b46048480f46f3e7aaf964c1c1b Mon Sep 17 00:00:00 2001 From: Alexander Jank Date: Mon, 27 May 2024 01:43:31 +0200 Subject: [PATCH] Handle PostgreSQL binary resources (#229) --- src/UuidBinaryType.php | 6 ++++++ tests/UuidBinaryTypeTest.php | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/UuidBinaryType.php b/src/UuidBinaryType.php index adf9a4b..04eed6e 100644 --- a/src/UuidBinaryType.php +++ b/src/UuidBinaryType.php @@ -24,8 +24,10 @@ use function class_exists; use function is_object; +use function is_resource; use function is_string; use function method_exists; +use function stream_get_contents; /** * Field type mapping for the Doctrine Database Abstraction Layer (DBAL). @@ -63,6 +65,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?UuidInte return $value; } + if (is_resource($value)) { + $value = stream_get_contents($value); + } + if (!is_string($value) || $value === '') { return null; } diff --git a/tests/UuidBinaryTypeTest.php b/tests/UuidBinaryTypeTest.php index ec41801..54d1d82 100644 --- a/tests/UuidBinaryTypeTest.php +++ b/tests/UuidBinaryTypeTest.php @@ -14,8 +14,11 @@ use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; +use function fopen; +use function fwrite; use function hex2bin; use function method_exists; +use function rewind; class UuidBinaryTypeTest extends TestCase { @@ -105,6 +108,21 @@ public function testNullConversionForPHPValue(): void $this->assertNull($this->getType()->convertToPHPValue(null, $this->getPlatform())); } + public function testResourceConvertsToPHPValue(): void + { + /** @var resource $stream */ + $stream = fopen('php://memory', 'r+b'); + /** @var string $binaryId */ + $binaryId = hex2bin('ff6f8cb0c57d11e19b210800200c9a66'); + + fwrite($stream, $binaryId); + rewind($stream); + + $uuid = $this->getType()->convertToPHPValue($stream, $this->getPlatform()); + $this->assertInstanceOf(UuidInterface::class, $uuid); + $this->assertSame('ff6f8cb0-c57d-11e1-9b21-0800200c9a66', $uuid->toString()); + } + public function testReturnValueIfUuid4ForPHPValue(): void { $uuid = Uuid::uuid4();