1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Schema\Adapter; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Schema\IndexInterface; |
6
|
|
|
use Bdf\Prime\Schema\Util\Name; |
7
|
|
|
use Doctrine\DBAL\Schema\AbstractAsset; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Add a name to the index, if not already provided |
11
|
|
|
* |
12
|
|
|
* The generated name will be same as doctrine generated name |
13
|
|
|
*/ |
14
|
|
|
final class NamedIndex implements IndexInterface |
15
|
|
|
{ |
16
|
|
|
public const FOR_PRIMARY = 'PRIMARY'; |
17
|
|
|
public const FOR_UNIQUE = 'UNIQ'; |
18
|
|
|
public const FOR_SIMPLE = 'IDX'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var IndexInterface |
22
|
|
|
*/ |
23
|
|
|
private $index; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $tableName; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* NamedIndex constructor. |
33
|
|
|
* |
34
|
|
|
* @param IndexInterface $index |
35
|
|
|
* @param string $tableName |
36
|
|
|
*/ |
37
|
739 |
|
public function __construct(IndexInterface $index, string $tableName) |
38
|
|
|
{ |
39
|
739 |
|
$this->index = $index; |
40
|
739 |
|
$this->tableName = $tableName; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
* |
46
|
|
|
* Generate the index name if not set. |
47
|
|
|
* For compatibility purpose, use same algo as Doctrine |
48
|
|
|
* |
49
|
|
|
* @see AbstractAsset::_generateIdentifierName() |
50
|
|
|
*/ |
51
|
736 |
|
public function name(): string |
52
|
|
|
{ |
53
|
736 |
|
$name = $this->index->name(); |
54
|
|
|
|
55
|
736 |
|
if ($this->isValidName($name)) { |
56
|
74 |
|
return $name; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
726 |
|
if ($this->index->primary()) { |
60
|
689 |
|
return self::FOR_PRIMARY; |
61
|
|
|
} |
62
|
|
|
|
63
|
91 |
|
return Name::generate( |
64
|
91 |
|
$this->index->unique() ? self::FOR_UNIQUE : self::FOR_SIMPLE, |
65
|
91 |
|
array_merge([$this->tableName], $this->fields()) |
66
|
91 |
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
699 |
|
public function unique(): bool |
73
|
|
|
{ |
74
|
699 |
|
return $this->index->unique(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
700 |
|
public function primary(): bool |
81
|
|
|
{ |
82
|
700 |
|
return $this->index->primary(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
8 |
|
public function type(): int |
89
|
|
|
{ |
90
|
8 |
|
return $this->index->type(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
727 |
|
public function fields(): array |
97
|
|
|
{ |
98
|
727 |
|
return $this->index->fields(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
1 |
|
public function isComposite(): bool |
105
|
|
|
{ |
106
|
1 |
|
return $this->index->isComposite(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
697 |
|
public function options(): array |
113
|
|
|
{ |
114
|
697 |
|
return $this->index->options(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
697 |
|
public function fieldOptions(string $field): array |
121
|
|
|
{ |
122
|
697 |
|
return $this->index->fieldOptions($field); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check if the name is valid |
127
|
|
|
* |
128
|
|
|
* @param string $name |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
* @psalm-assert-if-true non-empty-string $name |
132
|
|
|
*/ |
133
|
736 |
|
protected function isValidName($name) |
134
|
|
|
{ |
135
|
736 |
|
if (empty($name)) { |
136
|
723 |
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
77 |
|
if (!is_string($name)) { |
|
|
|
|
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ( |
144
|
77 |
|
!ctype_alpha($name[0]) |
145
|
77 |
|
&& $name[0] !== '_' |
146
|
|
|
) { |
147
|
12 |
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
74 |
|
return true; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|