Document.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Sabre\VObject;
  3. /**
  4. * Document
  5. *
  6. * A document is just like a component, except that it's also the top level
  7. * element.
  8. *
  9. * Both a VCALENDAR and a VCARD are considered documents.
  10. *
  11. * This class also provides a registry for document types.
  12. *
  13. * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
  14. * @author Evert Pot (http://evertpot.com/)
  15. * @license http://sabre.io/license/ Modified BSD License
  16. */
  17. abstract class Document extends Component {
  18. /**
  19. * Unknown document type
  20. */
  21. const UNKNOWN = 1;
  22. /**
  23. * vCalendar 1.0
  24. */
  25. const VCALENDAR10 = 2;
  26. /**
  27. * iCalendar 2.0
  28. */
  29. const ICALENDAR20 = 3;
  30. /**
  31. * vCard 2.1
  32. */
  33. const VCARD21 = 4;
  34. /**
  35. * vCard 3.0
  36. */
  37. const VCARD30 = 5;
  38. /**
  39. * vCard 4.0
  40. */
  41. const VCARD40 = 6;
  42. /**
  43. * The default name for this component.
  44. *
  45. * This should be 'VCALENDAR' or 'VCARD'.
  46. *
  47. * @var string
  48. */
  49. static public $defaultName;
  50. /**
  51. * List of properties, and which classes they map to.
  52. *
  53. * @var array
  54. */
  55. static public $propertyMap = array();
  56. /**
  57. * List of components, along with which classes they map to.
  58. *
  59. * @var array
  60. */
  61. static public $componentMap = array();
  62. /**
  63. * List of value-types, and which classes they map to.
  64. *
  65. * @var array
  66. */
  67. static public $valueMap = array();
  68. /**
  69. * Creates a new document.
  70. *
  71. * We're changing the default behavior slightly here. First, we don't want
  72. * to have to specify a name (we already know it), and we want to allow
  73. * children to be specified in the first argument.
  74. *
  75. * But, the default behavior also works.
  76. *
  77. * So the two sigs:
  78. *
  79. * new Document(array $children = array(), $defaults = true);
  80. * new Document(string $name, array $children = array(), $defaults = true)
  81. *
  82. * @return void
  83. */
  84. public function __construct() {
  85. $args = func_get_args();
  86. if (count($args)===0 || is_array($args[0])) {
  87. array_unshift($args, $this, static::$defaultName);
  88. call_user_func_array(array('parent', '__construct'), $args);
  89. } else {
  90. array_unshift($args, $this);
  91. call_user_func_array(array('parent', '__construct'), $args);
  92. }
  93. }
  94. /**
  95. * Returns the current document type.
  96. *
  97. * @return void
  98. */
  99. public function getDocumentType() {
  100. return self::UNKNOWN;
  101. }
  102. /**
  103. * Creates a new component or property.
  104. *
  105. * If it's a known component, we will automatically call createComponent.
  106. * otherwise, we'll assume it's a property and call createProperty instead.
  107. *
  108. * @param string $name
  109. * @param string $arg1,... Unlimited number of args
  110. * @return mixed
  111. */
  112. public function create($name) {
  113. if (isset(static::$componentMap[strtoupper($name)])) {
  114. return call_user_func_array(array($this,'createComponent'), func_get_args());
  115. } else {
  116. return call_user_func_array(array($this,'createProperty'), func_get_args());
  117. }
  118. }
  119. /**
  120. * Creates a new component
  121. *
  122. * This method automatically searches for the correct component class, based
  123. * on its name.
  124. *
  125. * You can specify the children either in key=>value syntax, in which case
  126. * properties will automatically be created, or you can just pass a list of
  127. * Component and Property object.
  128. *
  129. * By default, a set of sensible values will be added to the component. For
  130. * an iCalendar object, this may be something like CALSCALE:GREGORIAN. To
  131. * ensure that this does not happen, set $defaults to false.
  132. *
  133. * @param string $name
  134. * @param array $children
  135. * @param bool $defaults
  136. * @return Component
  137. */
  138. public function createComponent($name, array $children = null, $defaults = true) {
  139. $name = strtoupper($name);
  140. $class = 'Sabre\\VObject\\Component';
  141. if (isset(static::$componentMap[$name])) {
  142. $class=static::$componentMap[$name];
  143. }
  144. if (is_null($children)) $children = array();
  145. return new $class($this, $name, $children, $defaults);
  146. }
  147. /**
  148. * Factory method for creating new properties
  149. *
  150. * This method automatically searches for the correct property class, based
  151. * on its name.
  152. *
  153. * You can specify the parameters either in key=>value syntax, in which case
  154. * parameters will automatically be created, or you can just pass a list of
  155. * Parameter objects.
  156. *
  157. * @param string $name
  158. * @param mixed $value
  159. * @param array $parameters
  160. * @param string $valueType Force a specific valuetype, such as URI or TEXT
  161. * @return Property
  162. */
  163. public function createProperty($name, $value = null, array $parameters = null, $valueType = null) {
  164. // If there's a . in the name, it means it's prefixed by a groupname.
  165. if (($i=strpos($name,'.'))!==false) {
  166. $group = substr($name, 0, $i);
  167. $name = strtoupper(substr($name, $i+1));
  168. } else {
  169. $name = strtoupper($name);
  170. $group = null;
  171. }
  172. $class = null;
  173. if ($valueType) {
  174. // The valueType argument comes first to figure out the correct
  175. // class.
  176. $class = $this->getClassNameForPropertyValue($valueType);
  177. }
  178. if (is_null($class) && isset($parameters['VALUE'])) {
  179. // If a VALUE parameter is supplied, we should use that.
  180. $class = $this->getClassNameForPropertyValue($parameters['VALUE']);
  181. }
  182. if (is_null($class)) {
  183. $class = $this->getClassNameForPropertyName($name);
  184. }
  185. if (is_null($parameters)) $parameters = array();
  186. return new $class($this, $name, $value, $parameters, $group);
  187. }
  188. /**
  189. * This method returns a full class-name for a value parameter.
  190. *
  191. * For instance, DTSTART may have VALUE=DATE. In that case we will look in
  192. * our valueMap table and return the appropriate class name.
  193. *
  194. * This method returns null if we don't have a specialized class.
  195. *
  196. * @param string $valueParam
  197. * @return void
  198. */
  199. public function getClassNameForPropertyValue($valueParam) {
  200. $valueParam = strtoupper($valueParam);
  201. if (isset(static::$valueMap[$valueParam])) {
  202. return static::$valueMap[$valueParam];
  203. }
  204. }
  205. /**
  206. * Returns the default class for a property name.
  207. *
  208. * @param string $propertyName
  209. * @return string
  210. */
  211. public function getClassNameForPropertyName($propertyName) {
  212. if (isset(static::$propertyMap[$propertyName])) {
  213. return static::$propertyMap[$propertyName];
  214. } else {
  215. return 'Sabre\\VObject\\Property\\Unknown';
  216. }
  217. }
  218. }