components)) { $this->components = array(); foreach($this->valid_components as $name) { $this->components[$name] = array(); } } } function get_name() { return $this->name; } function add_property($name, $value = NULL, $parameters = NULL) { // Uppercase first of all $name = strtoupper($name); // Are we trying to add a valid property? $xname = false; if(!isset($this->valid_properties[$name])) { // If not, is it an x-name as per RFC 2445? if(!rfc2445_is_xname($name)) { return false; } // Since this is an xname, all components are supposed to allow this property $xname = true; } // Create a property object of the correct class if($xname) { $property = new iCalendar_property_x; $property->set_name($name); } else { $classname = 'iCalendar_property_'.strtolower(str_replace('-', '_', $name)); $property = new $classname; } // If $value is NULL, then this property must define a default value. if($value === NULL) { $value = $property->default_value(); if($value === NULL) { return false; } } // Set this property's parent component to ourselves, because some // properties behave differently according to what component they apply to. $property->set_parent_component($this->name); // Set parameters before value; this helps with some properties which // accept a VALUE parameter, and thus change their default value type. // The parameters must be valid according to property specifications if(!empty($parameters)) { foreach($parameters as $paramname => $paramvalue) { if(!$property->set_parameter($paramname, $paramvalue)) { return false; } } // Some parameters interact among themselves (e.g. ENCODING and VALUE) // so make sure that after the dust settles, these invariants hold true if(!$property->invariant_holds()) { return false; } } // $value MUST be valid according to the property data type if(!$property->set_value($value)) { return false; } // Check if the property already exists, and is limited to one occurrance, // DON'T overwrite the value - this can be done explicity with set_value() instead. if(!$xname && $this->valid_properties[$name] & RFC2445_ONCE && isset($this->properties[$name])) { return false; } else { // Otherwise add it to the instance array for this property $this->properties[$name][] = $property; } // Finally: after all these, does the component invariant hold? if(!$this->invariant_holds()) { // If not, completely undo the property addition array_pop($this->properties[$name]); if(empty($this->properties[$name])) { unset($this->properties[$name]); } return false; } return true; } function add_component($component) { // With the detailed interface, you can add only components with this function if(!is_object($component) || !is_subclass_of($component, 'iCalendar_component')) { return false; } $name = $component->get_name(); // Only valid components as specified by this component are allowed if(!in_array($name, $this->valid_components)) { return false; } // Add it $this->components[$name][] = $component; return true; } function get_property_list($name) { } function invariant_holds() { return true; } function is_valid() { // If we have any child components, check that they are all valid if(!empty($this->components)) { foreach($this->components as $component => $instances) { foreach($instances as $number => $instance) { if(!$instance->is_valid()) { return false; } } } } // Finally, check the valid property list for any mandatory properties // that have not been set and do not have a default value foreach($this->valid_properties as $property => $propdata) { if(($propdata & RFC2445_REQUIRED) && empty($this->properties[$property])) { $classname = 'iCalendar_property_'.strtolower(str_replace('-', '_', $property)); $object = new $classname; if($object->default_value() === NULL) { return false; } unset($object); } } return true; } function serialize() { // Check for validity of the object if(!$this->is_valid()) { return false; } // Maybe the object is valid, but there are some required properties that // have not been given explicit values. In that case, set them to defaults. foreach($this->valid_properties as $property => $propdata) { if(($propdata & RFC2445_REQUIRED) && empty($this->properties[$property])) { $this->add_property($property); } } // Start tag $string = rfc2445_fold('BEGIN:'.$this->name) . RFC2445_CRLF; // List of properties if(!empty($this->properties)) { foreach($this->properties as $name => $properties) { foreach($properties as $property) { $string .= $property->serialize(); } } } // List of components if(!empty($this->components)) { foreach($this->components as $name => $components) { foreach($components as $component) { $string .= $component->serialize(); } } } // End tag $string .= rfc2445_fold('END:'.$this->name) . RFC2445_CRLF; return $string; } /** * unserialize() * * I needed a way to convert an iCalendar component back to a Bennu object so I could * easily access and modify it after it had been stored; if this functionality is already * present somewhere in the library, I apologize for adding it here unnecessarily; however, * I couldn't find it so I added it myself. * @param string $string the iCalendar object to load in to this iCalendar_component * @return bool true if the file parsed with no errors. False if there were errors. */ function unserialize($string) { $string = rfc2445_unfold($string); // Unfold any long lines $lines = explode(RFC2445_CRLF, $string); // Create an array of lines $components = array(); // Initialise a stack of components $this->clear_errors(); foreach ($lines as $key => $line) { // ignore empty lines if (trim($line) == '') { continue; } // Divide the line up into label, parameters and data fields. if (!preg_match('#^(?P