class DurationService in Duration Field 8
Same name and namespace in other branches
- 8.2 src/Service/DurationService.php \Drupal\duration_field\Service\DurationService
- 3.0.x src/Service/DurationService.php \Drupal\duration_field\Service\DurationService
Provides services for the Duration Field module.
Hierarchy
- class \Drupal\duration_field\Service\DurationService implements DurationServiceInterface uses StringTranslationTrait
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'
File
- src/
Service/ DurationService.php, line 11
Namespace
Drupal\duration_field\ServiceView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DurationService:: |
public static | function |
Checks if a given duration is a valid Iso8601 duration format. Overrides DurationServiceInterface:: |
|
DurationService:: |
public static | function |
Convert array into PHP duration format. Overrides DurationServiceInterface:: |
|
DurationService:: |
constant | |||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |