You are here

public function GranularityService::convertGranularityStringToGranularityArray in Duration Field 8.2

Same name and namespace in other branches
  1. 3.0.x src/Service/GranularityService.php \Drupal\duration_field\Service\GranularityService::convertGranularityStringToGranularityArray()

Converts a granularity string to a granularity array.

Parameters

string $granularityString: The granularity string.

Return value

array An array containing the following keys. The value of each key will be either TRUE or FALSE.

  • y (years)
  • m (months)
  • d (days)
  • h (hours)
  • i (minutes)
  • s (seconds)

TRUE values mean the element should be included as part of the granularity.

Overrides GranularityServiceInterface::convertGranularityStringToGranularityArray

1 call to GranularityService::convertGranularityStringToGranularityArray()
GranularityService::includeGranularityElement in src/Service/GranularityService.php
Test if the given granularity element should be included.

File

src/Service/GranularityService.php, line 32

Class

GranularityService

Namespace

Drupal\duration_field\Service

Code

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];
}