-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCleanMultiValueValidationRulesStep.php
More file actions
112 lines (93 loc) · 3.36 KB
/
CleanMultiValueValidationRulesStep.php
File metadata and controls
112 lines (93 loc) · 3.36 KB
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
<?php
declare(strict_types=1);
namespace Relaticle\CustomFields\Console\Commands\Upgrade\Steps;
use Illuminate\Console\Command;
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStep;
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStepResult;
use Relaticle\CustomFields\CustomFields;
use Throwable;
/**
* Removes string-only validation rules from multi-value field types.
*
* Multi-value fields (link, email, phone) store arrays in json_value.
* Rules like starts_with and url expect strings and cause TypeErrors.
*/
final class CleanMultiValueValidationRulesStep implements UpgradeStep
{
/** @var list<string> */
private const MULTI_VALUE_FIELD_TYPES = ['link', 'email', 'phone'];
/** @var list<string> */
private const STRING_ONLY_RULES = [
'starts_with',
'ends_with',
'doesnt_start_with',
'doesnt_end_with',
'url',
'email',
'string',
'alpha',
'alpha_num',
'alpha_dash',
'regex',
'not_regex',
'active_url',
'ascii',
'ip',
'ipv4',
'ipv6',
'json',
'mac_address',
'uuid',
'uppercase',
];
public function name(): string
{
return 'Clean Multi-Value Validation Rules';
}
public function description(): string
{
return 'Remove string-only validation rules (starts_with, url, etc.) from multi-value fields';
}
public function execute(bool $dryRun, Command $command): UpgradeStepResult
{
$fieldModel = CustomFields::newCustomFieldModel();
$affectedFields = $fieldModel->newQuery()
->whereIn('type', self::MULTI_VALUE_FIELD_TYPES)
->whereNotNull('validation_rules')
->get();
$processed = 0;
$failed = 0;
foreach ($affectedFields as $field) {
$rules = $field->validation_rules?->toCollection() ?? collect();
$invalidRules = $rules->filter(
fn ($rule): bool => in_array($rule->name, self::STRING_ONLY_RULES, true)
);
if ($invalidRules->isEmpty()) {
continue;
}
$ruleNames = $invalidRules->pluck('name')->implode(', ');
$command->line(sprintf(" Processing field '%s' (type: %s, id: %s): removing [%s]", $field->name, $field->type, $field->id, $ruleNames));
if (! $dryRun) {
$cleanedRules = $rules->reject(
fn ($rule): bool => in_array($rule->name, self::STRING_ONLY_RULES, true)
)->values()->toArray();
try {
$field->update([
'validation_rules' => $cleanedRules === [] ? null : $cleanedRules,
]);
$processed++;
} catch (Throwable $e) {
$command->line(sprintf(' <error>Failed to update field %s: %s</error>', $field->id, $e->getMessage()));
$failed++;
}
} else {
$processed++;
}
}
if ($processed === 0 && $failed === 0) {
$command->line(' <info>No fields need cleanup</info>');
return UpgradeStepResult::skipped('No multi-value fields with string-only validation rules found');
}
return UpgradeStepResult::success($processed, $failed);
}
}