DateRecurNonRecurringHelper.php in Recurring Dates Field 3.0.x
File
src/DateRecurNonRecurringHelper.php
View source
<?php
declare (strict_types=1);
namespace Drupal\date_recur;
class DateRecurNonRecurringHelper implements DateRecurHelperInterface {
protected $occurrences = [];
public function __construct(\DateTimeInterface $dtStart, \DateTimeInterface $dtStartEnd = NULL) {
$dtStartEnd = $dtStartEnd ?? clone $dtStart;
$this->occurrences = [
new DateRange($dtStart, $dtStartEnd),
];
}
public static function createInstance(string $string, \DateTimeInterface $dtStart, ?\DateTimeInterface $dtStartEnd = NULL) : DateRecurHelperInterface {
return new static($dtStart, $dtStartEnd);
}
public function getRules() : array {
return [];
}
public function isInfinite() : bool {
return FALSE;
}
public function generateOccurrences(?\DateTimeInterface $rangeStart = NULL, ?\DateTimeInterface $rangeEnd = NULL) : \Generator {
foreach ($this->occurrences as $occurrence) {
$occurrenceStart = $occurrence
->getStart();
$occurrenceEnd = $occurrence
->getEnd();
if ($rangeStart) {
if ($occurrenceStart < $rangeStart && $occurrenceEnd < $rangeStart) {
continue;
}
}
if ($rangeEnd) {
if ($occurrenceStart > $rangeEnd && $occurrenceEnd > $rangeEnd) {
break;
}
}
(yield $occurrence);
}
}
public function getOccurrences(\DateTimeInterface $rangeStart = NULL, ?\DateTimeInterface $rangeEnd = NULL, ?int $limit = NULL) : array {
if (isset($limit) && (!is_int($limit) || $limit < 0)) {
throw new \InvalidArgumentException('Invalid count limit.');
}
if (isset($limit) && $limit === 0) {
return [];
}
return iterator_to_array($this
->generateOccurrences($rangeStart, $rangeEnd));
}
public function getExcluded() : array {
return [];
}
public function current() : DateRange {
return current($this->occurrences);
}
public function next() : void {
next($this->occurrences);
}
public function key() : ?int {
$key = key($this->occurrences);
assert(is_int($key));
return $key;
}
public function valid() : bool {
return TRUE;
}
public function rewind() : void {
reset($this->occurrences);
}
}