From 4a0ed8952cfefcdfb36a96ba21d2a80b0746c6b6 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Fri, 11 Sep 2009 00:33:41 +0200 Subject: [PATCH] First bits of the writer. Yay, we got output. --- framework/Icalendar/lib/Horde/Icalendar/Base.php | 20 +++++++------- framework/Icalendar/lib/Horde/Icalendar/Valarm.php | 3 ++- .../Icalendar/lib/Horde/Icalendar/Vcalendar.php | 17 +++++++++--- framework/Icalendar/lib/Horde/Icalendar/Vevent.php | 3 ++- .../Icalendar/lib/Horde/Icalendar/Vfreebusy.php | 3 ++- .../Icalendar/lib/Horde/Icalendar/Vjournal.php | 3 ++- .../Icalendar/lib/Horde/Icalendar/Vtimezone.php | 3 ++- framework/Icalendar/lib/Horde/Icalendar/Vtodo.php | 3 ++- .../Icalendar/lib/Horde/Icalendar/Writer/Base.php | 31 ++++++++++++++++++++-- 9 files changed, 65 insertions(+), 21 deletions(-) diff --git a/framework/Icalendar/lib/Horde/Icalendar/Base.php b/framework/Icalendar/lib/Horde/Icalendar/Base.php index 59ad336bc..f99c87f17 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Base.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Base.php @@ -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); } diff --git a/framework/Icalendar/lib/Horde/Icalendar/Valarm.php b/framework/Icalendar/lib/Horde/Icalendar/Valarm.php index 6f42e0937..5f095b94b 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Valarm.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Valarm.php @@ -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); } } diff --git a/framework/Icalendar/lib/Horde/Icalendar/Vcalendar.php b/framework/Icalendar/lib/Horde/Icalendar/Vcalendar.php index 90ee456f9..341611345 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Vcalendar.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Vcalendar.php @@ -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); } } diff --git a/framework/Icalendar/lib/Horde/Icalendar/Vevent.php b/framework/Icalendar/lib/Horde/Icalendar/Vevent.php index cbeb56b4b..69b097a13 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Vevent.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Vevent.php @@ -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() diff --git a/framework/Icalendar/lib/Horde/Icalendar/Vfreebusy.php b/framework/Icalendar/lib/Horde/Icalendar/Vfreebusy.php index b68126d55..d7040d1a7 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Vfreebusy.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Vfreebusy.php @@ -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); } /** diff --git a/framework/Icalendar/lib/Horde/Icalendar/Vjournal.php b/framework/Icalendar/lib/Horde/Icalendar/Vjournal.php index 7f027d777..094d91fa1 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Vjournal.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Vjournal.php @@ -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); } } diff --git a/framework/Icalendar/lib/Horde/Icalendar/Vtimezone.php b/framework/Icalendar/lib/Horde/Icalendar/Vtimezone.php index 56297b4d5..86f06c17b 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Vtimezone.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Vtimezone.php @@ -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); } } diff --git a/framework/Icalendar/lib/Horde/Icalendar/Vtodo.php b/framework/Icalendar/lib/Horde/Icalendar/Vtodo.php index ea30f4479..ee4e74b79 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Vtodo.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Vtodo.php @@ -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); } } diff --git a/framework/Icalendar/lib/Horde/Icalendar/Writer/Base.php b/framework/Icalendar/lib/Horde/Icalendar/Writer/Base.php index ad5d0451c..7940ba5d5 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Writer/Base.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Writer/Base.php @@ -1,9 +1,36 @@ _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"; + } } } -- 2.11.0