From 7d62756fb2cc44d1739261b0f46dd9f52b3960ab Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Wed, 16 Sep 2009 00:19:47 +0200 Subject: [PATCH] Make it easier to override setting of properties. 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 | 49 +++++++++++++++------- framework/Icalendar/lib/Horde/Icalendar/Vevent.php | 24 ++++++++--- .../Icalendar/lib/Horde/Icalendar/Writer/Base.php | 10 ++++- .../lib/Horde/Icalendar/Writer/Vcalendar/20.php | 20 +++++++++ 4 files changed, 79 insertions(+), 24 deletions(-) diff --git a/framework/Icalendar/lib/Horde/Icalendar/Base.php b/framework/Icalendar/lib/Horde/Icalendar/Base.php index f99c87f17..1b9e4d8ca 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Base.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Base.php @@ -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; diff --git a/framework/Icalendar/lib/Horde/Icalendar/Vevent.php b/framework/Icalendar/lib/Horde/Icalendar/Vevent.php index 69b097a13..f3a541775 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Vevent.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Vevent.php @@ -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); } } diff --git a/framework/Icalendar/lib/Horde/Icalendar/Writer/Base.php b/framework/Icalendar/lib/Horde/Icalendar/Writer/Base.php index 7940ba5d5..bf5a9bd39 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Writer/Base.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Writer/Base.php @@ -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"; + } } } diff --git a/framework/Icalendar/lib/Horde/Icalendar/Writer/Vcalendar/20.php b/framework/Icalendar/lib/Horde/Icalendar/Writer/Vcalendar/20.php index a644c374b..645929f43 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Writer/Vcalendar/20.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Writer/Vcalendar/20.php @@ -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); + } + } } -- 2.11.0