From 18334d80e9bd41c525647673aabc3fa9a10186ba Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Fri, 11 Sep 2009 00:00:57 +0200 Subject: [PATCH] Re-order classes, only iCalendar has its own base component, but it still works like a regular component. So drop components altogether to save one level in the filesystem/class hierarchy. --- framework/Icalendar/lib/Horde/Icalendar/Base.php | 200 ++++++++++++++++++++- .../lib/Horde/Icalendar/Component/Base.php | 199 -------------------- .../lib/Horde/Icalendar/{Component => }/Valarm.php | 2 +- .../Icalendar/{Icalendar.php => Vcalendar.php} | 2 +- .../lib/Horde/Icalendar/{Component => }/Vevent.php | 2 +- .../Horde/Icalendar/{Component => }/Vfreebusy.php | 2 +- .../Horde/Icalendar/{Component => }/Vjournal.php | 2 +- .../Horde/Icalendar/{Component => }/Vtimezone.php | 2 +- .../lib/Horde/Icalendar/{Component => }/Vtodo.php | 2 +- .../Icalendar/lib/Horde/Icalendar/Writer/Base.php | 3 + .../lib/Horde/Icalendar/Writer/Icalendar/20.php | 6 - .../lib/Horde/Icalendar/Writer/Vcalendar/20.php | 6 + .../Icalendar/test/Horde/Icalendar/WriterTest.php | 12 +- 13 files changed, 218 insertions(+), 222 deletions(-) delete mode 100644 framework/Icalendar/lib/Horde/Icalendar/Component/Base.php rename framework/Icalendar/lib/Horde/Icalendar/{Component => }/Valarm.php (85%) rename framework/Icalendar/lib/Horde/Icalendar/{Icalendar.php => Vcalendar.php} (74%) rename framework/Icalendar/lib/Horde/Icalendar/{Component => }/Vevent.php (94%) rename framework/Icalendar/lib/Horde/Icalendar/{Component => }/Vfreebusy.php (91%) rename framework/Icalendar/lib/Horde/Icalendar/{Component => }/Vjournal.php (90%) rename framework/Icalendar/lib/Horde/Icalendar/{Component => }/Vtimezone.php (89%) rename framework/Icalendar/lib/Horde/Icalendar/{Component => }/Vtodo.php (93%) delete mode 100644 framework/Icalendar/lib/Horde/Icalendar/Writer/Icalendar/20.php create mode 100644 framework/Icalendar/lib/Horde/Icalendar/Writer/Vcalendar/20.php diff --git a/framework/Icalendar/lib/Horde/Icalendar/Base.php b/framework/Icalendar/lib/Horde/Icalendar/Base.php index b455f1977..59ad336bc 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Base.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Base.php @@ -1,11 +1,16 @@ _params = $params; } - public function addComponent(Horde_Icalendar_Component_Base $component) + /** + * Validates a property-value-pair. + * + * @throws InvalidArgumentException + */ + protected function _validate($property, &$value, &$params = array()) + { + if (!isset($this->_properties[$property])) { + throw new InvalidArgumentException($property . ' is not a valid property'); + } + $myProperty = &$this->_properties[$property]; + if (isset($myProperty['type'])) { + $func = 'is_' . $myProperty['type']; + if (!$func) { + throw new InvalidArgumentException($value . ' is not a ' . $myProperty['type']); + } + } elseif (isset($myProperty['class'])) { + if (!($value instanceof $myProperty['class'])) { + throw new InvalidArgumentException($value . ' is not of class ' . $myProperty['class']); + } + } + if ($property == 'stamp') { + $value->setTimezone('UTC'); + } + } + + /** + * Setter. + * + * @throws InvalidArgumentException + */ + 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; + } + } + + /** + * 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. + * + * @throws InvalidArgumentException + */ + public function setProperty($property, $value, $params = array()) + { + $this->$name = $value; + $this->_properties[$property]['params'] = array($params); + } + + /** + * Adds 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. + * + * @throws InvalidArgumentException + * @throws Horde_Icalendar_Exception + */ + public function addProperty($property, $value, $params = array()) + { + $this->_validate($property, $value); + if (!$this->_properties[$property]['multiple'] && + !isset($this->_properties[$property]['value'])) { + 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->_properties[$property]['params'][] = $params; + } else { + $this->setProperty($property, $value, $params); + } + } + + /** + * Getter. + * + * @throws InvalidArgumentException + */ + public function __get($property) + { + if (!isset($this->_properties[$property])) { + throw new InvalidArgumentException($property . ' is not a valid property'); + } + return isset ($this->_properties[$property]['value']) + ? $this->_properties[$property]['value'] + : null; + } + + /** + * Returns the value of an property. + * + * @param string $name The name of the property. + * @param boolean $params Return the parameters for this property instead + * of its value. + * + * @return mixed (object) PEAR_Error if the property does not exist. + * (string) The value of the property. + * (array) The parameters for the property or + * multiple values for an property. + */ + function getProperty($name, $params = false) + { + $result = array(); + foreach ($this->_properties as $property) { + if ($property['name'] == $name) { + if ($params) { + $result[] = $property['params']; + } else { + $result[] = $property['value']; + } + } + } + if (!count($result)) { + require_once 'PEAR.php'; + return PEAR::raiseError('Property "' . $name . '" Not Found'); + } if (count($result) == 1 && !$params) { + return $result[0]; + } else { + return $result; + } + } + + public function getProperties() + { + return $this->_properties; + } + + /** + * Validates the complete component for missing properties or invalid + * property combinations. + * + * @throws Horde_Icalendar_Exception + */ + public function validate() + { + foreach ($this->_properties as $name => $property) { + if (!empty($property['required']) && !isset($property['value'])) { + switch ($name) { + case 'uid': + $this->uid = (string)new Horde_Support_Guid; + break; + case 'stamp': + $this->stamp = new Horde_Date(time()); + 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'); + } + } + } + } + + public function current() + { + return current($this->_properties); + } + + public function key() + { + return key($this->_properties); + } + + public function next() + { + next($this->_properties); + } + + public function rewind() + { + reset($this->_properties); + } + + public function valid() { - $this->_components[] = $component; + return current($this->_properties) !== false; } /** @@ -24,9 +214,11 @@ abstract class Horde_Icalendar_Base */ public function export() { + $this->validate(); $writer = Horde_Icalendar_Writer::factory( str_replace('Horde_Icalendar_', '', get_class($this)), str_replace('.', '', $this->_params['version'])); + return $writer->export($this); } } diff --git a/framework/Icalendar/lib/Horde/Icalendar/Component/Base.php b/framework/Icalendar/lib/Horde/Icalendar/Component/Base.php deleted file mode 100644 index d2ead5a7c..000000000 --- a/framework/Icalendar/lib/Horde/Icalendar/Component/Base.php +++ /dev/null @@ -1,199 +0,0 @@ -_properties[$property])) { - throw new InvalidArgumentException($property . ' is not a valid property'); - } - $myProperty = &$this->_properties[$property]; - if (isset($myProperty['type'])) { - $func = 'is_' . $myProperty['type']; - if (!$func) { - throw new InvalidArgumentException($value . ' is not a ' . $myProperty['type']); - } - } elseif (isset($myProperty['class'])) { - if (!($value instanceof $myProperty['class'])) { - throw new InvalidArgumentException($value . ' is not of class ' . $myProperty['class']); - } - } - if ($property == 'stamp') { - $value->setTimezone('UTC'); - } - } - - /** - * Setter. - * - * @throws InvalidArgumentException - */ - 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; - } - } - - /** - * 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. - * - * @throws InvalidArgumentException - */ - public function setProperty($property, $value, $params = array()) - { - $this->$name = $value; - $this->_properties[$property]['params'] = array($params); - } - - /** - * Adds 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. - * - * @throws InvalidArgumentException - * @throws Horde_Icalendar_Exception - */ - public function addProperty($property, $value, $params = array()) - { - $this->_validate($property, $value); - if (!$this->_properties[$property]['multiple'] && - !isset($this->_properties[$property]['value'])) { - 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->_properties[$property]['params'][] = $params; - } else { - $this->setProperty($property, $value, $params); - } - } - - /** - * Getter. - * - * @throws InvalidArgumentException - */ - public function __get($property) - { - if (!isset($this->_properties[$property])) { - throw new InvalidArgumentException($property . ' is not a valid property'); - } - return isset ($this->_properties[$property]['value']) - ? $this->_properties[$property]['value'] - : null; - } - - /** - * Returns the value of an property. - * - * @param string $name The name of the property. - * @param boolean $params Return the parameters for this property instead - * of its value. - * - * @return mixed (object) PEAR_Error if the property does not exist. - * (string) The value of the property. - * (array) The parameters for the property or - * multiple values for an property. - */ - function getProperty($name, $params = false) - { - $result = array(); - foreach ($this->_properties as $property) { - if ($property['name'] == $name) { - if ($params) { - $result[] = $property['params']; - } else { - $result[] = $property['value']; - } - } - } - if (!count($result)) { - require_once 'PEAR.php'; - return PEAR::raiseError('Property "' . $name . '" Not Found'); - } if (count($result) == 1 && !$params) { - return $result[0]; - } else { - return $result; - } - } - - public function getProperties() - { - return $this->_properties; - } - - /** - * Validates the complete component for missing properties or invalid - * property combinations. - * - * @throws Horde_Icalendar_Exception - */ - public function validate() - { - foreach ($this->_properties as $name => $property) { - if (!empty($property['required']) && !isset($property['value'])) { - switch ($name) { - case 'uid': - $this->uid = (string)new Horde_Support_Guid; - break; - case 'stamp': - $this->stamp = new Horde_Date(time()); - break; - default: - $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'); - } - } - } - } - - public function current() - { - return current($this->_properties); - } - - public function key() - { - return key($this->_properties); - } - - public function next() - { - next($this->_properties); - } - - public function rewind() - { - reset($this->_properties); - } - - public function valid() - { - return current($this->_properties) !== false; - } - -} diff --git a/framework/Icalendar/lib/Horde/Icalendar/Component/Valarm.php b/framework/Icalendar/lib/Horde/Icalendar/Valarm.php similarity index 85% rename from framework/Icalendar/lib/Horde/Icalendar/Component/Valarm.php rename to framework/Icalendar/lib/Horde/Icalendar/Valarm.php index b1556ad07..6f42e0937 100644 --- a/framework/Icalendar/lib/Horde/Icalendar/Component/Valarm.php +++ b/framework/Icalendar/lib/Horde/Icalendar/Valarm.php @@ -1,6 +1,6 @@ '2.0')); - $event1 = new Horde_Icalendar_Component_Vevent(); - $event2 = new Horde_Icalendar_Component_Vevent(); + $ical = new Horde_Icalendar_Vcalendar(array('version' => '2.0')); + $event1 = new Horde_Icalendar_Vevent(); + $event2 = new Horde_Icalendar_Vevent(); $event1->uid = '20041120-8550-innerjoin-org'; $event1->startDate = new Horde_Date(array('year' => 2005, 'month' => 5, 'mday' => 3)); @@ -36,8 +36,8 @@ class Horde_Icalendar_WriterTest extends Horde_Test_Case $event2->summary = 'Dash (rather than Comma) in the Description Field'; $event2->description = 'There are important words after this dash - see anything here or have the words gone?'; - $ical->addComponent($event1); - $ical->addComponent($event2); + $ical->components[] = $event1; + $ical->components[] = $event2; $this->assertEquals('BEGIN:VCALENDAR VERSION:2.0 @@ -60,7 +60,7 @@ DESCRIPTION:There are important words after this dash - see anything here or have the words gone? END:VEVENT END:VCALENDAR', - $ical->export()); + $ical->export()); } } \ No newline at end of file -- 2.11.0