DateRecurPartGrid.php in Recurring Dates Field 3.x
File
src/DateRecurPartGrid.php
View source
<?php
declare (strict_types=1);
namespace Drupal\date_recur;
use Drupal\date_recur\Exception\DateRecurRulePartIncompatible;
use Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem;
class DateRecurPartGrid {
protected $allowedParts = [];
public function allowParts(string $frequency, array $parts) : void {
$existingFrequencyParts = $this->allowedParts[$frequency] ?? [];
$this->allowedParts[$frequency] = array_merge($parts, $existingFrequencyParts);
}
public function isAllowEverything() : bool {
return count($this->allowedParts) === 0;
}
public function isFrequencyAllowed(string $frequency) : bool {
assert(in_array($frequency, DateRecurRruleMap::FREQUENCIES, TRUE));
if ($this
->isAllowEverything()) {
return TRUE;
}
return isset($this->allowedParts[$frequency]) && count($this->allowedParts[$frequency]) > 0;
}
public function isPartAllowed(string $frequency, string $part) : bool {
assert(in_array($frequency, DateRecurRruleMap::FREQUENCIES, TRUE) && in_array($part, DateRecurRruleMap::PARTS, TRUE));
if (in_array($part, DateRecurRruleMap::INCOMPATIBLE_PARTS[$frequency], TRUE)) {
throw new DateRecurRulePartIncompatible();
}
if ($this
->isAllowEverything()) {
return TRUE;
}
$partsInFrequency = $this->allowedParts[$frequency] ?? [];
return in_array($part, $partsInFrequency, TRUE) || in_array(DateRecurItem::PART_SUPPORTS_ALL, $partsInFrequency, TRUE);
}
public static function configSettingsToGrid(array $parts) {
$grid = new static();
if (!empty($parts['all'])) {
return $grid;
}
$frequencies = $parts['frequencies'] ?? [];
foreach ($frequencies as $frequency => $frequencyParts) {
$grid
->allowParts($frequency, $frequencyParts);
}
return $grid;
}
}