1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ecodev\Felix\DBAL\Types; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
use Exception; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
|
13
|
|
|
abstract class SetType extends Type |
14
|
|
|
{ |
15
|
1 |
|
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string |
16
|
|
|
{ |
17
|
1 |
|
$possibleValues = $this->getPossibleValues(); |
18
|
1 |
|
$quotedPossibleValues = implode(', ', array_map(fn ($str) => "'" . $str . "'", $possibleValues)); |
19
|
|
|
|
20
|
1 |
|
$sql = 'SET(' . $quotedPossibleValues . ')'; |
21
|
|
|
|
22
|
1 |
|
return $sql; |
23
|
|
|
} |
24
|
|
|
|
25
|
3 |
|
public function convertToPHPValue($value, AbstractPlatform $platform): ?array |
26
|
|
|
{ |
27
|
3 |
|
if ($value === null) { |
28
|
1 |
|
return null; |
29
|
|
|
} |
30
|
|
|
|
31
|
3 |
|
$values = is_string($value) ? preg_split('/,/', $value, -1, PREG_SPLIT_NO_EMPTY) : null; |
32
|
3 |
|
if (!$this->isValid($values)) { |
33
|
2 |
|
throw new InvalidArgumentException("Invalid '" . $value . "' value fetched from database for set " . $this->getName()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** @var array $values */ |
37
|
1 |
|
return $values; |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
public function convertToDatabaseValue($values, AbstractPlatform $platform): ?string |
41
|
|
|
{ |
42
|
4 |
|
if ($values === null) { |
43
|
1 |
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
$value = is_array($values) ? implode(',', $values) : null; |
47
|
4 |
|
if (!$this->isValid($values)) { |
48
|
3 |
|
throw new InvalidArgumentException("Invalid '" . $value . "' value to be stored in database for set " . $this->getName()); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
6 |
|
private function isValid(mixed $values): bool |
55
|
|
|
{ |
56
|
6 |
|
if (!is_array($values)) { |
57
|
3 |
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
$possibleValues = $this->getPossibleValues(); |
61
|
3 |
|
foreach ($values as $value) { |
62
|
3 |
|
if (!in_array($value, $possibleValues, true)) { |
63
|
2 |
|
return false; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return all possibles values as an array of string. |
72
|
|
|
* |
73
|
|
|
* @return string[] |
74
|
|
|
*/ |
75
|
|
|
abstract protected function getPossibleValues(): array; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the type name based on actual class name and possible values. |
79
|
|
|
*/ |
80
|
6 |
|
public function getName(): string |
81
|
|
|
{ |
82
|
6 |
|
$class = new ReflectionClass($this); |
83
|
6 |
|
$shortClassName = $class->getShortName(); |
84
|
6 |
|
$typeName = preg_replace('/Type$/', '', $shortClassName); |
85
|
|
|
|
86
|
6 |
|
if ($typeName === null) { |
87
|
|
|
throw new Exception('Could not extract set name from class name'); |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
return $typeName; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function requiresSQLCommentHint(AbstractPlatform $platform) |
94
|
|
|
{ |
95
|
1 |
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getMappedDatabaseTypes(AbstractPlatform $platform) |
99
|
|
|
{ |
100
|
|
|
return ['set']; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|