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