-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
MigrateRenderTypeColorpickerToTypeColorRector.php
117 lines (102 loc) · 3.56 KB
/
MigrateRenderTypeColorpickerToTypeColorRector.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
<?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\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-97271-NewTCATypeColor.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\tca\MigrateRenderTypeColorpickerToTypeColorRector\MigrateRenderTypeColorpickerToTypeColorRectorTest
*/
final class MigrateRenderTypeColorpickerToTypeColorRector extends AbstractTcaRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Migrate renderType colorpicker to type color', [new CodeSample(
<<<'CODE_SAMPLE'
return [
'columns' => [
'a_color_field' => [
'label' => 'Color field',
'config' => [
'type' => 'input',
'renderType' => 'colorpicker',
'required' => true,
'size' => 20,
'max' => 1024,
'eval' => 'trim',
'valuePicker' => [
'items' => [
['typo3 orange', '#FF8700'],
],
],
],
],
],
];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
return [
'columns' => [
'a_color_field' => [
'label' => 'Color field',
'config' => [
'type' => 'color',
'required' => true,
'size' => 20,
'valuePicker' => [
'items' => [
['typo3 orange', '#FF8700'],
],
],
],
],
],
];
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')) {
return;
}
if (! $this->configIsOfRenderType($configArray, 'colorpicker')) {
return;
}
// Set the TCA type to "color"
$this->changeTcaType($configArray, 'color');
// Remove 'max' config
$this->removeArrayItemFromArrayByKey($configArray, 'max');
// Remove 'renderType' config
$this->removeArrayItemFromArrayByKey($configArray, 'renderType');
$evalArrayItem = $this->extractArrayItemByKey($configArray, 'eval');
if (! $evalArrayItem instanceof ArrayItem) {
return;
}
$evalListValue = $this->valueResolver->getValue($evalArrayItem->value);
if (! is_string($evalListValue)) {
return;
}
$evalList = ArrayUtility::trimExplode(',', $evalListValue, true);
if (in_array('null', $evalList, true)) {
// Set "eval" to "null", since it's currently defined and the only allowed "eval" for type=color
$evalArrayItem->value = new String_('null');
} else {
// 'eval' is empty, remove whole configuration
$this->removeArrayItemFromArrayByKey($configArray, 'eval');
}
$this->hasAstBeenChanged = true;
}
}