1 <?php
2 /*
3 Copyright (c) 2012, University of Cambridge Computing Service
4
5 This file is part of the Lookup/Ibis client library.
6
7 This library is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /**
22 * Abstract base class for all DTO classes. This defines a couple of methods
23 * used when unmarshalling DTOs from XML.
24 *
25 * @author Dean Rasheed (dev-group@ucs.cam.ac.uk)
26 */
27 abstract class IbisDto
28 {
29 /* Properties marked as @XmlAttribte in the JAXB class */
30 protected static $xmlAttrs = array(); // Set in sub-classes
31
32 /* Properties marked as @XmlElement in the JAXB class */
33 protected static $xmlElems = array(); // Set in sub-classes
34
35 /* Properties marked as @XmlElementWrapper in the JAXB class */
36 protected static $xmlArrays = array(); // Set in sub-classes
37
38 /**
39 * @ignore
40 * Create an IbisDto from the attributes of an XML node. This just sets
41 * the properties marked as @XmlAttribute in the JAXB class.
42 *
43 * @param array $attrs The attributes on the XML node.
44 */
45 public function __construct($attrs=array())
46 {
47 foreach (static::$xmlAttrs as $attr)
48 $this->$attr = isset($attrs[$attr]) ? $attrs[$attr] : null;
49 }
50
51 /**
52 * @ignore
53 * Start element callback invoked during XML parsing when the opening
54 * tag of a child element is encountered. This creates and returns any
55 * properties marked as @XmlElementWrapper in the JAXB class, so that
56 * child collections can be populated.
57 *
58 * @param string $tagname The name of the XML wrapper element.
59 * @return IbisDto[] A reference to the array of child elements
60 * corresponding to the XML wrapper element.
61 */
62 public function &startChildElement($tagname)
63 {
64 if (in_array($tagname, static::$xmlArrays, true))
65 {
66 if (is_null($this->$tagname)) $this->$tagname = array();
67 return $this->$tagname;
68 }
69
70 // Keep PHP quiet (must return a reference to a variable)
71 $dummy = null;
72 return $dummy;
73 }
74
75 /**
76 * @ignore
77 * End element callback invoked during XML parsing when the end tag of
78 * a child element is encountered, and the tag's data is available. This
79 * sets the value of any properties marked as @XmlElement in the JAXB
80 * class.
81 *
82 * @param string $tagname The name of the XML element.
83 * @param string $data The textual value of the XML element.
84 * @return void.
85 */
86 public function endChildElement($tagname, $data)
87 {
88 if (in_array($tagname, static::$xmlElems, true))
89 $this->$tagname = $data;
90 }
91 }
92