示例#1
0
 /**
  * Using the setter method you can add properties or subcomponents
  *
  * You can either pass a Sabre_VObject_Component, Sabre_VObject_Property
  * object, or a string to automatically create a Property.
  *
  * If the item already exists, it will be removed. If you want to add
  * a new item with the same name, always use the add() method.
  *
  * @param string $name
  * @param mixed $value
  * @return void
  */
 public function __set($name, $value)
 {
     $matches = $this->select($name);
     $overWrite = count($matches) ? key($matches) : null;
     if ($value instanceof Sabre_VObject_Component || $value instanceof Sabre_VObject_Property) {
         $value->parent = $this;
         if (!is_null($overWrite)) {
             $this->children[$overWrite] = $value;
         } else {
             $this->children[] = $value;
         }
     } elseif (is_scalar($value)) {
         $property = Sabre_VObject_Property::create($name, $value);
         $property->parent = $this;
         if (!is_null($overWrite)) {
             $this->children[$overWrite] = $property;
         } else {
             $this->children[] = $property;
         }
     } else {
         throw new InvalidArgumentException('You must pass a Sabre_VObject_Component, Sabre_VObject_Property or scalar type');
     }
 }
示例#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 Sabre_VObject_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 = Sabre_VObject_Component::create($componentName);
         $nextLine = current($lines);
         while (stripos($nextLine, "END:") !== 0) {
             $obj->add(self::readLine($lines));
             $nextLine = current($lines);
             if ($nextLine === false) {
                 throw new Sabre_VObject_ParseException('Invalid VObject. Document ended prematurely.');
             }
         }
         // Checking component name of the 'END:' line.
         if (substr($nextLine, 4) !== $obj->name) {
             throw new Sabre_VObject_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 Sabre_VObject_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 = Sabre_VObject_Property::create($propertyName, $propertyValue);
     if ($matches['parameters']) {
         foreach (self::readParameters($matches['parameters']) as $param) {
             $obj->add($param);
         }
     }
     return $obj;
 }