1<?php
2/**
3 * Handles auto-generated date attributes.
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 auto-generated date attributes.
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_AutomaticDate
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($name, &$attributes, $parent_node,
50                         Horde_Kolab_Format_Xml_Helper $helper,
51                         $params = array())
52    {
53        $result = parent::load($name, $attributes, $parent_node, $helper, $params);
54        if ($result !== false) {
55            return $result;
56        } else {
57            $attributes[$name] = new DateTime();
58        }
59    }
60
61    /**
62     * Load the value of a node.
63     *
64     * @param DOMNode                       $node   Retrieve value for this node.
65     * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
66     * @param array                         $params Additiona parameters for
67     *                                              this parse operation.
68     *
69     * @return mixed|null The value or null if no value was found.
70     */
71    public function loadNodeValue($node, Horde_Kolab_Format_Xml_Helper $helper,
72                                  $params = array())
73    {
74        $result = $helper->fetchNodeValue($node);
75        if ($result !== null) {
76            $date = Horde_Kolab_Format_Date::readUtcDateTime($result);
77            if ($date === false && !$this->isRelaxed($params)) {
78                throw new Horde_Kolab_Format_Exception(
79                    sprintf('Invalid date input "%s"!', $result)
80                );
81            }
82            return $date;
83        } else {
84            return $result;
85        }
86    }
87}
88