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