示例#1
0
 /**
  * Creates a new component
  *
  * This method automatically searches for the correct component class, based
  * on its name.
  *
  * You can specify the children either in key=>value syntax, in which case
  * properties will automatically be created, or you can just pass a list of
  * Component and Property object.
  *
  * @param string $name
  * @param array $children
  * @return Component
  */
 public function createComponent($name, array $children = array())
 {
     $component = Component::create($name);
     foreach ($children as $k => $v) {
         if ($v instanceof Node) {
             $component->add($v);
         } else {
             $component->add($k, $v);
         }
     }
     return $component;
 }
示例#2
0
 /**
  * Reads and parses a single line.
  *
  * This method receives the full array of lines. The array pointer is used
  * to traverse.
  *
  * @param array $lines
  * @return Element
  */
 private static function readLine(&$lines)
 {
     $line = current($lines);
     $lineNr = key($lines);
     next($lines);
     // Components
     if (stripos($line, "BEGIN:") === 0) {
         $componentName = strtoupper(substr($line, 6));
         $obj = Component::create($componentName);
         $nextLine = current($lines);
         while (stripos($nextLine, "END:") !== 0) {
             $obj->add(self::readLine($lines));
             $nextLine = current($lines);
             if ($nextLine === false) {
                 throw new ParseException('Invalid VObject. Document ended prematurely.');
             }
         }
         // Checking component name of the 'END:' line.
         if (substr($nextLine, 4) !== $obj->name) {
             throw new ParseException('Invalid VObject, expected: "END:' . $obj->name . '" got: "' . $nextLine . '"');
         }
         next($lines);
         return $obj;
     }
     // Properties
     //$result = preg_match('/(?P<name>[A-Z0-9-]+)(?:;(?P<parameters>^(?<!:):))(.*)$/',$line,$matches);
     $token = '[A-Z0-9-\\.]+';
     $parameters = "(?:;(?P<parameters>([^:^\"]|\"([^\"]*)\")*))?";
     $regex = "/^(?P<name>{$token}){$parameters}:(?P<value>.*)\$/i";
     $result = preg_match($regex, $line, $matches);
     if (!$result) {
         throw new ParseException('Invalid VObject, line ' . ($lineNr + 1) . ' did not follow the icalendar/vcard format');
     }
     $propertyName = strtoupper($matches['name']);
     $propertyValue = preg_replace_callback('#(\\\\(\\\\|N|n|;|,))#', function ($matches) {
         if ($matches[2] === 'n' || $matches[2] === 'N') {
             return "\n";
         } else {
             return $matches[2];
         }
     }, $matches['value']);
     $obj = Property::create($propertyName, $propertyValue);
     if ($matches['parameters']) {
         foreach (self::readParameters($matches['parameters']) as $param) {
             $obj->add($param);
         }
     }
     return $obj;
 }
 /**
  * Parses the input data and returns a correct VFREEBUSY object, wrapped in
  * a VCALENDAR.
  *
  * @return Component
  */
 public function getResult()
 {
     $busyTimes = array();
     foreach ($this->objects as $object) {
         foreach ($object->getBaseComponents() as $component) {
             switch ($component->name) {
                 case 'VEVENT':
                     $FBTYPE = 'BUSY';
                     if (isset($component->TRANSP) && strtoupper($component->TRANSP) === 'TRANSPARENT') {
                         break;
                     }
                     if (isset($component->STATUS)) {
                         $status = strtoupper($component->STATUS);
                         if ($status === 'CANCELLED') {
                             break;
                         }
                         if ($status === 'TENTATIVE') {
                             $FBTYPE = 'BUSY-TENTATIVE';
                         }
                     }
                     $times = array();
                     if ($component->RRULE) {
                         $iterator = new RecurrenceIterator($object, (string) $component->uid);
                         if ($this->start) {
                             $iterator->fastForward($this->start);
                         }
                         $maxRecurrences = 200;
                         while ($iterator->valid() && --$maxRecurrences) {
                             $startTime = $iterator->getDTStart();
                             if ($this->end && $startTime > $this->end) {
                                 break;
                             }
                             $times[] = array($iterator->getDTStart(), $iterator->getDTEnd());
                             $iterator->next();
                         }
                     } else {
                         $startTime = $component->DTSTART->getDateTime();
                         if ($this->end && $startTime > $this->end) {
                             break;
                         }
                         $endTime = null;
                         if (isset($component->DTEND)) {
                             $endTime = $component->DTEND->getDateTime();
                         } elseif (isset($component->DURATION)) {
                             $duration = DateTimeParser::parseDuration((string) $component->DURATION);
                             $endTime = clone $startTime;
                             $endTime->add($duration);
                         } elseif ($component->DTSTART->getDateType() === Property\DateTime::DATE) {
                             $endTime = clone $startTime;
                             $endTime->modify('+1 day');
                         } else {
                             // The event had no duration (0 seconds)
                             break;
                         }
                         $times[] = array($startTime, $endTime);
                     }
                     foreach ($times as $time) {
                         if ($this->end && $time[0] > $this->end) {
                             break;
                         }
                         if ($this->start && $time[1] < $this->start) {
                             break;
                         }
                         $busyTimes[] = array($time[0], $time[1], $FBTYPE);
                     }
                     break;
                 case 'VFREEBUSY':
                     foreach ($component->FREEBUSY as $freebusy) {
                         $fbType = isset($freebusy['FBTYPE']) ? strtoupper($freebusy['FBTYPE']) : 'BUSY';
                         // Skipping intervals marked as 'free'
                         if ($fbType === 'FREE') {
                             continue;
                         }
                         $values = explode(',', $freebusy);
                         foreach ($values as $value) {
                             list($startTime, $endTime) = explode('/', $value);
                             $startTime = DateTimeParser::parseDateTime($startTime);
                             if (substr($endTime, 0, 1) === 'P' || substr($endTime, 0, 2) === '-P') {
                                 $duration = DateTimeParser::parseDuration($endTime);
                                 $endTime = clone $startTime;
                                 $endTime->add($duration);
                             } else {
                                 $endTime = DateTimeParser::parseDateTime($endTime);
                             }
                             if ($this->start && $this->start > $endTime) {
                                 continue;
                             }
                             if ($this->end && $this->end < $startTime) {
                                 continue;
                             }
                             $busyTimes[] = array($startTime, $endTime, $fbType);
                         }
                     }
                     break;
             }
         }
     }
     if ($this->baseObject) {
         $calendar = $this->baseObject;
     } else {
         $calendar = Component::create('VCALENDAR');
         $calendar->version = '2.0';
         $calendar->prodid = '-//Sabre//Sabre VObject ' . Version::VERSION . '//EN';
         $calendar->calscale = 'GREGORIAN';
     }
     $vfreebusy = Component::create('VFREEBUSY');
     $calendar->add($vfreebusy);
     if ($this->start) {
         $dtstart = Property::create('DTSTART');
         $dtstart->setDateTime($this->start, Property\DateTime::UTC);
         $vfreebusy->add($dtstart);
     }
     if ($this->end) {
         $dtend = Property::create('DTEND');
         $dtend->setDateTime($this->end, Property\DateTime::UTC);
         $vfreebusy->add($dtend);
     }
     $dtstamp = Property::create('DTSTAMP');
     $dtstamp->setDateTime(new \DateTime('now'), Property\DateTime::UTC);
     $vfreebusy->add($dtstamp);
     foreach ($busyTimes as $busyTime) {
         $busyTime[0]->setTimeZone(new \DateTimeZone('UTC'));
         $busyTime[1]->setTimeZone(new \DateTimeZone('UTC'));
         $prop = Property::create('FREEBUSY', $busyTime[0]->format('Ymd\\THis\\Z') . '/' . $busyTime[1]->format('Ymd\\THis\\Z'));
         $prop['FBTYPE'] = $busyTime[2];
         $vfreebusy->add($prop);
     }
     return $calendar;
 }
示例#4
0
 function testAddScalarParams()
 {
     $comp = Component::create('VCALENDAR');
     $comp->add('myprop', 'value', array('param1' => 'value1'));
     $this->assertEquals(1, count($comp->children));
     $this->assertTrue($comp->children[0] instanceof Property);
     $this->assertEquals('MYPROP', $comp->children[0]->name);
     $this->assertEquals('value', $comp->children[0]->value);
     $this->assertEquals(1, count($comp->children[0]->parameters));
     $this->assertTrue($comp->children[0]->parameters[0] instanceof Parameter);
     $this->assertEquals('PARAM1', $comp->children[0]->parameters[0]->name);
     $this->assertEquals('value1', $comp->children[0]->parameters[0]->value);
 }