1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Thunder\Platenum\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use Thunder\Platenum\Enum\AbstractConstantsEnum; |
8
|
|
|
use Thunder\Platenum\Enum\EnumTrait; |
9
|
|
|
|
10
|
|
|
/** @psalm-suppress PropertyNotSetInConstructor, MissingConstructor */ |
11
|
|
|
final class PlatenumDoctrineType extends Type |
12
|
|
|
{ |
13
|
|
|
/** @var class-string */ |
|
|
|
|
14
|
|
|
private $platenumClass; |
15
|
|
|
/** @var string */ |
16
|
|
|
private $platenumAlias; |
17
|
|
|
/** @var callable(mixed):mixed */ |
18
|
|
|
private $platenumCallback; |
19
|
|
|
/** @psalm-var callable(array,AbstractPlatform):string */ |
20
|
|
|
private $platenumSql; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $alias |
24
|
|
|
* @psalm-param class-string $class |
25
|
3 |
|
*/ |
26
|
|
|
public static function registerInteger(string $alias, string $class): void |
27
|
|
|
{ |
28
|
3 |
|
/** @psalm-suppress MissingClosureParamType */ |
29
|
1 |
|
$toInteger = function($value): int { |
30
|
3 |
|
return (int)$value; |
31
|
3 |
|
}; |
32
|
1 |
|
$sql = function(array $declaration, AbstractPlatform $platform): string { |
33
|
3 |
|
return $platform->getIntegerTypeDeclarationSQL([]); |
34
|
|
|
}; |
35
|
3 |
|
|
36
|
2 |
|
self::registerCallback($alias, $class, $toInteger, $sql); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $alias |
41
|
|
|
* @psalm-param class-string $class |
42
|
5 |
|
*/ |
43
|
|
|
public static function registerString(string $alias, string $class): void |
44
|
|
|
{ |
45
|
5 |
|
/** @psalm-suppress MissingClosureParamType */ |
46
|
2 |
|
$toString = function($value): string { |
47
|
5 |
|
return (string)$value; |
48
|
5 |
|
}; |
49
|
1 |
|
$sql = function(array $declaration, AbstractPlatform $platform): string { |
50
|
5 |
|
return method_exists($platform, 'getStringTypeDeclarationSQL') |
51
|
|
|
? $platform->getStringTypeDeclarationSQL([]) |
52
|
5 |
|
: $platform->getVarcharTypeDeclarationSQL([]); |
|
|
|
|
53
|
5 |
|
}; |
54
|
|
|
|
55
|
|
|
self::registerCallback($alias, $class, $toString, $sql); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $alias |
60
|
|
|
* @psalm-param class-string $class |
61
|
6 |
|
* @param callable(int|string):mixed $callback |
62
|
|
|
* @psalm-param callable(array<mixed>,AbstractPlatform):string $sql |
63
|
6 |
|
*/ |
64
|
1 |
|
private static function registerCallback(string $alias, string $class, callable $callback, callable $sql): void |
65
|
|
|
{ |
66
|
6 |
|
if(self::hasType($alias)) { |
67
|
1 |
|
throw new \LogicException(sprintf('Alias `%s` was already registered in PlatenumDoctrineType.', $class)); |
68
|
|
|
} |
69
|
|
|
if(false === in_array(EnumTrait::class, self::allTraitsOf($class), true)) { |
70
|
5 |
|
throw new \LogicException(sprintf('PlatenumDoctrineType allows only Platenum enumerations, `%s` given.', $class)); |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
self::addType($alias, self::class); |
74
|
5 |
|
|
75
|
5 |
|
/** @var static $type */ |
76
|
5 |
|
$type = self::getType($alias); |
77
|
5 |
|
$type->platenumAlias = $alias; |
78
|
5 |
|
$type->platenumClass = $class; |
79
|
|
|
$type->platenumCallback = $callback; |
80
|
|
|
$type->platenumSql = $sql; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
6 |
|
* @param class-string $class |
|
|
|
|
85
|
|
|
* @psalm-return list<string> |
86
|
6 |
|
*/ |
87
|
|
|
private static function allTraitsOf(string $class): array |
88
|
|
|
{ |
89
|
6 |
|
$traits = []; |
90
|
5 |
|
|
91
|
|
|
do { |
92
|
6 |
|
foreach(class_uses($class, true) as $fqcn) { |
93
|
|
|
$traits[] = $fqcn; |
94
|
6 |
|
} |
95
|
5 |
|
} while($class = get_parent_class($class)); |
96
|
5 |
|
|
97
|
|
|
foreach ($traits as /* $trait => */ $same) { |
98
|
|
|
foreach(class_uses($same, true) as $fqcn) { |
99
|
|
|
$traits[] = $fqcn; |
100
|
6 |
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return $traits; |
104
|
|
|
} |
105
|
1 |
|
|
106
|
|
|
public function getName(): string |
107
|
|
|
{ |
108
|
|
|
return $this->platenumAlias; |
109
|
1 |
|
} |
110
|
|
|
|
111
|
1 |
|
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string |
112
|
|
|
{ |
113
|
|
|
return ($this->platenumSql)($column, $platform); |
114
|
3 |
|
} |
115
|
|
|
|
116
|
3 |
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
117
|
1 |
|
{ |
118
|
|
|
if (null === $value) { |
119
|
3 |
|
return null; |
120
|
1 |
|
} |
121
|
1 |
|
if(false === is_object($value)) { |
122
|
|
|
$message = 'Impossible situation: `%s` allows to register only Platenum types, `%s` given.'; |
123
|
|
|
throw new \LogicException(sprintf($message, self::class, gettype($value))); |
124
|
|
|
} |
125
|
2 |
|
|
126
|
|
|
/** @var AbstractConstantsEnum $value */ |
127
|
|
|
return call_user_func($this->platenumCallback, $value->getValue()); |
128
|
1 |
|
} |
129
|
|
|
|
130
|
1 |
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
131
|
1 |
|
{ |
132
|
|
|
if (null === $value) { |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
1 |
|
|
136
|
|
|
/** @psalm-suppress MixedMethodCall */ |
137
|
|
|
return ($this->platenumClass)::fromValue(($this->platenumCallback)($value)); |
138
|
1 |
|
} |
139
|
|
|
|
140
|
1 |
|
public function requiresSQLCommentHint(AbstractPlatform $platform): bool |
141
|
|
|
{ |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|