class DateRecurNonRecurringHelper in Recurring Dates Field 3.0.x
Same name and namespace in other branches
- 8.2 src/DateRecurNonRecurringHelper.php \Drupal\date_recur\DateRecurNonRecurringHelper
- 3.x src/DateRecurNonRecurringHelper.php \Drupal\date_recur\DateRecurNonRecurringHelper
- 3.1.x src/DateRecurNonRecurringHelper.php \Drupal\date_recur\DateRecurNonRecurringHelper
Dummy helper for handling non-recurring values.
Hierarchy
- class \Drupal\date_recur\DateRecurNonRecurringHelper implements DateRecurHelperInterface
Expanded class hierarchy of DateRecurNonRecurringHelper
2 files declare their use of DateRecurNonRecurringHelper
- DateRecurItem.php in src/
Plugin/ Field/ FieldType/ DateRecurItem.php - DateRecurNonRecurringHelperUnitTest.php in tests/
src/ Unit/ DateRecurNonRecurringHelperUnitTest.php
File
- src/
DateRecurNonRecurringHelper.php, line 10
Namespace
Drupal\date_recurView source
class DateRecurNonRecurringHelper implements DateRecurHelperInterface {
/**
* The occurrences.
*
* @var \Drupal\date_recur\DateRange[]
*/
protected $occurrences = [];
/**
* Constructor for DateRecurNonRecurringHelper.
*
* @param \DateTimeInterface $dtStart
* The initial occurrence start date.
* @param \DateTimeInterface|null $dtStartEnd
* The initial occurrence end date, or NULL to use start date.
*/
public function __construct(\DateTimeInterface $dtStart, \DateTimeInterface $dtStartEnd = NULL) {
$dtStartEnd = $dtStartEnd ?? clone $dtStart;
$this->occurrences = [
new DateRange($dtStart, $dtStartEnd),
];
}
/**
* {@inheritdoc}
*/
public static function createInstance(string $string, \DateTimeInterface $dtStart, ?\DateTimeInterface $dtStartEnd = NULL) : DateRecurHelperInterface {
return new static($dtStart, $dtStartEnd);
}
/**
* {@inheritdoc}
*/
public function getRules() : array {
return [];
}
/**
* {@inheritdoc}
*/
public function isInfinite() : bool {
return FALSE;
}
/**
* {@inheritdoc}
*/
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);
}
}
/**
* {@inheritdoc}
*/
public function getOccurrences(\DateTimeInterface $rangeStart = NULL, ?\DateTimeInterface $rangeEnd = NULL, ?int $limit = NULL) : array {
if (isset($limit) && (!is_int($limit) || $limit < 0)) {
// Limit must be a number and more than one.
throw new \InvalidArgumentException('Invalid count limit.');
}
// There can either by zero or one occurrence generated for non-recurring
// generator.
if (isset($limit) && $limit === 0) {
return [];
}
return iterator_to_array($this
->generateOccurrences($rangeStart, $rangeEnd));
}
/**
* {@inheritdoc}
*/
public function getExcluded() : array {
return [];
}
/**
* {@inheritdoc}
*/
public function current() : DateRange {
return current($this->occurrences);
}
/**
* {@inheritdoc}
*/
public function next() : void {
next($this->occurrences);
}
/**
* {@inheritdoc}
*/
public function key() : ?int {
$key = key($this->occurrences);
assert(is_int($key));
return $key;
}
/**
* {@inheritdoc}
*/
public function valid() : bool {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function rewind() : void {
reset($this->occurrences);
}
}