You are here

public static function DurationService::convertValue in Duration Field 8

Convert array into PHP duration format.

Parameters

array $input: An array containing any of the following keys:

  • year
  • month
  • day
  • hour
  • minute
  • second.

Return value

string A string in Iso 8601 duration format

Overrides DurationServiceInterface::convertValue

See also

http://en.wikipedia.org/wiki/Iso8601#Durations

4 calls to DurationService::convertValue()
Duration::validateElement in src/Element/Duration.php
Sets the value of the submitted element.
Duration::valueCallback in src/Element/Duration.php
Determines how user input is mapped to an element's #value property.
DurationField::setValue in src/Plugin/Field/FieldType/DurationField.php
Sets the value of the field.
DurationServiceTest::testConvertValue in tests/src/Unit/Service/DurationServiceTest.php
@covers ::convertValue @dataProvider convertValueDataProvider

File

src/Service/DurationService.php, line 32

Class

DurationService
Provides services for the Duration Field module.

Namespace

Drupal\duration_field\Service

Code

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 : '';
}