GranularityService.php in Duration Field 8.2
File
src/Service/GranularityService.php
View source
<?php
namespace Drupal\duration_field\Service;
class GranularityService implements GranularityServiceInterface {
public function convertGranularityArrayToGranularityString(array $granularityArray) {
$granularities = [];
foreach (array_keys($granularityArray) as $granularity) {
if ($granularityArray[$granularity]) {
$granularities[] = $granularity;
}
}
return implode(':', $granularities);
}
public function convertGranularityStringToGranularityArray($granularityString) {
$granularities = $this
->getDrupalStatic(__CLASS__ . '::' . __FUNCTION__);
if (!isset($granularities[$granularityString])) {
$granularities[$granularityString] = [
'y' => FALSE,
'm' => FALSE,
'd' => FALSE,
'h' => FALSE,
'i' => FALSE,
's' => FALSE,
];
foreach (explode(':', $granularityString) as $key) {
if (strlen($key)) {
$granularities[$granularityString][$key] = TRUE;
}
}
}
return $granularities[$granularityString];
}
public function includeGranularityElement($granularityElement, $granularityString) {
$granularities = $this
->convertGranularityStringToGranularityArray($granularityString);
return $granularities[$granularityElement];
}
protected function getDrupalStatic($key) {
$static =& drupal_static($key);
return $static;
}
}