class Duration

The duration data type.

The plain value of a duration is an instance of the DateInterval class. For setting the value an instance of the DateInterval class, a ISO8601 string as supported by DateInterval::__construct, or an integer in seconds may be passed.

Hierarchy

Expanded class hierarchy of Duration

1 string reference to 'Duration'
system_data_type_info in drupal/core/modules/system/system.module
Implements hook_data_type_info().

File

drupal/core/lib/Drupal/Core/TypedData/Type/Duration.php, line 22
Contains \Drupal\Core\TypedData\Type\Duration.

Namespace

Drupal\Core\TypedData\Type
View source
class Duration extends TypedData {

  /**
   * The data value.
   *
   * @var \DateInterval
   */
  protected $value;

  /**
   * Overrides TypedData::setValue().
   */
  public function setValue($value, $notify = TRUE) {

    // Notify the parent of any changes to be made.
    if ($notify && isset($this->parent)) {
      $this->parent
        ->onChange($this->name);
    }

    // Catch any exceptions thrown due to invalid values being passed.
    try {
      if ($value instanceof DateInterval || !isset($value)) {
        $this->value = $value;
      }
      elseif ((string) (int) $value === (string) $value) {
        $this->value = new DateInterval('PT' . $value . 'S');
      }
      elseif (is_string($value)) {

        // @todo: Add support for negative intervals on top of the DateInterval
        // constructor.
        $this->value = new DateInterval($value);
      }
      else {

        // Unknown value given.
        $this->value = $value;
      }
    } catch (\Exception $e) {

      // An invalid value has been given. Setting any invalid value will let
      // validation fail.
      $this->value = $e;
    }
  }

  /**
   * Overrides TypedData::getString().
   */
  public function getString() {

    // Generate an ISO 8601 formatted string as supported by
    // DateInterval::__construct() and setValue().
    return (string) $this
      ->getValue()
      ->format('%rP%yY%mM%dDT%hH%mM%sS');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Duration::$value protected property The data value.
Duration::getString public function Overrides TypedData::getString(). Overrides TypedData::getString
Duration::setValue public function Overrides TypedData::setValue(). Overrides TypedData::setValue
TypedData::$definition protected property The data definition.
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::getConstraints public function Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints(). Overrides TypedDataInterface::getConstraints 2
TypedData::getDefinition public function Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getName public function Implements \Drupal\Core\TypedData\TypedDataInterface::getName(). Overrides TypedDataInterface::getName
TypedData::getParent public function Implements \Drupal\Core\TypedData\TypedDataInterface::getParent(). Overrides TypedDataInterface::getParent
TypedData::getPropertyPath public function Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath(). Overrides TypedDataInterface::getPropertyPath
TypedData::getRoot public function Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot(). Overrides TypedDataInterface::getRoot
TypedData::getType public function Implements \Drupal\Core\TypedData\TypedDataInterface::getType(). Overrides TypedDataInterface::getType
TypedData::getValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::getValue(). Overrides TypedDataInterface::getValue 8
TypedData::setContext public function Implements \Drupal\Core\TypedData\TypedDataInterface::setContext(). Overrides TypedDataInterface::setContext 1
TypedData::validate public function Implements \Drupal\Core\TypedData\TypedDataInterface::validate(). Overrides TypedDataInterface::validate 3
TypedData::__construct public function Constructs a TypedData object given its definition and context. 5