You are here

class DurationService in Duration Field 8

Same name and namespace in other branches
  1. 8.2 src/Service/DurationService.php \Drupal\duration_field\Service\DurationService
  2. 3.0.x src/Service/DurationService.php \Drupal\duration_field\Service\DurationService

Provides services for the Duration Field module.

Hierarchy

Expanded class hierarchy of DurationService

4 files declare their use of DurationService
Duration.php in src/Element/Duration.php
DurationData.php in src/Plugin/DataType/DurationData.php
DurationField.php in src/Plugin/Field/FieldType/DurationField.php
DurationServiceTest.php in tests/src/Unit/Service/DurationServiceTest.php
1 string reference to 'DurationService'
duration_field.services.yml in ./duration_field.services.yml
duration_field.services.yml

File

src/Service/DurationService.php, line 11

Namespace

Drupal\duration_field\Service
View source
class DurationService implements DurationServiceInterface {
  use StringTranslationTrait;
  const DURATION_FIELD_PATTERN = '/^P(\\d+Y)?(\\d+M)?(\\d+D)?(T)?(\\d+H)?(\\d+M)?(\\d+S)?$/';

  /**
   * {@inheritdoc}
   */
  public static function checkDurationInvalid($duration) {
    if (!empty($duration) && !preg_match(self::DURATION_FIELD_PATTERN, $duration)) {
      throw new InvalidDurationException($duration . ' is not a valid duration');
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public static function convertValue(array $input) {
    $duration = '';
    $date_mappings = [
      'year' => 'Y',
      'month' => 'M',
      'day' => 'D',
    ];
    foreach ($date_mappings as $key => $duration_key) {
      if (isset($input[$key]) && $input[$key]) {
        $duration .= $input[$key] . $duration_key;
      }
    }
    $time_mappings = [
      'hour' => 'H',
      'minute' => 'M',
      'second' => 'S',
    ];
    $found = FALSE;
    foreach ($time_mappings as $key => $duration_key) {
      if (isset($input[$key]) && $input[$key]) {
        if (!$found) {
          $found = TRUE;
          $duration .= 'T';
        }
        $duration .= $input[$key] . $duration_key;
      }
    }
    return strlen($duration) ? 'P' . $duration : '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DurationService::checkDurationInvalid public static function Checks if a given duration is a valid Iso8601 duration format. Overrides DurationServiceInterface::checkDurationInvalid
DurationService::convertValue public static function Convert array into PHP duration format. Overrides DurationServiceInterface::convertValue
DurationService::DURATION_FIELD_PATTERN constant
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.