Skip to content

Commit

Permalink
Handle PostgreSQL binary resources (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
jankal authored May 26, 2024
1 parent 61ce825 commit 5eae94d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/UuidBinaryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/UuidBinaryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 5eae94d

Please sign in to comment.