Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle PostgreSQL binary resources #229

Merged
merged 5 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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