-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNormalizer.php
More file actions
86 lines (74 loc) · 2.17 KB
/
Copy pathNormalizer.php
File metadata and controls
86 lines (74 loc) · 2.17 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
<?php
declare(strict_types=1);
/*
* This file is part of the PHP-CRON-EXPR package.
*
* (c) Jitendra Adhikari <[email protected]>
* <https://github.com/adhocore>
*
* Licensed under MIT license.
*/
namespace Ahc\Cron;
class Normalizer
{
const YEARLY = '@yearly';
const ANNUALLY = '@annually';
const MONTHLY = '@monthly';
const WEEKLY = '@weekly';
const DAILY = '@daily';
const HOURLY = '@hourly';
const ALWAYS = '@always';
const FIVE_MIN = '@5minutes';
const TEN_MIN = '@10minutes';
const FIFTEEN_MIN = '@15minutes';
const THIRTY_MIN = '@30minutes';
protected static $expressions = [
self::YEARLY => '0 0 1 1 *',
self::ANNUALLY => '0 0 1 1 *',
self::MONTHLY => '0 0 1 * *',
self::WEEKLY => '0 0 * * 0',
self::DAILY => '0 0 * * *',
self::HOURLY => '0 * * * *',
self::ALWAYS => '* * * * *',
self::FIVE_MIN => '*/5 * * * *',
self::TEN_MIN => '*/10 * * * *',
self::FIFTEEN_MIN => '*/15 * * * *',
self::THIRTY_MIN => '0,30 * * * *',
];
protected static $literals = [
'sun' => 0,
'mon' => 1,
'tue' => 2,
'wed' => 3,
'thu' => 4,
'fri' => 5,
'sat' => 6,
'jan' => 1,
'feb' => 2,
'mar' => 3,
'apr' => 4,
'may' => 5,
'jun' => 6,
'jul' => 7,
'aug' => 8,
'sep' => 9,
'oct' => 10,
'nov' => 11,
'dec' => 12,
];
public function normalizeExpr(string $expr): string
{
$expr = \trim($expr);
if (isset(static::$expressions[$exp = \strtolower($expr)])) {
return static::$expressions[$exp];
}
$expr = \preg_replace('~\s+~', ' ', $expr);
$count = \substr_count($expr, ' ');
if ($count < 4 || $count > 5) {
throw new \UnexpectedValueException(
'Cron $expr should have 5 or 6 segments delimited by space'
);
}
return \str_ireplace(\array_keys(static::$literals), \array_values(static::$literals), $expr);
}
}