Path: blob/master/src/applications/calendar/parser/data/PhutilCalendarDuration.php
12262 views
<?php12final class PhutilCalendarDuration extends Phobject {34private $isNegative = false;5private $weeks = 0;6private $days = 0;7private $hours = 0;8private $minutes = 0;9private $seconds = 0;1011public static function newFromDictionary(array $dict) {12static $keys;13if ($keys === null) {14$keys = array_fuse(15array(16'isNegative',17'weeks',18'days',19'hours',20'minutes',21'seconds',22));23}2425foreach ($dict as $key => $value) {26if (!isset($keys[$key])) {27throw new Exception(28pht(29'Unexpected key "%s" in duration dictionary, expected keys: %s.',30$key,31implode(', ', array_keys($keys))));32}33}3435$duration = id(new self())36->setIsNegative(idx($dict, 'isNegative', false))37->setWeeks(idx($dict, 'weeks', 0))38->setDays(idx($dict, 'days', 0))39->setHours(idx($dict, 'hours', 0))40->setMinutes(idx($dict, 'minutes', 0))41->setSeconds(idx($dict, 'seconds', 0));4243return $duration;44}4546public function toDictionary() {47return array(48'isNegative' => $this->getIsNegative(),49'weeks' => $this->getWeeks(),50'days' => $this->getDays(),51'hours' => $this->getHours(),52'minutes' => $this->getMinutes(),53'seconds' => $this->getSeconds(),54);55}5657public static function newFromISO8601($value) {58$pattern =59'/^'.60'(?P<sign>[+-])?'.61'P'.62'(?:'.63'(?P<W>\d+)W'.64'|'.65'(?:(?:(?P<D>\d+)D)?'.66'(?:T(?:(?P<H>\d+)H)?(?:(?P<M>\d+)M)?(?:(?P<S>\d+)S)?)?'.67')'.68')'.69'\z/';7071$matches = null;72$ok = preg_match($pattern, $value, $matches);73if (!$ok) {74throw new Exception(75pht(76'Expected ISO8601 duration in the format "P12DT3H4M5S", found '.77'"%s".',78$value));79}8081$is_negative = (idx($matches, 'sign') == '-');8283return id(new self())84->setIsNegative($is_negative)85->setWeeks((int)idx($matches, 'W', 0))86->setDays((int)idx($matches, 'D', 0))87->setHours((int)idx($matches, 'H', 0))88->setMinutes((int)idx($matches, 'M', 0))89->setSeconds((int)idx($matches, 'S', 0));90}9192public function toISO8601() {93$parts = array();94$parts[] = 'P';9596$weeks = $this->getWeeks();97if ($weeks) {98$parts[] = $weeks.'W';99} else {100$days = $this->getDays();101if ($days) {102$parts[] = $days.'D';103}104105$parts[] = 'T';106107$hours = $this->getHours();108if ($hours) {109$parts[] = $hours.'H';110}111112$minutes = $this->getMinutes();113if ($minutes) {114$parts[] = $minutes.'M';115}116117$seconds = $this->getSeconds();118if ($seconds) {119$parts[] = $seconds.'S';120}121}122123return implode('', $parts);124}125126public function setIsNegative($is_negative) {127$this->isNegative = $is_negative;128return $this;129}130131public function getIsNegative() {132return $this->isNegative;133}134135public function setWeeks($weeks) {136$this->weeks = $weeks;137return $this;138}139140public function getWeeks() {141return $this->weeks;142}143144public function setDays($days) {145$this->days = $days;146return $this;147}148149public function getDays() {150return $this->days;151}152153public function setHours($hours) {154$this->hours = $hours;155return $this;156}157158public function getHours() {159return $this->hours;160}161162public function setMinutes($minutes) {163$this->minutes = $minutes;164return $this;165}166167public function getMinutes() {168return $this->minutes;169}170171public function setSeconds($seconds) {172$this->seconds = $seconds;173return $this;174}175176public function getSeconds() {177return $this->seconds;178}179180}181182183