class DurationService in Duration Field 8.2
Same name and namespace in other branches
- 8 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
1 file declares its use of DurationService
- DurationServiceTest.php in tests/
src/ Unit/ Service/ DurationServiceTest.php
1 string reference to 'DurationService'
File
- src/
Service/ DurationService.php, line 13
Namespace
Drupal\duration_field\ServiceView source
class DurationService implements DurationServiceInterface {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function checkDurationInvalid($duration) {
if (!empty($duration) && !preg_match(Iso8601StringInterface::DURATION_STRING_PATTERN, $duration)) {
throw new InvalidDurationException('The submitted duration is not a valid duration string.');
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function convertDateArrayToDurationString(array $input) {
$duration = '';
$date_mappings = [
'y' => 'Y',
'm' => 'M',
'd' => 'D',
];
foreach (array_keys($date_mappings) as $key) {
if (isset($input[$key]) && $input[$key]) {
$duration .= $input[$key] . $date_mappings[$key];
}
}
$time_mappings = [
'h' => 'H',
'i' => 'M',
's' => 'S',
];
$found = FALSE;
foreach (array_keys($time_mappings) as $key) {
if (isset($input[$key]) && $input[$key]) {
if (!$found) {
$found = TRUE;
$duration .= 'T';
}
$duration .= $input[$key] . $time_mappings[$key];
}
}
return strlen($duration) ? 'P' . $duration : Iso8601StringInterface::EMPTY_DURATION;
}
/**
* {@inheritdoc}
*/
public function getDateIntervalFromDurationString($durationString) {
// Note: this will throw
// \Drupal\duration_field\Exception\InvalidDurationException if $value is
// an invalid ISO 8601 Duration string.
$this
->checkDurationInvalid($durationString);
if (!empty($durationString)) {
return new DateInterval($durationString);
}
else {
return $this
->createEmptyDateInterval();
}
}
/**
* {@inheritdoc}
*/
public function getDurationStringFromDateInterval(DateInterval $dateInterval) {
return $this
->convertDateArrayToDurationString([
'y' => $dateInterval
->format('%y'),
'm' => $dateInterval
->format('%m'),
'd' => $dateInterval
->format('%d'),
'h' => $dateInterval
->format('%h'),
'i' => $dateInterval
->format('%i'),
's' => $dateInterval
->format('%s'),
]);
}
/**
* {@inheritdoc}
*/
public function convertDateArrayToDateInterval(array $input) {
$duration_string = $this
->convertDateArrayToDurationString($input);
return $this
->getDateIntervalFromDurationString($duration_string);
}
/**
* {@inheritdoc}
*/
public function createEmptyDateInterval() {
return new DateInterval(Iso8601StringInterface::EMPTY_DURATION);
}
/**
* {@inheritdoc}
*/
public function getHumanReadableStringFromDateInterval(DateInterval $dateInterval, array $granularity, $separator = ' ', $textLength = 'full') {
$output = [];
if ($granularity['y'] && ($years = $dateInterval
->format('%y'))) {
$output[] = $this
->getTimePeriod('year', $years, $textLength);
}
if ($granularity['m'] && ($months = $dateInterval
->format('%m'))) {
$output[] = $this
->getTimePeriod('month', $months, $textLength);
}
if ($granularity['d'] && ($days = $dateInterval
->format('%d'))) {
$output[] = $this
->getTimePeriod('day', $days, $textLength);
}
if ($granularity['h'] && ($hours = $dateInterval
->format('%h'))) {
$output[] = $this
->getTimePeriod('hour', $hours, $textLength);
}
if ($granularity['i'] && ($minutes = $dateInterval
->format('%i'))) {
$output[] = $this
->getTimePeriod('minute', $minutes, $textLength);
}
if ($granularity['s'] && ($seconds = $dateInterval
->format('%s'))) {
$output[] = $this
->getTimePeriod('second', $seconds, $textLength);
}
return count($output) ? implode($separator, $output) : $this
->t('Empty');
}
/**
* {@inheritdoc}
*/
public function getSecondsFromDateInterval(DateInterval $dateInterval) {
return date_create('@0')
->add($dateInterval)
->getTimestamp();
}
/**
* {@inheritdoc}
*/
public function getSecondsFromDurationString($durationString) {
$date_interval = $this
->getDateIntervalFromDurationString($durationString);
return date_create('@0')
->add($date_interval)
->getTimestamp();
}
/**
* Returns a human-friendly value for a given time period key.
*
* @param string $type
* The type of the human-readable value to retrieve.
* @param int $value
* The amount for that time period.
* @param string $textLength
* The length of text to use. Allowed values are 'full' and 'short'.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The translated text value of the given type.
*/
protected function getTimePeriod($type, $value, $textLength) {
if ($type == 'year') {
if ($textLength == 'full') {
return $this
->formatPlural($value, '1 year', '@count years');
}
elseif ($textLength == 'short') {
return $this
->formatPlural($value, '1 yr', '@count yr');
}
}
elseif ($type == 'month') {
if ($textLength == 'full') {
return $this
->formatPlural($value, '1 month', '@count months');
}
elseif ($textLength == 'short') {
return $this
->formatPlural($value, '1 mo', '@count mo');
}
}
elseif ($type == 'day') {
return $this
->formatPlural($value, '1 day', '@count days');
}
elseif ($type == 'hour') {
if ($textLength == 'full') {
return $this
->formatPlural($value, '1 hour', '@count hours');
}
elseif ($textLength == 'short') {
return $this
->formatPlural($value, '1 hr', '@count hr');
}
}
elseif ($type == 'minute') {
if ($textLength == 'full') {
return $this
->formatPlural($value, '1 minute', '@count minutes');
}
elseif ($textLength == 'short') {
return $this
->formatPlural($value, '1 min', '@count min');
}
}
elseif ($type == 'second') {
if ($textLength == 'full') {
return $this
->formatPlural($value, '1 second', '@count seconds');
}
elseif ($textLength == 'short') {
return $this
->formatPlural($value, '1 s', '@count s');
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DurationService:: |
public | function |
Checks if a given string is a valid ISO 8601 duration string. Overrides DurationServiceInterface:: |
|
DurationService:: |
public | function |
Convert a PHP DateInterval object to an ISO 8601 duration string. Overrides DurationServiceInterface:: |
|
DurationService:: |
public | function |
Convert array into an ISO 8601 duration string. Overrides DurationServiceInterface:: |
|
DurationService:: |
public | function | ||
DurationService:: |
public | function |
Convert an ISO 8601 string into a PHP DateInterval object. Overrides DurationServiceInterface:: |
|
DurationService:: |
public | function |
Converts a PHP DateINterval object to an ISO 8601 duration string. Overrides DurationServiceInterface:: |
|
DurationService:: |
public | function |
Get a human-readable string representing a DateTime interval. Overrides DurationServiceInterface:: |
|
DurationService:: |
public | function |
Get the number of seconds a given duration represents. Overrides DurationServiceInterface:: |
|
DurationService:: |
public | function |
Get the number of seconds an ISO 8601 duration string represents. Overrides DurationServiceInterface:: |
|
DurationService:: |
protected | function | Returns a human-friendly value for a given time period key. | |
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. |