/framework/Kolab_Format/lib/Horde/Kolab/Format/Xml/Type/Base.php

https://github.com/ewandor/horde · PHP · 305 lines · 114 code · 15 blank · 176 comment · 15 complexity · fbe82c552b0d7ceb55058bfe10a0fdd2 MD5 · raw file

  1. <?php
  2. /**
  3. * Utilities for the various XML handlers.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Kolab
  8. * @package Kolab_Format
  9. * @author Gunnar Wrobel <wrobel@pardus.de>
  10. * @license http://www.horde.org/licenses/lgpl21 LGPL
  11. * @link http://www.horde.org/libraries/Horde_Kolab_Format
  12. */
  13. /**
  14. * Utilities for the various XML handlers.
  15. *
  16. * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
  17. *
  18. * See the enclosed file COPYING for license information (LGPL). If you did not
  19. * receive this file, see
  20. * http://www.horde.org/licenses/lgpl21.
  21. *
  22. * @since Horde_Kolab_Format 1.1.0
  23. *
  24. * @category Kolab
  25. * @package Kolab_Format
  26. * @author Gunnar Wrobel <wrobel@pardus.de>
  27. * @license http://www.horde.org/licenses/lgpl21 LGPL
  28. * @link http://www.horde.org/libraries/Horde_Kolab_Format
  29. */
  30. class Horde_Kolab_Format_Xml_Type_Base
  31. {
  32. /**
  33. * The factory for any additional objects required.
  34. *
  35. * @var Horde_Kolab_Format_Factory
  36. */
  37. private $_factory;
  38. /**
  39. * Indicate which value type is expected.
  40. *
  41. * @var int
  42. */
  43. protected $value = Horde_Kolab_Format_Xml::VALUE_MAYBE_MISSING;
  44. /**
  45. * A default value if required.
  46. *
  47. * @var string
  48. */
  49. protected $default;
  50. /**
  51. * Collects xml types already created.
  52. *
  53. * @var array
  54. */
  55. static private $_xml_types;
  56. /**
  57. * Constructor
  58. *
  59. * @param Horde_Kolab_Format_Factory $factory The factory for any additional
  60. * objects required.
  61. */
  62. public function __construct($factory)
  63. {
  64. $this->_factory = $factory;
  65. }
  66. /**
  67. * Load the node value from the Kolab object.
  68. *
  69. * @param string $name The name of the the
  70. * attribute to be fetched.
  71. * @param array &$attributes The data array that
  72. * holds all attribute
  73. * values.
  74. * @param DOMNode $parent_node The parent node of the
  75. * node to be loaded.
  76. * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
  77. * @param array $params Additiona parameters for
  78. * this parse operation.
  79. *
  80. * @return DOMNode|boolean The named DOMNode or false if no node value was
  81. * found.
  82. */
  83. public function load($name, &$attributes, $parent_node,
  84. Horde_Kolab_Format_Xml_Helper $helper,
  85. $params = array())
  86. {
  87. if ($node = $helper->findNodeRelativeTo('./' . $name, $parent_node)) {
  88. if (($value = $this->loadNodeValue($node, $helper, $params)) !== null) {
  89. $attributes[$name] = $value;
  90. return $node;
  91. }
  92. }
  93. return false;
  94. }
  95. /**
  96. * Load the value of a node.
  97. *
  98. * @param DOMNode $node Retrieve value for this node.
  99. * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
  100. * @param array $params Additiona parameters for
  101. * this parse operation.
  102. *
  103. * @return mixed|null The value or null if no value was found.
  104. */
  105. public function loadNodeValue($node, Horde_Kolab_Format_Xml_Helper $helper,
  106. $params = array())
  107. {
  108. return $helper->fetchNodeValue($node);
  109. }
  110. /**
  111. * Load a default value for a node.
  112. *
  113. * @param string $name The attribute name.
  114. * @param array $params The parameters for the current operation.
  115. *
  116. * @return mixed The default value.
  117. *
  118. * @throws Horde_Kolab_Format_Exception In case the attribute may not be
  119. * missing or the default value was
  120. * left undefined.
  121. */
  122. protected function loadMissing($name, $params)
  123. {
  124. if ($this->value == Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY
  125. && !$this->isRelaxed($params)) {
  126. throw new Horde_Kolab_Format_Exception_MissingValue($name);
  127. }
  128. if ($this->value == Horde_Kolab_Format_Xml::VALUE_DEFAULT) {
  129. return $this->default;
  130. }
  131. }
  132. /**
  133. * Update the specified attribute.
  134. *
  135. * @param string $name The name of the the
  136. * attribute to be updated.
  137. * @param array $attributes The data array that holds
  138. * all attribute values.
  139. * @param DOMNode $parent_node The parent node of the
  140. * node that should be
  141. * updated.
  142. * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
  143. * @param array $params Additional parameters
  144. * for this write operation.
  145. *
  146. * @return DOMNode|boolean The new/updated child node or false if this
  147. * failed.
  148. *
  149. * @throws Horde_Kolab_Format_Exception If converting the data to XML failed.
  150. */
  151. public function save($name, $attributes, $parent_node,
  152. Horde_Kolab_Format_Xml_Helper $helper,
  153. $params = array())
  154. {
  155. $node = $helper->findNodeRelativeTo(
  156. './' . $name, $parent_node
  157. );
  158. $result = $this->saveNodeValue(
  159. $name,
  160. $this->generateWriteValue($name, $attributes, $params),
  161. $parent_node,
  162. $helper,
  163. $params,
  164. $node
  165. );
  166. return ($node !== false) ? $node : $result;
  167. }
  168. /**
  169. * Generate the value that should be written to the node. Override in the
  170. * extending classes.
  171. *
  172. * @param string $name The name of the the attribute
  173. * to be updated.
  174. * @param array $attributes The data array that holds all
  175. * attribute values.
  176. * @param array $params The parameters for this write operation.
  177. *
  178. * @return mixed The value to be written.
  179. */
  180. protected function generateWriteValue($name, $attributes, $params)
  181. {
  182. if (isset($attributes[$name])) {
  183. return $attributes[$name];
  184. } else {
  185. return '';
  186. }
  187. }
  188. /**
  189. * Update the specified attribute.
  190. *
  191. * @param string $name The name of the attribute
  192. * to be updated.
  193. * @param mixed $value The value to store.
  194. * @param DOMNode $parent_node The parent node of the
  195. * node that should be
  196. * updated.
  197. * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
  198. * @param array $params The parameters for this
  199. * write operation.
  200. * @param DOMNode|NULL $old_node The previous value (or
  201. * null if there is none).
  202. *
  203. * @return DOMNode|boolean The new/updated child node or false if this
  204. * failed.
  205. *
  206. * @throws Horde_Kolab_Format_Exception If converting the data to XML failed.
  207. */
  208. public function saveNodeValue(
  209. $name,
  210. $value,
  211. $parent_node,
  212. Horde_Kolab_Format_Xml_Helper $helper,
  213. $params = array(),
  214. $old_node = false
  215. )
  216. {
  217. if ($old_node === false) {
  218. return $helper->storeNewNodeValue(
  219. $parent_node, $name, $value
  220. );
  221. } else {
  222. $helper->replaceFirstNodeTextValue($old_node, $value);
  223. return $old_node;
  224. }
  225. }
  226. /**
  227. * Validate that the parameter array contains all required parameters.
  228. *
  229. * @param string $key The parameter name.
  230. * @param array $params The parameters.
  231. * @param string $attribute The attribute name.
  232. *
  233. * @throws Horde_Kolab_Format_Exception In case required parameters are
  234. * missing.
  235. */
  236. protected function checkMissing($key, $params, $attribute)
  237. {
  238. if (!isset($params[$key])) {
  239. throw new Horde_Kolab_Format_Exception(
  240. sprintf(
  241. 'Required parameter "%s" missing (attribute: %s)!',
  242. $key,
  243. $attribute
  244. )
  245. );
  246. }
  247. }
  248. /**
  249. * Return a parameter value.
  250. *
  251. * @param string $name The parameter name.
  252. *
  253. * @return mixed The parameter value.
  254. */
  255. public function getParam($name)
  256. {
  257. return isset($this->_params[$name]) ? $this->_params[$name] : null;
  258. }
  259. /**
  260. * Returns if the XML handling should be relaxed.
  261. *
  262. * @param array $params The parameters.
  263. *
  264. * @return boolean True if the XML should not be strict.
  265. */
  266. protected function isRelaxed($params)
  267. {
  268. return !empty($params['relaxed']);
  269. }
  270. /**
  271. * Create a handler for the sub type of this attribute.
  272. *
  273. * @param string $type The sub type.
  274. * @param array $params Additional parameters.
  275. *
  276. * @return Horde_Kolab_Format_Xml_Type The sub type handler.
  277. */
  278. protected function createSubType($type, $params)
  279. {
  280. if (isset($params['api-version'])) {
  281. $class = $type . '_V' . $params['api-version'];
  282. } else {
  283. $class = $type;
  284. }
  285. if (!isset(self::$_xml_types[$class])) {
  286. self::$_xml_types[$class] = $this->_factory->createXmlType($type, $params);
  287. }
  288. return self::$_xml_types[$class];
  289. }
  290. }