Skip to content

Commit d8cbb56

Browse files
author
Gennadi Janzen
committed
Add AbstractBinaryUidType
1 parent c415768 commit d8cbb56

File tree

6 files changed

+95
-82
lines changed

6 files changed

+95
-82
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Types;
13+
14+
use Doctrine\DBAL\Platforms\AbstractPlatform;
15+
use Doctrine\DBAL\Types\ConversionException;
16+
use Doctrine\DBAL\Types\GuidType;
17+
use Symfony\Component\Uid\AbstractUid;
18+
use Symfony\Component\Uid\Uuid;
19+
20+
abstract class AbstractBinaryUidType extends GuidType
21+
{
22+
abstract protected function getUidObject(): AbstractUid;
23+
24+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
25+
{
26+
return $platform->getBinaryTypeDeclarationSQL(
27+
[
28+
'length' => '16',
29+
'fixed' => true,
30+
]
31+
);
32+
}
33+
34+
/**
35+
* @throws ConversionException
36+
*/
37+
public function convertToPHPValue($value, AbstractPlatform $platform): ?AbstractUid
38+
{
39+
if (null === $value || '' === $value) {
40+
return null;
41+
}
42+
43+
if ($value instanceof AbstractUid) {
44+
return $value;
45+
}
46+
47+
try {
48+
$uuid = $this->getUidObject()::fromString($value);
49+
} catch (\InvalidArgumentException $e) {
50+
throw ConversionException::conversionFailed($value, $this->getName());
51+
}
52+
53+
return $uuid;
54+
}
55+
56+
/*
57+
* @throws ConversionException
58+
*/
59+
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
60+
{
61+
if (null === $value || '' === $value) {
62+
return null;
63+
}
64+
65+
if ($value instanceof AbstractUid) {
66+
return $value->toBinary();
67+
}
68+
69+
try {
70+
if (\is_string($value) || method_exists($value, '__toString')) {
71+
return $this->getUidObject()::fromString((string) $value)->toBinary();
72+
}
73+
} catch (\InvalidArgumentException $e) {
74+
throw ConversionException::conversionFailed($value, $this->getName());
75+
}
76+
}
77+
}

src/Symfony/Bridge/Doctrine/Types/AbstractUidType.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
5151
return null;
5252
}
5353

54+
if ($value instanceof AbstractUid) {
55+
return $value;
56+
}
57+
5458
if (
55-
$value instanceof AbstractUid
56-
|| (
57-
(\is_string($value) || method_exists($value, '__toString'))
58-
&& $this->getUidObject()::isValid((string) $value))
59+
(\is_string($value) || method_exists($value, '__toString'))
60+
&& $this->getUidObject()::isValid((string)$value)
5961
) {
60-
return (string) $value;
62+
return (string)$value;
6163
}
6264

6365
throw ConversionException::conversionFailed($value, $this->getName());

src/Symfony/Bridge/Doctrine/Types/UlidBinaryType.php

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,19 @@
1414
use Doctrine\DBAL\Platforms\AbstractPlatform;
1515
use Doctrine\DBAL\Types\ConversionException;
1616
use Symfony\Component\Uid\AbstractUid;
17+
use Symfony\Component\Uid\Ulid;
1718
use Symfony\Component\Uid\Uuid;
1819

19-
final class UlidBinaryType extends UlidType
20+
final class UlidBinaryType extends AbstractBinaryUidType
2021
{
21-
private const NAME = 'ulid_binary';
22-
2322
public function getName(): string
2423
{
25-
return static::NAME;
26-
}
27-
28-
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
29-
{
30-
return $platform->getBinaryTypeDeclarationSQL(
31-
[
32-
'length' => '16',
33-
'fixed' => true,
34-
]
35-
);
24+
return 'ulid_binary';
3625
}
3726

38-
/**
39-
* @throws ConversionException
40-
*/
41-
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
42-
{
43-
if (null === $value || '' === $value) {
44-
return null;
45-
}
46-
47-
if ($value instanceof AbstractUid) {
48-
return $value->toBinary();
49-
}
50-
51-
try {
52-
if (\is_string($value) || method_exists($value, '__toString')) {
53-
return Uuid::fromString((string) $value)->toBinary();
54-
}
55-
} catch (\InvalidArgumentException $e) {
56-
throw ConversionException::conversionFailed($value, static::NAME);
57-
}
58-
}
5927

60-
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
28+
protected function getUidObject(): AbstractUid
6129
{
62-
return true;
30+
return new Ulid();
6331
}
6432
}

src/Symfony/Bridge/Doctrine/Types/UlidType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Uid\AbstractUid;
1515
use Symfony\Component\Uid\Ulid;
1616

17-
class UlidType extends AbstractUidType
17+
final class UlidType extends AbstractUidType
1818
{
1919
public function getName(): string
2020
{

src/Symfony/Bridge/Doctrine/Types/UuidBinaryType.php

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,15 @@
1616
use Symfony\Component\Uid\AbstractUid;
1717
use Symfony\Component\Uid\Uuid;
1818

19-
final class UuidBinaryType extends UuidType
19+
final class UuidBinaryType extends AbstractBinaryUidType
2020
{
21-
private const NAME = 'uuid_binary';
22-
2321
public function getName(): string
2422
{
25-
return static::NAME;
26-
}
27-
28-
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
29-
{
30-
return $platform->getBinaryTypeDeclarationSQL(
31-
[
32-
'length' => '16',
33-
'fixed' => true,
34-
]
35-
);
36-
}
37-
38-
/**
39-
* @throws ConversionException
40-
*/
41-
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
42-
{
43-
if (null === $value || '' === $value) {
44-
return null;
45-
}
46-
47-
if ($value instanceof AbstractUid) {
48-
return $value->toBinary();
49-
}
50-
51-
try {
52-
if (\is_string($value) || method_exists($value, '__toString')) {
53-
return Uuid::fromString((string) $value)->toBinary();
54-
}
55-
} catch (\InvalidArgumentException $e) {
56-
throw ConversionException::conversionFailed($value, static::NAME);
57-
}
23+
return 'uuid_binary';
5824
}
5925

60-
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
26+
protected function getUidObject(): AbstractUid
6127
{
62-
return true;
28+
return Uuid::v4();
6329
}
6430
}

src/Symfony/Bridge/Doctrine/Types/UuidType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Uid\AbstractUid;
1515
use Symfony\Component\Uid\Uuid;
1616

17-
class UuidType extends AbstractUidType
17+
final class UuidType extends AbstractUidType
1818
{
1919
public function getName(): string
2020
{

0 commit comments

Comments
 (0)