forked from Setono/CronExpressionBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCronExpressionToPartsTransformer.php
More file actions
121 lines (102 loc) · 3.72 KB
/
CronExpressionToPartsTransformer.php
File metadata and controls
121 lines (102 loc) · 3.72 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
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace Setono\CronExpressionBundle\Form\DataTransformer;
use Cron\CronExpression;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
/**
* @template-implements DataTransformerInterface<CronExpression, array<string, array<string>>>
*/
final class CronExpressionToPartsTransformer implements DataTransformerInterface
{
/**
* @param mixed $value
*
* @return array<string, array<string>>
*/
#[\Override]
public function transform($value): array
{
if (null === $value) {
return [
'minutes' => ['*'],
'hours' => ['*'],
'days' => ['*'],
'months' => ['*'],
'weekdays' => ['*'],
];
}
if (!$value instanceof CronExpression) {
throw new TransformationFailedException('Expected an instance of ' . CronExpression::class);
}
return [
'minutes' => $this->convertCronString((string) $value->getExpression((string) CronExpression::MINUTE)),
'hours' => $this->convertCronString((string) $value->getExpression((string) CronExpression::HOUR)),
'days' => $this->convertCronString((string) $value->getExpression((string) CronExpression::DAY)),
'months' => $this->convertCronString((string) $value->getExpression((string) CronExpression::MONTH)),
'weekdays' => $this->convertCronString((string) $value->getExpression((string) CronExpression::WEEKDAY)),
];
}
/**
* @param mixed $value
*/
#[\Override]
public function reverseTransform($value): CronExpression
{
$cronExpression = CronExpression::factory('* * * * *');
if (null === $value) {
return $cronExpression;
}
$exception = new TransformationFailedException('Expected an instance of array{minutes: array, hours: array, days: array, months: array, weekdays: array}');
if (!is_array($value)) {
throw $exception;
}
if (!isset($value['minutes'], $value['hours'], $value['days'], $value['months'], $value['weekdays'])) {
throw $exception;
}
if (!self::allArrayScalar($value)) {
throw $exception;
}
try {
$cronExpression
->setPart(CronExpression::MINUTE, $this->convertCronParts($value['minutes']))
->setPart(CronExpression::HOUR, $this->convertCronParts($value['hours']))
->setPart(CronExpression::DAY, $this->convertCronParts($value['days']))
->setPart(CronExpression::MONTH, $this->convertCronParts($value['months']))
->setPart(CronExpression::WEEKDAY, $this->convertCronParts($value['weekdays']))
;
return $cronExpression;
} catch (\InvalidArgumentException $e) {
throw $exception;
}
}
/**
* @phpstan-assert array<string, array<scalar>> $value
*
* @phpstan-ignore missingType.iterableValue
*/
private static function allArrayScalar(array $value): bool
{
return array_all($value, fn (mixed $s) => is_array($s) && array_all($s, fn (mixed $o) => is_scalar($o)));
}
/**
* @param array<scalar> $cronArray
*/
private function convertCronParts(array $cronArray): string
{
if ([] === $cronArray) {
return '*';
}
return implode(',', $cronArray);
}
/**
* @return array<string>
*/
private function convertCronString(string $cronString): array
{
if ('*' === $cronString) {
return [];
}
return explode(',', $cronString);
}
}