Make it easier to override setting of properties.
authorJan Schneider <jan@horde.org>
Tue, 15 Sep 2009 22:19:47 +0000 (00:19 +0200)
committerJan Schneider <jan@horde.org>
Wed, 16 Sep 2009 12:54:12 +0000 (14:54 +0200)
Store startDate properties in start.
Add class-property-to-icalendar-property map.
Start working on iCalender 2.0 writer.

framework/Icalendar/lib/Horde/Icalendar/Base.php
framework/Icalendar/lib/Horde/Icalendar/Vevent.php
framework/Icalendar/lib/Horde/Icalendar/Writer/Base.php
framework/Icalendar/lib/Horde/Icalendar/Writer/Vcalendar/20.php

index f99c87f..1b9e4d8 100644 (file)
@@ -53,13 +53,7 @@ abstract class Horde_Icalendar_Base implements Iterator
     public function __set($property, $value)
     {
         $this->_validate($property, $value);
-        if ($this->_properties[$property]['multiple']) {
-            $this->_properties[$property]['value'] = array($value);
-            $this->_properties[$property]['params'] = array();
-        } else {
-            $this->_properties[$property]['value'] = $value;
-            $this->_properties[$property]['params'] = null;
-        }
+        $this->_setProperty($property, $value);
     }
 
     /**
@@ -74,8 +68,8 @@ abstract class Horde_Icalendar_Base implements Iterator
      */
     public function setProperty($property, $value, $params = array())
     {
-        $this->$property = $value;
-        $this->_properties[$property]['params'] = $params;
+        $this->_validate($property, $value);
+        $this->_setProperty($property, $value, $params);
     }
 
     /**
@@ -93,14 +87,35 @@ abstract class Horde_Icalendar_Base implements Iterator
     {
         $this->_validate($property, $value);
         if (!$this->_properties[$property]['multiple'] &&
-            isset($this->_properties[$property]['value'])) {
+            !empty($this->_properties[$property]['values'])) {
             throw new Horde_Icalendar_Exception($property . ' properties must not occur more than once.');
         }
-        if (isset($this->_properties[$property]['value'])) {
-            $this->_properties[$property]['value'][] = $value;
+        $this->_setProperty($property, $value, $params, true);
+    }
+
+    /**
+     * Sets the value of a property.
+     *
+     * @param string $property  The name of the property.
+     * @param string $value     The value of the property.
+     * @param array $params     Array containing any addition parameters for
+     *                          this property.
+     * @param boolean $add      Whether to add (instead of replace) the value.
+     *
+     * @throws InvalidArgumentException
+     */
+    protected function _setProperty($property, $value, $params = array(), $add = false)
+    {
+        if ($add) {
+            if (!isset($this->_properties[$property]['values'])) {
+                $this->_properties[$property]['values'] = array();
+                $this->_properties[$property]['params'] = array();
+            }
+            $this->_properties[$property]['values'][] = $value;
             $this->_properties[$property]['params'][] = $params;
         } else {
-            $this->setProperty($property, $value, $params);
+            $this->_properties[$property]['values'] = array($value);
+            $this->_properties[$property]['params'] = $params;
         }
     }
 
@@ -114,8 +129,10 @@ abstract class Horde_Icalendar_Base implements Iterator
         if (!isset($this->_properties[$property])) {
             throw new InvalidArgumentException($property . ' is not a valid property');
         }
-        return isset ($this->_properties[$property]['value'])
-            ? $this->_properties[$property]['value']
+        return isset($this->_properties[$property]['values'])
+            ? ($this->_properties[$property]['multiple']
+               ? $this->_properties[$property]['values']
+               : $this->_properties[$property]['values'][0])
             : null;
     }
 
@@ -167,7 +184,7 @@ abstract class Horde_Icalendar_Base implements Iterator
     public function validate()
     {
         foreach ($this->_properties as $name => $property) {
-            if (!empty($property['required']) && !isset($property['value'])) {
+            if (!empty($property['required']) && !isset($property['values'])) {
                 switch ($name) {
                 case 'uid':
                     $this->uid = (string)new Horde_Support_Guid;
index 69b097a..f3a5417 100644 (file)
@@ -11,7 +11,7 @@ class Horde_Icalendar_Vevent extends Horde_Icalendar_Base
             'uid' => array('required' => true,
                            'multiple' => false,
                            'type' => 'string'),
-            'start' => array('required' => false,
+            'start' => array('required' => true,
                              'multiple' => false,
                              'class' => 'Horde_Date'),
             'startDate' => array('required' => false,
@@ -29,13 +29,25 @@ class Horde_Icalendar_Vevent extends Horde_Icalendar_Base
         parent::__construct($properties);
     }
 
-    public function validate()
+    /**
+     * Sets the value of a property.
+     *
+     * @param string $property  The name of the property.
+     * @param string $value     The value of the property.
+     * @param array $params     Array containing any addition parameters for
+     *                          this property.
+     * @param boolean $add      Whether to add (instead of replace) the value.
+     *
+     * @throws InvalidArgumentException
+     */
+    protected function _setProperty($property, $value, $params = array(), $add = false)
     {
-        parent::validate();
-        if (!isset($this->_properties['start']['value']) &&
-            !isset($this->_properties['startDate']['value'])) {
-            throw new Horde_Icalendar_Exception('VEVENT components must have a start property set');
+        if ($property == 'startDate') {
+            $this->_validate('start', $value);
+            $property = 'start';
+            $params['value'] = 'date';
         }
+        parent::_setProperty($property, $value, $params, $add);
     }
 
 }
index 7940ba5..bf5a9bd 100644 (file)
@@ -2,6 +2,7 @@
 
 abstract class Horde_Icalendar_Writer_Base
 {
+    protected $_propertyMap = array();
 
     protected $_output = '';
 
@@ -28,8 +29,13 @@ abstract class Horde_Icalendar_Writer_Base
 
     protected function _exportProperty($name, $property)
     {
-        if (isset($property['value'])) {
-            $this->_output .= Horde_String::upper($name) . ':' . $property['value'] . "\n";
+        if (isset($property['values'])) {
+            if (isset($this->_propertyMap[$name])) {
+                $name = $this->_propertyMap[$name];
+            }
+            foreach ($property['values'] as $value) {
+                $this->_output .= Horde_String::upper($name) . ':' . $value . "\n";
+            }
         }
     }
 
index a644c37..645929f 100644 (file)
@@ -2,5 +2,25 @@
 
 class Horde_Icalendar_Writer_Vcalendar_20 extends Horde_Icalendar_Writer_Base
 {
+    protected $_propertyMap = array('product' => 'PRODID',
+                                    'start' => 'DTSTART',
+                                    'stamp' => 'DTSTAMP');
+
+    protected function _exportProperty($name, $property)
+    {
+        if (!isset($property['values'])) {
+            return;
+        }
+        if (isset($property['class']) && $property['class'] == 'Horde_Date') {
+            if (isset($this->_propertyMap[$name])) {
+                $name = $this->_propertyMap[$name];
+            }
+            foreach ($property['values'] as $value) {
+                $this->_output .= Horde_String::upper($name) . ':' . $value->format('Ymd\THms\Z') . "\n";
+            }
+        } else {
+            parent::_exportProperty($name, $property);
+        }
+    }
 
 }