1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the PHP-CRON-EXPR package. |
7
|
|
|
* |
8
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
9
|
|
|
* <https://github.com/adhocore> |
10
|
|
|
* |
11
|
|
|
* Licensed under MIT license. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Ahc\Cron; |
15
|
|
|
|
16
|
|
|
class ReferenceTime |
17
|
|
|
{ |
18
|
|
|
// The cron parts. (Donot change it) |
19
|
|
|
const MINUTE = 0; |
20
|
|
|
const HOUR = 1; |
21
|
|
|
const MONTHDAY = 2; |
22
|
|
|
const MONTH = 3; |
23
|
|
|
const WEEKDAY = 4; |
24
|
|
|
const YEAR = 5; |
25
|
|
|
|
26
|
|
|
// Meta data parts. |
27
|
|
|
const DAY = 6; |
28
|
|
|
const WEEKDAY1 = 7; |
29
|
|
|
const NUMDAYS = 8; |
30
|
|
|
|
31
|
|
|
/** @var array The data */ |
32
|
|
|
protected $values = []; |
33
|
|
|
|
34
|
|
|
/** @var array The Magic methods */ |
35
|
|
|
protected $methods = []; |
36
|
|
|
|
37
|
|
|
public function __construct($time) |
38
|
|
|
{ |
39
|
|
|
$timestamp = $this->normalizeTime($time); |
40
|
|
|
|
41
|
|
|
$this->values = $this->parse($timestamp); |
42
|
|
|
$this->methods = (new \ReflectionClass($this))->getConstants(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function __call(string $method, array $args): int |
46
|
|
|
{ |
47
|
|
|
$method = \preg_replace('/^GET/', '', \strtoupper($method)); |
48
|
|
|
if (isset($this->methods[$method])) { |
49
|
|
|
return $this->values[$this->methods[$method]]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// @codeCoverageIgnoreStart |
53
|
|
|
throw new \BadMethodCallException("Method '$method' doesnot exist in ReferenceTime."); |
54
|
|
|
// @codeCoverageIgnoreEnd |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function get(int $segment): int |
58
|
|
|
{ |
59
|
|
|
return $this->values[$segment]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function isAt($value, $segment): bool |
63
|
|
|
{ |
64
|
|
|
return $this->values[$segment] == $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function normalizeTime($time): int |
68
|
|
|
{ |
69
|
|
|
if (empty($time)) { |
70
|
|
|
$time = \time(); |
71
|
|
|
} elseif (\is_string($time)) { |
72
|
|
|
$time = \strtotime($time); |
73
|
|
|
} elseif ($time instanceof \DateTime) { |
74
|
|
|
$time = $time->getTimestamp(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $time; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function parse(int $timestamp): array |
81
|
|
|
{ |
82
|
|
|
$parts = \date('i G j n w Y d N t', $timestamp); |
83
|
|
|
$parts = \explode(' ', $parts); |
84
|
|
|
$parts = \array_map('intval', $parts); |
85
|
|
|
|
86
|
|
|
return $parts; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|