1: <?php
2: /**
3: * Handles a string attribute.
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 a string attribute.
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_String
32: extends Horde_Kolab_Format_Xml_Type_Base
33: {
34: /**
35: * Load the node value from the Kolab object.
36: *
37: * @param string $name The name of the the
38: * attribute to be fetched.
39: * @param array &$attributes The data array that
40: * holds all attribute
41: * values.
42: * @param DOMNode $parent_node The parent node of the
43: * node to be loaded.
44: * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
45: * @param array $params Additiona parameters for
46: * this parse operation.
47: *
48: * @return DOMNode|boolean The named DOMNode or false if no node value was
49: * found.
50: */
51: public function load(
52: $name,
53: &$attributes,
54: $parent_node,
55: Horde_Kolab_Format_Xml_Helper $helper,
56: $params = array()
57: )
58: {
59: $result = parent::load($name, $attributes, $parent_node, $helper, $params);
60: if ($result !== false) {
61: return $result;
62: } else {
63: if ($this->value != Horde_Kolab_Format_Xml::VALUE_MAYBE_MISSING) {
64: $attributes[$name] = $this->loadMissing($name, $params);
65: }
66: return false;
67: }
68: }
69:
70: /**
71: * Load the value of a node.
72: *
73: * @param DOMNode $node Retrieve value for this node.
74: * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
75: * @param array $params Additiona parameters for
76: * this parse operation.
77: *
78: * @return mixed|null The value or null if no value was found.
79: */
80: public function loadNodeValue(
81: $node,
82: Horde_Kolab_Format_Xml_Helper $helper,
83: $params = array()
84: )
85: {
86: $result = $helper->fetchNodeValue($node);
87: if ($result === null) {
88: $result = '';
89: }
90: return $result;
91: }
92:
93: /**
94: * Update the specified attribute.
95: *
96: * @param string $name The name of the the
97: * attribute to be updated.
98: * @param array $attributes The data array that holds
99: * all attribute values.
100: * @param DOMNode $parent_node The parent node of the
101: * node that should be
102: * updated.
103: * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
104: * @param array $params Additional parameters
105: * for this write operation.
106: *
107: * @return DOMNode|boolean The new/updated child node or false if this
108: * failed.
109: *
110: * @throws Horde_Kolab_Format_Exception If converting the data to XML failed.
111: */
112: public function save(
113: $name,
114: $attributes,
115: $parent_node,
116: Horde_Kolab_Format_Xml_Helper $helper,
117: $params = array()
118: )
119: {
120: $node = $helper->findNodeRelativeTo(
121: './' . $name, $parent_node
122: );
123:
124: if (!isset($attributes[$name])) {
125: if ($node === false) {
126: if ($this->value == Horde_Kolab_Format_Xml::VALUE_MAYBE_MISSING ||
127: ($this->value == Horde_Kolab_Format_Xml::VALUE_NOT_EMPTY &&
128: $this->isRelaxed($params))) {
129: return false;
130: }
131: } else {
132: if ($this->value == Horde_Kolab_Format_Xml::VALUE_MAYBE_MISSING) {
133: /** Client indicates that the value should get removed */
134: $helper->removeNodes($parent_node, $name);
135: return false;
136: } else {
137: return $node;
138: }
139: }
140: }
141:
142: return $this->saveNodeValue(
143: $name,
144: $this->generateWriteValue($name, $attributes, $params),
145: $parent_node,
146: $helper,
147: $params,
148: $node
149: );
150: }
151:
152: /**
153: * Generate the value that should be written to the node. Override in the
154: * extending classes.
155: *
156: * @param string $name The name of the the attribute
157: * to be updated.
158: * @param array $attributes The data array that holds all
159: * attribute values.
160: * @param array $params The parameters for this write operation.
161: *
162: * @return mixed The value to be written.
163: */
164: protected function generateWriteValue($name, $attributes, $params)
165: {
166: if (isset($attributes[$name])) {
167: return $attributes[$name];
168: } else {
169: return $this->loadMissing($name, $params);
170: }
171: }
172: }
173: