4 * Base class of all iCalendar components
7 * @http://www.projectpier.org/
9 abstract class iCalendar_Component
{
12 * Names of valid properties. Names are uppercase
16 private $valid_properties = array();
19 * Classes of valid components. Names are in uppercase
23 private $valid_components = array();
26 * Name of this component (VCALENDAR, VEVENT, VTODO). This value is always uppercased
33 * Array of property values (property name => property value)
37 private $property_values = array();
40 * Array of subcomponents for this specific component
44 private $components = array();
47 * Return specific property value
49 * @param string $property_name
50 * @param mixed $default
53 function getPropertyValue($property_name, $default = null) {
54 $search_for = strtoupper($property_name);
55 return isset($this->property_values
[$property_name]) ?
$this->property_values
[$property_name] : $default;
59 * Set value of specific property
61 * @param string $property_name
63 * @param mixed $attributes Value attributes
65 * @throws iCalendar_InvalidComponentPropertyError
67 function setPropertyValue($property_name, $value, $attributes = null) {
68 $prepared_property_name = strtoupper($property_name);
69 if(!$this->isValidProperty($prepared_property_name)) {
70 throw new iCalendar_InvalidComponentPropertyError($this->getName(), $prepared_property_name);
72 $this->property_values
[$property_name] = new iCalendar_PropertyValue($value, $attributes);
76 * Add new subcomponent to this component
78 * @param iCalendar_Component $component
80 * @throws iCalendar_InvalidComponentSubcomponentError
82 function addComponent(iCalendar_Component
$component) {
83 if(!$this->isValidComponent($component)) {
84 throw new iCalendar_InvalidComponentSubcomponentError($this->getName(), $component->getName());
86 $this->components
[] = $component;
90 * Check if specific property is valid in case of this component
92 * @param string $property_name
95 function isValidProperty($property_name) {
96 $search_for = strtoupper($property_name);
97 return str_starts_with($search_for, 'X-') ||
in_array($search_for, $this->valid_properties
);
101 * Check if $component is valid subcomponent
103 * @param iCalendar_Component $component
106 function isValidComponent(iCalendar_Component
$component) {
107 $search_for = $component->getName();
108 return in_array($search_for, $this->valid_components
);
109 } // isValidComponent
112 * Render this specific component
118 $parts = array('BEGIN:' . $this->getName());
119 //pre_var_dump($this->getName());
122 foreach($this->property_values
as $property => $value) {
123 $parts[] = $value->render($property);
126 // Now render components
127 foreach($this->components
as $component) {
128 $parts[] = $component->render();
131 $parts[] = 'END:' . $this->getName();
133 return implode("\r\n", $parts);
136 // ---------------------------------------------------
137 // Getters and setters
138 // ---------------------------------------------------
141 * Get valid_properties
146 function getValidProperties() {
147 return $this->valid_properties
;
148 } // getValidProperties
151 * Set valid_properties value. Insert them like $this->setValidProperties('STATUS', 'CATEGORY', 'CLASS')
156 function setValidProperties() {
157 $value = func_get_args();
158 if(is_array($value)) {
159 foreach($value as $k => $v) {
160 $value[$k] = strtoupper($v);
162 $this->valid_properties
= $value;
164 $this->valid_properties
= array();
166 } // setValidProperties
169 * Get valid_components
174 function getValidComponents() {
175 return $this->valid_components
;
176 } // getValidComponents
179 * Set valid_components value. Input them like $this->setValidComponents('VEVENT', 'VCALENDAR')
184 function setValidComponents() {
185 $value = func_get_args();
186 if(is_array($value)) {
187 foreach($value as $k => $v) {
188 $value[$k] = strtoupper($v);
190 $this->valid_components
= $value;
192 $this->valid_components
;
194 } // setValidComponents
209 * @param string $value
212 protected function setName($value) {
213 $this->name
= strtoupper($value);
217 * Return all property values
222 function getPropertyValues() {
223 return $this->property_values
;
224 } // getPropertyValues
227 * Return all subcomponents
232 function getComponents() {
233 return $this->components
;
236 } // iCalendar_Component