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