summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/scheduler/src/OneOff.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:30:08 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:30:08 +0000
commit4ce65d59ca91871cfd126497158200a818720bce (patch)
treee277def01fc7eba7dbc21c4a4ae5576e8aa2cf1f /vendor/ipl/scheduler/src/OneOff.php
parentInitial commit. (diff)
downloadicinga-php-library-4ce65d59ca91871cfd126497158200a818720bce.tar.xz
icinga-php-library-4ce65d59ca91871cfd126497158200a818720bce.zip
Adding upstream version 0.13.1.upstream/0.13.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/scheduler/src/OneOff.php')
-rw-r--r--vendor/ipl/scheduler/src/OneOff.php69
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/ipl/scheduler/src/OneOff.php b/vendor/ipl/scheduler/src/OneOff.php
new file mode 100644
index 0000000..ebe945d
--- /dev/null
+++ b/vendor/ipl/scheduler/src/OneOff.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace ipl\Scheduler;
+
+use DateTime;
+use DateTimeInterface;
+use DateTimeZone;
+use InvalidArgumentException;
+use ipl\Scheduler\Contract\Frequency;
+
+use function ipl\Stdlib\get_php_type;
+
+class OneOff implements Frequency
+{
+ /** @var DateTimeInterface Start time of this frequency */
+ protected $dateTime;
+
+ public function __construct(DateTimeInterface $dateTime)
+ {
+ $this->dateTime = clone $dateTime;
+ $this->dateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
+ }
+
+ public function isDue(DateTimeInterface $dateTime): bool
+ {
+ return ! $this->isExpired($dateTime) && $this->dateTime == $dateTime;
+ }
+
+ public function getNextDue(DateTimeInterface $dateTime): DateTimeInterface
+ {
+ return $this->dateTime;
+ }
+
+ public function isExpired(DateTimeInterface $dateTime): bool
+ {
+ return $this->dateTime < $dateTime;
+ }
+
+ public function getStart(): ?DateTimeInterface
+ {
+ return $this->dateTime;
+ }
+
+ public function getEnd(): ?DateTimeInterface
+ {
+ return $this->getStart();
+ }
+
+ public static function fromJson(string $json): Frequency
+ {
+ $data = json_decode($json, true);
+ if (! is_string($data)) {
+ throw new InvalidArgumentException(
+ sprintf(
+ '%s expects json decoded value to be string, got %s instead',
+ __METHOD__,
+ get_php_type($data)
+ )
+ );
+ }
+
+ return new static(new DateTime($data));
+ }
+
+ public function jsonSerialize(): string
+ {
+ return $this->dateTime->format(static::SERIALIZED_DATETIME_FORMAT);
+ }
+}