summaryrefslogtreecommitdiffstats
path: root/vendor/dragonmantank/cron-expression/src/Cron/MinutesField.php
blob: f077e6ec51d2e8604fda280a99f2b14e36528db2 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
<?php

declare(strict_types=1);

namespace Cron;

use DateTimeInterface;

/**
 * Minutes field.  Allows: * , / -.
 */
class MinutesField extends AbstractField
{
    /**
     * {@inheritdoc}
     */
    protected $rangeStart = 0;

    /**
     * {@inheritdoc}
     */
    protected $rangeEnd = 59;

    /**
     * {@inheritdoc}
     */
    public function isSatisfiedBy(DateTimeInterface $date, $value, bool $invert):bool
    {
        if ($value === '?') {
            return true;
        }

        return $this->isSatisfied((int)$date->format('i'), $value);
    }

    /**
     * {@inheritdoc}
     * {@inheritDoc}
     *
     * @param string|null                  $parts
     */
    public function increment(DateTimeInterface &$date, $invert = false, $parts = null): FieldInterface
    {
        if (is_null($parts)) {
            $date = $this->timezoneSafeModify($date, ($invert ? "-" : "+") ."1 minute");
            return $this;
        }

        $current_minute = (int) $date->format('i');

        $parts = false !== strpos($parts, ',') ? explode(',', $parts) : [$parts];
        sort($parts);
        $minutes = [];
        foreach ($parts as $part) {
            $minutes = array_merge($minutes, $this->getRangeForExpression($part, 59));
        }

        $position = $invert ? \count($minutes) - 1 : 0;
        if (\count($minutes) > 1) {
            for ($i = 0; $i < \count($minutes) - 1; ++$i) {
                if ((!$invert && $current_minute >= $minutes[$i] && $current_minute < $minutes[$i + 1]) ||
                    ($invert && $current_minute > $minutes[$i] && $current_minute <= $minutes[$i + 1])) {
                    $position = $invert ? $i : $i + 1;

                    break;
                }
            }
        }

        $target = (int) $minutes[$position];
        $originalMinute = (int) $date->format("i");

        if (! $invert) {
            if ($originalMinute >= $target) {
                $distance = 60 - $originalMinute;
                $date = $this->timezoneSafeModify($date, "+{$distance} minutes");

                $originalMinute = (int) $date->format("i");
            }

            $distance = $target - $originalMinute;
            $date = $this->timezoneSafeModify($date, "+{$distance} minutes");
        } else {
            if ($originalMinute <= $target) {
                $distance = ($originalMinute + 1);
                $date = $this->timezoneSafeModify($date, "-{$distance} minutes");

                $originalMinute = (int) $date->format("i");
            }

            $distance = $originalMinute - $target;
            $date = $this->timezoneSafeModify($date, "-{$distance} minutes");
        }

        return $this;
    }
}