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

https://github.com/ewandor/horde · PHP · 192 lines · 98 code · 7 blank · 87 comment · 11 complexity · 5bb9bc2b85a3a6177ad10a846659a688 MD5 · raw file

  1. <?php
  2. /**
  3. * Handles the document root.
  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. * Handles the document root.
  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_Root
  31. extends Horde_Kolab_Format_Xml_Type_Composite
  32. {
  33. /**
  34. * Indicate which value type is expected.
  35. *
  36. * @var int
  37. */
  38. protected $value = Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY;
  39. /**
  40. * Should the velues be merged into the parent attributes?
  41. *
  42. * @var boolean
  43. */
  44. protected $merge = true;
  45. /**
  46. * Basic attributes in any Kolab object
  47. *
  48. * @var array
  49. */
  50. private $_attributes_basic = array(
  51. 'uid' => 'Horde_Kolab_Format_Xml_Type_Uid',
  52. 'body' => 'Horde_Kolab_Format_Xml_Type_String_Empty',
  53. 'categories' => 'Horde_Kolab_Format_Xml_Type_String_Empty',
  54. 'creation-date' => 'Horde_Kolab_Format_Xml_Type_CreationDate',
  55. 'last-modification-date' => 'Horde_Kolab_Format_Xml_Type_ModificationDate',
  56. 'sensitivity' => 'Horde_Kolab_Format_Xml_Type_Sensitivity',
  57. 'inline-attachment' => 'Horde_Kolab_Format_Xml_Type_Multiple_String',
  58. 'link-attachment' => 'Horde_Kolab_Format_Xml_Type_Multiple_String',
  59. 'product-id' => 'Horde_Kolab_Format_Xml_Type_ProductId',
  60. );
  61. /**
  62. * Load the node value from the Kolab object.
  63. *
  64. * @param string $name The name of the the
  65. * attribute to be fetched.
  66. * @param array &$attributes The data array that
  67. * holds all attribute
  68. * values.
  69. * @param DOMNode $parent_node The parent node of the
  70. * node to be loaded.
  71. * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
  72. * @param array $params Additiona parameters for
  73. * this parse operation.
  74. *
  75. * @return DOMNode|boolean The named DOMNode or false if no node value was
  76. * found.
  77. */
  78. public function load(
  79. $name,
  80. &$attributes,
  81. $parent_node,
  82. Horde_Kolab_Format_Xml_Helper $helper,
  83. $params = array()
  84. )
  85. {
  86. if (!($root = $helper->findNode('/' . $name))) {
  87. throw new Horde_Kolab_Format_Exception_InvalidRoot(
  88. sprintf('Missing root node "%s"!', $name)
  89. );
  90. }
  91. $attributes['_format-version'] = $root->getAttribute('version');
  92. $attributes['_api-version'] = $params['api-version'];
  93. if (!$this->isRelaxed($params)) {
  94. if (version_compare($params['expected-version'], $attributes['_format-version']) < 0) {
  95. throw new Horde_Kolab_Format_Exception_InvalidRoot(
  96. sprintf(
  97. 'Not attempting to read higher root version of %s with our version %s!',
  98. $attributes['_format-version'],
  99. $params['expected-version']
  100. )
  101. );
  102. }
  103. }
  104. $this->_prepareCompositeParameters(
  105. $params, $attributes['_format-version']
  106. );
  107. parent::load($name, $attributes, $parent_node, $helper, $params);
  108. return $root;
  109. }
  110. /**
  111. * Update the specified attribute.
  112. *
  113. * @param string $name The name of the the
  114. * attribute to be updated.
  115. * @param array $attributes The data array that holds
  116. * all attribute values.
  117. * @param DOMNode $parent_node The parent node of the
  118. * node that should be
  119. * updated.
  120. * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
  121. * @param array $params Additional parameters
  122. * for this write operation.
  123. *
  124. * @return DOMNode|boolean The new/updated child node or false if this
  125. * failed.
  126. *
  127. * @throws Horde_Kolab_Format_Exception If converting the data to XML failed.
  128. */
  129. public function save(
  130. $name,
  131. $attributes,
  132. $parent_node,
  133. Horde_Kolab_Format_Xml_Helper $helper,
  134. $params = array()
  135. )
  136. {
  137. if (!($root = $helper->findNode('/' . $name, $parent_node))) {
  138. $root = $helper->createNewNode($parent_node, $name);
  139. $root->setAttribute('version', $params['expected-version']);
  140. } else {
  141. if (!$this->isRelaxed($params)) {
  142. if (version_compare($params['expected-version'], $root->getAttribute('version')) < 0) {
  143. throw new Horde_Kolab_Format_Exception_InvalidRoot(
  144. sprintf(
  145. 'Not attempting to overwrite higher root version of %s with our version %s!',
  146. $root->getAttribute('version'),
  147. $params['expected-version']
  148. )
  149. );
  150. }
  151. }
  152. if ($params['expected-version'] != $root->getAttribute('version')) {
  153. $root->setAttribute('version', $params['expected-version']);
  154. }
  155. }
  156. $this->_prepareCompositeParameters(
  157. $params, $params['expected-version']
  158. );
  159. parent::save($name, $attributes, $parent_node, $helper, $params);
  160. return $root;
  161. }
  162. /**
  163. * Prepare the parameters for the parent composite handler.
  164. *
  165. * @param array &$params The parameters for this operation.
  166. * @param string $version The format version of the document.
  167. *
  168. * @return NULL
  169. */
  170. private function _prepareCompositeParameters(&$params, $version)
  171. {
  172. $params['format-version'] = $version;
  173. $this->elements = $this->_attributes_basic;
  174. if (isset($params['attributes-specific'])) {
  175. $this->elements = array_merge(
  176. $this->elements, $params['attributes-specific']
  177. );
  178. }
  179. if (isset($params['attributes-application'])) {
  180. $this->elements = array_merge(
  181. $this->elements, $params['attributes-application']
  182. );
  183. }
  184. }
  185. }