forked from sabbelasichon/typo3-rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MigrateInputDateTimeRector.php
219 lines (191 loc) · 7.87 KB
/
MigrateInputDateTimeRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
declare(strict_types=1);
namespace Ssch\TYPO3Rector\TYPO312\v0;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use Ssch\TYPO3Rector\Helper\ArrayUtility;
use Ssch\TYPO3Rector\Rector\AbstractTcaRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Feature-97232-NewTCATypeDatetime.html
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Breaking-97358-RemovedEvalintFromTCATypeDatetime.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\tca\MigrateInputDateTimeRector\MigrateInputDateTimeRectorTest
*/
final class MigrateInputDateTimeRector extends AbstractTcaRector
{
/**
* @var string
*/
private const INPUT_DATE_TIME = 'inputDateTime';
/**
* @var string[]
*/
private const DATETIME_TYPES = ['date', 'datetime', 'time'];
/**
* @var array<string, array<string, string>>
*/
private const DATETIME_EMPTY_VALUES = [
'date' => [
'empty' => '0000-00-00',
],
'datetime' => [
'empty' => '0000-00-00 00:00:00',
],
'time' => [
'empty' => '00:00:00',
],
];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Migrate renderType inputDateTime to new TCA type datetime', [new CodeSample(
<<<'CODE_SAMPLE'
return [
'columns' => [
'a_datetime_field' => [
'label' => 'Datetime field',
'config' => [
'type' => 'input',
'renderType' => 'inputDateTime',
'required' => true,
'size' => 20,
'max' => 1024,
'eval' => 'date,int',
'default' => 0,
],
],
],
];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
return [
'columns' => [
'a_datetime_field' => [
'label' => 'Datetime field',
'config' => [
'type' => 'datetime',
'format' => 'date',
'required' => true,
'size' => 20,
'default' => 0,
],
],
],
];
CODE_SAMPLE
)]);
}
protected function refactorColumn(Expr $columnName, Expr $columnTca): void
{
$configArray = $this->extractSubArrayByKey($columnTca, self::CONFIG);
if (! $configArray instanceof Array_) {
return;
}
// Early return in case column is not of type=input with renderType=inputDateTime
if (! $this->isConfigType($configArray, 'input')
|| ! $this->configIsOfRenderType($configArray, self::INPUT_DATE_TIME)
) {
return;
}
// Set the TCA type to "datetime"
$this->changeTcaType($configArray, 'datetime');
// Remove "renderType" and "max"
// Note: Also remove "format". This option had been documented but was actually
// never used in the FormEngine element. This migration will set it according
// to the corresponding "eval" value.
$this->removeArrayItemFromArrayByKey($configArray, 'renderType');
$this->removeArrayItemFromArrayByKey($configArray, 'max');
$this->removeArrayItemFromArrayByKey($configArray, 'format');
$evalList = [];
$evalArrayItem = $this->extractArrayItemByKey($configArray, 'eval');
if ($evalArrayItem instanceof ArrayItem) {
$evalString = $this->valueResolver->getValue($evalArrayItem->value);
if (! is_string($evalString)) {
return;
}
$evalList = ArrayUtility::trimExplode(',', $evalString, true);
// Remove 'eval' config
$this->removeArrayItemFromArrayByKey($configArray, 'eval');
}
// Set the "format" based on "eval"
// If no 'format' config is set it will fall back to 'datetime'
if ($evalList !== []) {
if (in_array('date', $evalList, true)) {
$configArray->items[] = new ArrayItem(new String_('date'), new String_('format'));
} elseif (in_array('time', $evalList, true)) {
$configArray->items[] = new ArrayItem(new String_('time'), new String_('format'));
} elseif (in_array('timesec', $evalList, true)) {
$configArray->items[] = new ArrayItem(new String_('timesec'), new String_('format'));
}
}
// Set "eval" to "null" or "required", since they're currently defined and the only allowed "eval" for type=datetime
// Those will be migrated by according rector rules, but have to be kept by this one
if (in_array('null', $evalList, true) && in_array('required', $evalList, true)) {
$configArray->items[] = new ArrayItem(new String_('null,required'), new String_('eval'));
} elseif (in_array('required', $evalList, true)) {
$configArray->items[] = new ArrayItem(new String_('required'), new String_('eval'));
} elseif (in_array('null', $evalList, true)) {
$configArray->items[] = new ArrayItem(new String_('null'), new String_('eval'));
} else {
$this->removeArrayItemFromArrayByKey($configArray, 'eval');
}
// Removes option [config][default], if the default is the native "empty" value
$defaultArrayItem = $this->extractArrayItemByKey($configArray, 'default');
if ($defaultArrayItem instanceof ArrayItem) {
$dbTypeValue = null;
$dbTypeArrayItem = $this->extractArrayItemByKey($configArray, 'dbType');
if ($dbTypeArrayItem instanceof ArrayItem) {
$dbTypeValue = $this->valueResolver->getValue($dbTypeArrayItem->value);
}
$defaultValue = $this->valueResolver->getValue($defaultArrayItem->value);
if (in_array($dbTypeValue, self::DATETIME_TYPES, true)) {
if ($defaultValue === self::DATETIME_EMPTY_VALUES[$dbTypeValue]['empty']) {
// Remove default for native datetime fields if the default is the native "empty" value
$this->removeArrayItemFromArrayByKey($configArray, 'default');
}
$this->hasAstBeenChanged = true;
return;
}
if (is_int($defaultValue)) {
$this->hasAstBeenChanged = true;
return;
}
if ($defaultValue === '') {
// Always use int as default (string values are no longer supported for "datetime")
$defaultArrayItem->value = new LNumber(0);
$this->hasAstBeenChanged = true;
return;
}
if ($this->canBeInterpretedAsInteger($defaultValue)) {
// Cast default to int, in case it can be interpreted as integer
$defaultArrayItem->value = new LNumber((int) $defaultValue);
$this->hasAstBeenChanged = true;
return;
}
if ($defaultArrayItem->value instanceof FuncCall
&& $this->getName($defaultArrayItem->value) === 'time'
) {
$this->hasAstBeenChanged = true;
return;
}
// Unset default in case it's a no longer supported string
$this->removeArrayItemFromArrayByKey($configArray, 'default');
}
$this->hasAstBeenChanged = true;
}
/**
* @param mixed $var
*/
private function canBeInterpretedAsInteger($var): bool
{
if ($var === '' || is_object($var) || is_array($var)) {
return false;
}
return (string) (int) $var === (string) $var;
}
}