1: <?php
2:
3: namespace Cron;
4:
5: 6: 7:
8: abstract class AbstractField implements FieldInterface
9: {
10: 11: 12: 13: 14: 15: 16: 17:
18: public function isSatisfied($dateValue, $value)
19: {
20: if ($this->isIncrementsOfRanges($value)) {
21: return $this->isInIncrementsOfRanges($dateValue, $value);
22: } elseif ($this->isRange($value)) {
23: return $this->isInRange($dateValue, $value);
24: }
25:
26: return $value == '*' || $dateValue == $value;
27: }
28:
29: 30: 31: 32: 33: 34: 35:
36: public function isRange($value)
37: {
38: return strpos($value, '-') !== false;
39: }
40:
41: 42: 43: 44: 45: 46: 47:
48: public function isIncrementsOfRanges($value)
49: {
50: return strpos($value, '/') !== false;
51: }
52:
53: 54: 55: 56: 57: 58: 59: 60:
61: public function isInRange($dateValue, $value)
62: {
63: $parts = array_map('trim', explode('-', $value, 2));
64:
65: return $dateValue >= $parts[0] && $dateValue <= $parts[1];
66: }
67:
68: 69: 70: 71: 72: 73: 74: 75:
76: public function isInIncrementsOfRanges($dateValue, $value)
77: {
78: $parts = array_map('trim', explode('/', $value, 2));
79: $stepSize = isset($parts[1]) ? $parts[1] : 0;
80: if ($parts[0] == '*' || $parts[0] === '0') {
81: return (int) $dateValue % $stepSize == 0;
82: }
83:
84: $range = explode('-', $parts[0], 2);
85: $offset = $range[0];
86: $to = isset($range[1]) ? $range[1] : $dateValue;
87:
88: if ($dateValue < $offset || $dateValue > $to) {
89: return false;
90: }
91:
92: for ($i = $offset; $i <= $to; $i+= $stepSize) {
93: if ($i == $dateValue) {
94: return true;
95: }
96: }
97:
98: return false;
99: }
100: }
101: