First bits of the writer. Yay, we got output.
authorJan Schneider <jan@horde.org>
Thu, 10 Sep 2009 22:33:41 +0000 (00:33 +0200)
committerJan Schneider <jan@horde.org>
Thu, 10 Sep 2009 22:33:41 +0000 (00:33 +0200)
framework/Icalendar/lib/Horde/Icalendar/Base.php
framework/Icalendar/lib/Horde/Icalendar/Valarm.php
framework/Icalendar/lib/Horde/Icalendar/Vcalendar.php
framework/Icalendar/lib/Horde/Icalendar/Vevent.php
framework/Icalendar/lib/Horde/Icalendar/Vfreebusy.php
framework/Icalendar/lib/Horde/Icalendar/Vjournal.php
framework/Icalendar/lib/Horde/Icalendar/Vtimezone.php
framework/Icalendar/lib/Horde/Icalendar/Vtodo.php
framework/Icalendar/lib/Horde/Icalendar/Writer/Base.php

index 59ad336..f99c87f 100644 (file)
@@ -12,11 +12,11 @@ abstract class Horde_Icalendar_Base implements Iterator
      */
     protected $_properties = array();
 
-    protected $_params;
-
-    public function __construct($params)
+    public function __construct($properties = array())
     {
-        $this->_params = $params;
+        foreach ($properties as $property => $value) {
+            $this->addProperty($property, $value);
+        }
     }
 
     /**
@@ -74,8 +74,8 @@ abstract class Horde_Icalendar_Base implements Iterator
      */
     public function setProperty($property, $value, $params = array())
     {
-        $this->$name = $value;
-        $this->_properties[$property]['params'] = array($params);
+        $this->$property = $value;
+        $this->_properties[$property]['params'] = $params;
     }
 
     /**
@@ -93,7 +93,7 @@ abstract class Horde_Icalendar_Base implements Iterator
     {
         $this->_validate($property, $value);
         if (!$this->_properties[$property]['multiple'] &&
-            !isset($this->_properties[$property]['value'])) {
+            isset($this->_properties[$property]['value'])) {
             throw new Horde_Icalendar_Exception($property . ' properties must not occur more than once.');
         }
         if (isset($this->_properties[$property]['value'])) {
@@ -177,8 +177,8 @@ abstract class Horde_Icalendar_Base implements Iterator
                     break;
                 default:
                     // @todo Use LSB (static::__CLASS__) once we require PHP 5.3
-                    $component = Horde_String::upper(str_replace('Horde_Icalendar_Component_', '', get_class($this)));
-                    throw new Horde_Icalendar_Exception($component . ' components must have a ' . $name . ' property set');
+                    $component = Horde_String::upper(str_replace('Horde_Icalendar_', '', get_class($this)));
+                    throw new Horde_Icalendar_Exception('This ' . $component . ' component must have a ' . $name . ' property set');
                 }
             }
         }
@@ -217,7 +217,7 @@ abstract class Horde_Icalendar_Base implements Iterator
         $this->validate();
         $writer = Horde_Icalendar_Writer::factory(
             str_replace('Horde_Icalendar_', '', get_class($this)),
-            str_replace('.', '', $this->_params['version']));
+            str_replace('.', '', $this->version));
         return $writer->export($this);
     }
 
index 6f42e09..5f095b9 100644 (file)
@@ -5,7 +5,7 @@ class Horde_Icalendar_Valarm extends Horde_Icalendar_Base
     /**
      * Constructor.
      */
-    public function __construct()
+    public function __construct($properties = array())
     {
         $this->_properties += array(
             'summary' => array('required' => false,
@@ -14,6 +14,7 @@ class Horde_Icalendar_Valarm extends Horde_Icalendar_Base
             'description' => array('required' => false,
                                    'multiple' => false,
                                    'type' => 'string'));
+        parent::__construct($properties);
     }
 
 }
index 90ee456..3416113 100644 (file)
@@ -2,10 +2,21 @@
 
 class Horde_Icalendar_Vcalendar extends Horde_Icalendar_Base
 {
-    public function __construct($params = array())
+    public function __construct($properties = array())
     {
-        $params = array_merge(array('version' => '2.0'), $params);
-        parent::__construct($params);
+        $this->_properties += array(
+            'version' => array('required' => true,
+                               'multiple' => false,
+                               'type' => 'string'),
+            'product' => array('required' => true,
+                               'multiple' => false,
+                               'type' => 'string')
+        );
+
+        $properties = array_merge(array('version' => '2.0',
+                                        'product' => '-//The Horde Project//Horde_Icalendar Library//EN'),
+                                  $properties);
+        parent::__construct($properties);
     }
 
 }
index cbeb56b..69b097a 100644 (file)
@@ -5,7 +5,7 @@ class Horde_Icalendar_Vevent extends Horde_Icalendar_Base
     /**
      * Constructor.
      */
-    public function __construct()
+    public function __construct($properties = array())
     {
         $this->_properties += array(
             'uid' => array('required' => true,
@@ -26,6 +26,7 @@ class Horde_Icalendar_Vevent extends Horde_Icalendar_Base
             'description' => array('required' => false,
                                    'multiple' => false,
                                    'type' => 'string'));
+        parent::__construct($properties);
     }
 
     public function validate()
index b68126d..d7040d1 100644 (file)
@@ -5,7 +5,7 @@ class Horde_Icalendar_Vfreebusy extends Horde_Icalendar_Base
     /**
      * Constructor.
      */
-    public function __construct()
+    public function __construct($properties = array())
     {
         $this->_properties += array(
             'uid' => array('required' => true,
@@ -18,6 +18,7 @@ class Horde_Icalendar_Vfreebusy extends Horde_Icalendar_Base
                              'multiple' => false,
                              'class' => 'Horde_Date'),
         );
+        parent::__construct($properties);
     }
 
     /**
index 7f027d7..094d91f 100644 (file)
@@ -5,7 +5,7 @@ class Horde_Icalendar_Vjournal extends Horde_Icalendar_Base
     /**
      * Constructor.
      */
-    public function __construct()
+    public function __construct($properties = array())
     {
         $this->_properties += array(
             'uid' => array('required' => true,
@@ -20,6 +20,7 @@ class Horde_Icalendar_Vjournal extends Horde_Icalendar_Base
             'description' => array('required' => false,
                                    'multiple' => false,
                                    'type' => 'string'));
+        parent::__construct($properties);
     }
 
 }
index 56297b4..86f06c1 100644 (file)
@@ -5,7 +5,7 @@ class Horde_Icalendar_Vtimezone extends Horde_Icalendar_Base
     /**
      * Constructor.
      */
-    public function __construct()
+    public function __construct($properties = array())
     {
         $this->_properties += array(
             /*
@@ -19,6 +19,7 @@ class Horde_Icalendar_Vtimezone extends Horde_Icalendar_Base
                              'multiple' => false,
                              'class' => 'Horde_Date'),
         );
+        parent::__construct($properties);
     }
 
 }
index ea30f44..ee4e74b 100644 (file)
@@ -5,7 +5,7 @@ class Horde_Icalendar_Vtodo extends Horde_Icalendar_Base
     /**
      * Constructor.
      */
-    public function __construct()
+    public function __construct($properties = array())
     {
         $this->_properties += array(
             'uid' => array('required' => true,
@@ -26,6 +26,7 @@ class Horde_Icalendar_Vtodo extends Horde_Icalendar_Base
             'description' => array('required' => false,
                                    'multiple' => false,
                                    'type' => 'string'));
+        parent::__construct($properties);
     }
 
 }
index ad5d045..7940ba5 100644 (file)
@@ -1,9 +1,36 @@
 <?php
 
-class Horde_Icalendar_Writer_Base
+abstract class Horde_Icalendar_Writer_Base
 {
-    public function export($component)
+
+    protected $_output = '';
+
+    public function export($object)
+    {
+        $this->_output = '';
+        $this->_exportComponent($object);
+        return $this->_output;
+    }
+
+    protected function _exportComponent($object)
+    {
+        $basename = Horde_String::upper(str_replace('Horde_Icalendar_', '', get_class($object)));
+        $this->_output .= 'BEGIN:' . $basename . "\n";
+        foreach ($object as $name => $property) {
+            $this->_exportProperty($name, $property);
+        }
+        foreach ($object->components as $component) {
+            $this->_exportComponent($component);
+        }
+        $this->_output .= 'END:' . $basename . "\n";
+        return $this->_output;
+    }
+
+    protected function _exportProperty($name, $property)
     {
+        if (isset($property['value'])) {
+            $this->_output .= Horde_String::upper($name) . ':' . $property['value'] . "\n";
+        }
     }
 
 }