1 | <?php |
||||
2 | |||||
3 | namespace AkkiIo\CronExpressionGenerator; |
||||
4 | |||||
5 | use Illuminate\Support\Arr; |
||||
6 | use Illuminate\Validation\Rule; |
||||
7 | use InvalidArgumentException; |
||||
8 | |||||
9 | class ParseInput |
||||
10 | { |
||||
11 | const SUPPORTED_TYPES = [ |
||||
12 | 'ONCE', |
||||
13 | 'EVERY', |
||||
14 | 'LIST', |
||||
15 | 'RANGE', |
||||
16 | 'STEP', |
||||
17 | ]; |
||||
18 | |||||
19 | public array $options; |
||||
20 | |||||
21 | public int $max; |
||||
22 | |||||
23 | /** |
||||
24 | * ParseInput constructor. |
||||
25 | * |
||||
26 | * @param array $options |
||||
27 | * @param int $min |
||||
28 | * @param int $max |
||||
29 | */ |
||||
30 | public function __construct(array $options, int $min, int $max) |
||||
31 | { |
||||
32 | $this->options = $options; |
||||
33 | |||||
34 | if (count($this->options)) { |
||||
35 | $validator = (new ValidatorFactory())->make($this->options, $this->rules($min, $max)); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
36 | |||||
37 | if ($validator->fails()) { |
||||
38 | throw new InvalidArgumentException('UNSUPPORTED_OPTIONS'); |
||||
39 | } |
||||
40 | } |
||||
41 | } |
||||
42 | |||||
43 | /** |
||||
44 | * Get the validation rules. |
||||
45 | * |
||||
46 | * @param int $min |
||||
47 | * @param int $max |
||||
48 | * @return array[] |
||||
49 | */ |
||||
50 | public function rules(int $min, int $max): array |
||||
51 | { |
||||
52 | return [ |
||||
53 | 'type' => [ |
||||
54 | 'required', |
||||
55 | Rule::in(self::SUPPORTED_TYPES), |
||||
56 | ], |
||||
57 | 'at' => [ |
||||
58 | 'required_if:type,ONCE', |
||||
59 | 'between:'.($min).','.($max), |
||||
60 | ], |
||||
61 | 'every' => [ |
||||
62 | 'required_if:type,EVERY', |
||||
63 | 'required_if:type,STEP', |
||||
64 | 'between:'.($min + 1).','.($max), |
||||
65 | ], |
||||
66 | 'list' => [ |
||||
67 | 'required_if:type,LIST', |
||||
68 | 'array', |
||||
69 | ], |
||||
70 | 'list.*' => [ |
||||
71 | 'required_if:type,LIST', |
||||
72 | 'between:'.($min).','.($max - 1), |
||||
73 | ], |
||||
74 | 'start' => [ |
||||
75 | 'required_if:type,RANGE', |
||||
76 | 'required_if:type,STEP', |
||||
77 | 'between:'.($min).','.($max - 1), |
||||
78 | ], |
||||
79 | 'end' => [ |
||||
80 | 'required_if:type,RANGE', |
||||
81 | 'required_if:type,STEP', |
||||
82 | 'between:'.($min).','.($max - 1), |
||||
83 | 'gte:start', |
||||
84 | ], |
||||
85 | ]; |
||||
86 | } |
||||
87 | |||||
88 | /** |
||||
89 | * Generate the cron. |
||||
90 | * |
||||
91 | * @return string |
||||
92 | */ |
||||
93 | public function generate(): string |
||||
94 | { |
||||
95 | $type = Arr::get($this->options, 'type'); |
||||
96 | |||||
97 | if ($type === 'ONCE') { |
||||
98 | return Arr::get($this->options, 'at'); |
||||
99 | } |
||||
100 | |||||
101 | if ($type === 'EVERY') { |
||||
102 | return '*/'.Arr::get($this->options, 'every'); |
||||
103 | } |
||||
104 | |||||
105 | if ($type === 'LIST') { |
||||
106 | return implode(',', Arr::get($this->options, 'list')); |
||||
0 ignored issues
–
show
It seems like
Illuminate\Support\Arr::...$this->options, 'list') can also be of type null ; however, parameter $pieces of implode() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
107 | } |
||||
108 | |||||
109 | if ($type === 'RANGE') { |
||||
110 | return Arr::get($this->options, 'start') |
||||
111 | .'-' |
||||
112 | .Arr::get($this->options, 'end'); |
||||
113 | } |
||||
114 | |||||
115 | if ($type === 'STEP') { |
||||
116 | return Arr::get($this->options, 'start') |
||||
117 | .'-' |
||||
118 | .Arr::get($this->options, 'end') |
||||
119 | .'/' |
||||
120 | .Arr::get($this->options, 'every'); |
||||
121 | } |
||||
122 | |||||
123 | return '*'; |
||||
124 | } |
||||
125 | } |
||||
126 |