Example #1
0
 /**
  * Parses a formal date string.
  * @param $formalDateString
  * @throws \Exception
  */
 public function parse($formalDateString)
 {
     $matches = array();
     $status = preg_match(self::$FORMAL_DATE_PATTERN, $formalDateString, $matches);
     if ($status === false) {
         throw new \Exception("Malformed simple date string {$formalDateString}");
     }
     // group 1: A or R[numRepetitions]
     if (isset($matches[1])) {
         if ($matches[1] === "A") {
             $this->isApproximate = true;
         } elseif (substr($matches[1], 0, 1) === "R") {
             $this->isRecurring = true;
             // Group 2: numRepetitions
             if (isset($matches[2]) && !empty($matches[2])) {
                 $this->numRepetitions = intval($matches[2]);
             }
         }
     }
     // Group 3: starting simpleDate
     if (isset($matches[3]) && !empty($matches[3])) {
         $this->start = new SimpleDate();
         $this->start->parse($matches[3]);
     }
     // Group 4: "/" and ending simpleDate or duration
     if (isset($matches[4])) {
         $this->isRange = true;
         if (isset($matches[5]) && !empty($matches[5])) {
             if (substr($matches[5], 0, 1) === "P") {
                 if ($this->start === null) {
                     throw new \Exception("Error: Cannot have duration without a starting date");
                 }
                 $this->duration = new Duration();
                 $this->duration->parse($matches[5]);
             } else {
                 $this->end = new SimpleDate();
                 $this->end->parse($matches[5]);
             }
         }
     }
 }