1<?php
2
3namespace Cron;
4
5use DateTime;
6
7
8/**
9 * Minutes field.  Allows: * , / -
10 */
11class MinutesField extends AbstractField
12{
13    public function isSatisfiedBy(DateTime $date, $value)
14    {
15        return $this->isSatisfied($date->format('i'), $value);
16    }
17
18    public function increment(DateTime $date, $invert = false, $parts = null)
19    {
20        if (is_null($parts)) {
21            if ($invert) {
22                $date->modify('-1 minute');
23            } else {
24                $date->modify('+1 minute');
25            }
26            return $this;
27        }
28
29        $parts = strpos($parts, ',') !== false ? explode(',', $parts) : array($parts);
30        $minutes = array();
31        foreach ($parts as $part) {
32            $minutes = array_merge($minutes, $this->getRangeForExpression($part, 59));
33        }
34
35        $current_minute = $date->format('i');
36        $position = $invert ? count($minutes) - 1 : 0;
37        if (count($minutes) > 1) {
38            for ($i = 0; $i < count($minutes) - 1; $i++) {
39                if ((!$invert && $current_minute >= $minutes[$i] && $current_minute < $minutes[$i + 1]) ||
40                    ($invert && $current_minute > $minutes[$i] && $current_minute <= $minutes[$i + 1])) {
41                    $position = $invert ? $i : $i + 1;
42                    break;
43                }
44            }
45        }
46
47        if ((!$invert && $current_minute >= $minutes[$position]) || ($invert && $current_minute <= $minutes[$position])) {
48            $date->modify(($invert ? '-' : '+') . '1 hour');
49            $date->setTime($date->format('H'), $invert ? 59 : 0);
50        }
51        else {
52            $date->setTime($date->format('H'), $minutes[$position]);
53        }
54
55        return $this;
56    }
57
58    public function validate($value)
59    {
60        return (bool) preg_match('/^[\*,\/\-0-9]+$/', $value);
61    }
62}
63