8000 Add: Ulid and Uuid as Doctrine Types · symfony/symfony@d3af2ba · GitHub
[go: up one dir, main page]

Skip to content

Commit d3af2ba

Browse files
Add: Ulid and Uuid as Doctrine Types
1 parent 0ab5eed commit d3af2ba

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Bridge\Doctrine\Types;
6+
7+
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use Doctrine\DBAL\Types\ConversionException;
9+
use Doctrine\DBAL\Types\GuidType;
10+
use InvalidArgumentException;
11+
use Symfony\Component\Uid\AbstractUid;
12+
use Symfony\Component\Uid\Ulid;
13+
14+
final class UlidType extends GuidType
15+
{
16+
private const NAME = 'ulid';
17+
18+
/**
19+
* @throws ConversionException
20+
*/
21+
public function convertToPHPValue($value, AbstractPlatform $platform)
22+
{
23+
if (null === $value || '' === $value) {
24+
return null;
25+
}
26+
27+
if ($value instanceof AbstractUid) {
28+
return $value;
29+
}
30+
31+
try {
32+
return Ulid::fromString($value);
33+
} catch (InvalidArgumentException $e) {
34+
throw ConversionException::conversionFailed($value, static::NAME);
35+
}
36+
}
37+
38+
/*
39+
* @throws ConversionException
40+
*/
41+
public function convertToDatabaseValue($value, AbstractPlatform $platform)
42+
{
43+
if (null === $value || '' === $value) {
44+
return null;
45+
}
46+
47+
if (
48+
$value instanceof AbstractUid
49+
|| (
50+
(is_string($value) || method_exists($value, '__toString'))
51+
&& Ulid::isValid((string) $value))
52+
) {
53+
return (string) $value;
54+
}
55+
56+
throw ConversionException::conversionFailed($value, static::NAME);
57+
}
58+
59+
public function getName(): string
60+
{
61+
return static::NAME;
62+
}
63+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Bridge\Doctrine\Types;
6+
7+
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use Doctrine\DBAL\Types\ConversionException;
9+
use Doctrine\DBAL\Types\GuidType;
10+
use InvalidArgumentException;
11+
use Symfony\Component\Uid\AbstractUid;
12+
use Symfony\Component\Uid\Uuid;
13+
14+
final class UuidType extends GuidType
15+
{
16+
private const NAME = 'uuid';
17+
18+
/**
19+
* @throws ConversionException
20+
*/
21+
public function convertToPHPValue($value, AbstractPlatform $platform)
22+
{
23+
if (null === $value || '' === $value) {
24+
return null;
25+
}
26+
27+
if ($value instanceof AbstractUid) {
28+
return $value;
29+
}
30+
31+
try {
32+
$uuid = Uuid::fromString($value);
33+
} catch (InvalidArgumentException $e) {
34+
throw ConversionException::conversionFailed($value, static::NAME);
35+
}
36+
37+
return $uuid;
38+
}
39+
40+
/*
41+
* @throws ConversionException
42+
*/
43+
public function convertToDatabaseValue($value, AbstractPlatform $platform)
44+
{
45+
if (null === $value || '' === $value) {
46+
return null;
47+
}
48+
49+
if (
50+
$value instanceof AbstractUid
51+
|| (
52+
(is_string($value) || method_exists($value, '__toString'))
53+
&& Uuid::isValid((string) $value))
54+
) {
55+
return (string) $value;
56+
}
57+
58+
throw ConversionException::conversionFailed($value, static::NAME);
59+
}
60+
61+
public function getName(): string
62+
{
63+
return static::NAME;
64+
}
65+
}

src/Symfony/Bridge/Doctrine/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"symfony/polyfill-ctype": "~1.8",
2323
"symfony/polyfill-mbstring": "~1.0",
2424
"symfony/polyfill-php80": "^1.15",
25+
"symfony/uid": "^5.1",
2526
"symfony/service-contracts": "^1.1|^2"
2627
},
2728
"require-dev": {

0 commit comments

Comments
 (0)
0