first import
[projectpier.git] / library / icalendar / components / iCalendar_Component.class.php
blob78a822c54a0cf46893475a6519364523d1729787
1 <?php
3 /**
4 * Base class of all iCalendar components
6 * @package iCalendar
7 * @http://www.projectpier.org/
8 */
9 abstract class iCalendar_Component {
11 /**
12 * Names of valid properties. Names are uppercase
14 * @var array
16 private $valid_properties = array();
18 /**
19 * Classes of valid components. Names are in uppercase
21 * @var array
23 private $valid_components = array();
25 /**
26 * Name of this component (VCALENDAR, VEVENT, VTODO). This value is always uppercased
28 * @var string
30 private $name;
32 /**
33 * Array of property values (property name => property value)
35 * @var array
37 private $property_values = array();
39 /**
40 * Array of subcomponents for this specific component
42 * @var array
44 private $components = array();
46 /**
47 * Return specific property value
49 * @param string $property_name
50 * @param mixed $default
51 * @return mixed
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;
56 } // getPropertyValue
58 /**
59 * Set value of specific property
61 * @param string $property_name
62 * @param mixed $value
63 * @param mixed $attributes Value attributes
64 * @return void
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);
71 } // if
72 $this->property_values[$property_name] = new iCalendar_PropertyValue($value, $attributes);
73 } // setPropertyValue
75 /**
76 * Add new subcomponent to this component
78 * @param iCalendar_Component $component
79 * @return null
80 * @throws iCalendar_InvalidComponentSubcomponentError
82 function addComponent(iCalendar_Component $component) {
83 if(!$this->isValidComponent($component)) {
84 throw new iCalendar_InvalidComponentSubcomponentError($this->getName(), $component->getName());
85 } // if
86 $this->components[] = $component;
87 } // addComponent
89 /**
90 * Check if specific property is valid in case of this component
92 * @param string $property_name
93 * @return boolean
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);
98 } // isValidProperty
101 * Check if $component is valid subcomponent
103 * @param iCalendar_Component $component
104 * @return boolean
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
114 * @param void
115 * @return string
117 function render() {
118 $parts = array('BEGIN:' . $this->getName());
119 //pre_var_dump($this->getName());
121 // Render properties
122 foreach($this->property_values as $property => $value) {
123 $parts[] = $value->render($property);
124 } // foreach
126 // Now render components
127 foreach($this->components as $component) {
128 $parts[] = $component->render();
129 } // foreach
131 $parts[] = 'END:' . $this->getName();
133 return implode("\r\n", $parts);
134 } // render
136 // ---------------------------------------------------
137 // Getters and setters
138 // ---------------------------------------------------
141 * Get valid_properties
143 * @param null
144 * @return array
146 function getValidProperties() {
147 return $this->valid_properties;
148 } // getValidProperties
151 * Set valid_properties value. Insert them like $this->setValidProperties('STATUS', 'CATEGORY', 'CLASS')
153 * @param null
154 * @return null
156 function setValidProperties() {
157 $value = func_get_args();
158 if(is_array($value)) {
159 foreach($value as $k => $v) {
160 $value[$k] = strtoupper($v);
161 } // foreach
162 $this->valid_properties = $value;
163 } else {
164 $this->valid_properties = array();
165 } // if
166 } // setValidProperties
169 * Get valid_components
171 * @param null
172 * @return array
174 function getValidComponents() {
175 return $this->valid_components;
176 } // getValidComponents
179 * Set valid_components value. Input them like $this->setValidComponents('VEVENT', 'VCALENDAR')
181 * @param null
182 * @return null
184 function setValidComponents() {
185 $value = func_get_args();
186 if(is_array($value)) {
187 foreach($value as $k => $v) {
188 $value[$k] = strtoupper($v);
189 } // foreach
190 $this->valid_components = $value;
191 } else {
192 $this->valid_components;
193 } // if
194 } // setValidComponents
197 * Get name
199 * @param null
200 * @return string
202 function getName() {
203 return $this->name;
204 } // getName
207 * Set name value
209 * @param string $value
210 * @return null
212 protected function setName($value) {
213 $this->name = strtoupper($value);
214 } // setName
217 * Return all property values
219 * @param void
220 * @return array
222 function getPropertyValues() {
223 return $this->property_values;
224 } // getPropertyValues
227 * Return all subcomponents
229 * @param void
230 * @return array
232 function getComponents() {
233 return $this->components;
234 } // getComponents
236 } // iCalendar_Component