Skip to content

Commit d0b62b4

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

File tree

3 files changed

+125
-0
lines changed

3 files changed

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

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy