1: <?php
2: /**
3: * Handles a integer 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 integer 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_Integer
32: extends Horde_Kolab_Format_Xml_Type_String
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: $this->_checkInteger($result, $params);
53: $result = (int)$result;
54: }
55: return $result;
56: }
57:
58:
59: /**
60: * Update the specified attribute.
61: *
62: * @param string $name The name of the attribute
63: * to be updated.
64: * @param mixed $value The value to store.
65: * @param DOMNode $parent_node The parent node of the
66: * node that should be
67: * updated.
68: * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
69: * @param array $params The parameters for this
70: * write operation.
71: * @param DOMNode|NULL $old_node The previous value (or
72: * null if there is none).
73: *
74: * @return DOMNode|boolean The new/updated child node or false if this
75: * failed.
76: *
77: * @throws Horde_Kolab_Format_Exception If converting the data to XML failed.
78: */
79: public function saveNodeValue(
80: $name,
81: $value,
82: $parent_node,
83: Horde_Kolab_Format_Xml_Helper $helper,
84: $params = array(),
85: $old_node = false
86: )
87: {
88: if (!is_string($value)) {
89: $value = (string)$value;
90: }
91: $this->_checkInteger($value, $params);
92: return parent::saveNodeValue(
93: $name, $value, $parent_node, $helper, $params, $old_node
94: );
95: }
96:
97: /**
98: * Test if the input seems to be a real integer.
99: *
100: * @param string $integer The string to check.
101: * @param array $params The parameters for this operation.
102: *
103: * @return NULL
104: *
105: * @throws Horde_Kolab_Format_Exception If the input is no integer.
106: */
107: private function _checkInteger($integer, $params)
108: {
109: if (((string)((int)$integer) !== $integer)
110: && !$this->isRelaxed($params)) {
111: throw new Horde_Kolab_Format_Exception(
112: sprintf('Invalid integer input "%s"!', $integer)
113: );
114: }
115: }
116:
117: }
118: