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
Definition of Drupal\Core\TypedData\Type\Duration.

Namespace

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

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

  /**
   * Implements TypedDataInterface::setValue().
   */
  public function setValue($value) {
    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 {
      throw new InvalidArgumentException("Invalid duration format given.");
    }
  }

  /**
   * Implements TypedDataInterface::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');
  }

  /**
   * Implements TypedDataInterface::validate().
   */
  public function validate() {

    // TODO: Implement validate() method.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Duration::$value protected property The data value.
Duration::getString public function Implements TypedDataInterface::getString(). Overrides TypedData::getString
Duration::setValue public function Implements TypedDataInterface::setValue(). Overrides TypedData::setValue
Duration::validate public function Implements TypedDataInterface::validate(). Overrides TypedDataInterface::validate
TypedData::$definition protected property The data definition.
TypedData::getDefinition public function Implements TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getType public function Implements TypedDataInterface::getType(). Overrides TypedDataInterface::getType
TypedData::getValue public function Implements TypedDataInterface::getValue(). Overrides TypedDataInterface::getValue 7
TypedData::__construct public function Constructs a TypedData object given its definition. 3