1: <?php
2: /**
3: * Handles a string attribute that may also be missing.
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 that may also be missing.
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_MaybeMissing
32: extends Horde_Kolab_Format_Xml_Type_Base
33: {
34: /**
35: * Load the value of a node.
36: *
37: * @param DOMNode $node Retrieve value for this node.
38: * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
39: * @param array $params Additiona parameters for
40: * this parse operation.
41: *
42: * @return mixed|null The value or null if no value was found.
43: */
44: public function loadNodeValue(
45: $node,
46: Horde_Kolab_Format_Xml_Helper $helper,
47: $params = array()
48: )
49: {
50: $result = $helper->fetchNodeValue($node);
51: if ($result === null) {
52: $result = '';
53: }
54: return $result;
55: }
56:
57: /**
58: * Update the specified attribute.
59: *
60: * @param string $name The name of the the
61: * attribute to be updated.
62: * @param array $attributes The data array that holds
63: * all attribute values.
64: * @param DOMNode $parent_node The parent node of the
65: * node that should be
66: * updated.
67: * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
68: * @param array $params Additional parameters
69: * for this write operation.
70: *
71: * @return DOMNode|boolean The new/updated child node or false if this
72: * failed.
73: *
74: * @throws Horde_Kolab_Format_Exception If converting the data to XML failed.
75: */
76: public function save(
77: $name,
78: $attributes,
79: $parent_node,
80: Horde_Kolab_Format_Xml_Helper $helper,
81: $params = array()
82: )
83: {
84: $node = $helper->findNodeRelativeTo(
85: './' . $name, $parent_node
86: );
87:
88: if (!isset($attributes[$name])) {
89: if ($node !== false) {
90: /** Client indicates that the value should get removed */
91: $helper->removeNodes($parent_node, $name);
92: }
93: return false;
94: }
95:
96: return $this->saveNodeValue(
97: $name,
98: $this->generateWriteValue($name, $attributes, $params),
99: $parent_node,
100: $helper,
101: $params,
102: $node
103: );
104: }
105:
106: /**
107: * Generate the value that should be written to the node. Override in the
108: * extending classes.
109: *
110: * @param string $name The name of the the attribute
111: * to be updated.
112: * @param array $attributes The data array that holds all
113: * attribute values.
114: * @param array $params The parameters for this write operation.
115: *
116: * @return mixed The value to be written.
117: */
118: protected function generateWriteValue($name, $attributes, $params)
119: {
120: return $attributes[$name];
121: }
122: }
123: