Version 1
[yaffs-website] / web / core / lib / Drupal / Core / TypedData / Plugin / DataType / DurationIso8601.php
1 <?php
2
3 namespace Drupal\Core\TypedData\Plugin\DataType;
4
5 use Drupal\Core\TypedData\Type\DurationInterface;
6
7 /**
8  * The duration ISO8601 data type.
9  *
10  * The plain value of this data type is a ISO8601 duration string.
11  *
12  * @DataType(
13  *   id = "duration_iso8601",
14  *   label = @Translation("Duration")
15  * )
16  */
17 class DurationIso8601 extends StringData implements DurationInterface {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function getDuration() {
23     if ($this->value) {
24       // @todo: Add support for negative intervals on top of the DateInterval
25       // constructor.
26       return new \DateInterval($this->value);
27     }
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function setDuration(\DateInterval $duration, $notify = TRUE) {
34     // Generate an ISO 8601 formatted string as supported by
35     // DateInterval::__construct() and setValue().
36     $this->value = $duration->format('%rP%yY%mM%dDT%hH%mM%sS');
37     // Notify the parent of any changes.
38     if ($notify && isset($this->parent)) {
39       $this->parent->onChange($this->name);
40     }
41   }
42
43 }