]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/XMPPHP/XMLObj.php
log a db error for inserting the notice
[quix0rs-gnu-social.git] / extlib / XMPPHP / XMLObj.php
1 <?php 
2 /**
3  * XMPPHP: The PHP XMPP Library
4  * Copyright (C) 2008  Nathanael C. Fritz
5  * This file is part of SleekXMPP.
6  * 
7  * XMPPHP is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  * 
12  * XMPPHP is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with XMPPHP; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  * @category   xmpphp 
22  * @package     XMPPHP
23  * @author       Nathanael C. Fritz <JID: fritzy@netflint.net>
24  * @author       Stephan Wentz <JID: stephan@jabber.wentz.it>
25  * @copyright  2008 Nathanael C. Fritz
26  */
27
28 /**
29  * XMPPHP XML Object
30  * 
31  * @category   xmpphp 
32  * @package     XMPPHP
33  * @author       Nathanael C. Fritz <JID: fritzy@netflint.net>
34  * @author       Stephan Wentz <JID: stephan@jabber.wentz.it>
35  * @copyright  2008 Nathanael C. Fritz
36  * @version     $Id$
37  */
38 class XMPPHP_XMLObj {
39         /**
40          * Tag name
41          *
42          * @var string
43          */
44         public $name;
45         
46         /**
47          * Namespace
48          *
49          * @var string
50          */
51         public $ns;
52         
53         /**
54          * Attributes
55          *
56          * @var array
57          */
58         public $attrs = array();
59         
60         /**
61          * Subs?
62          *
63          * @var array
64          */
65         public $subs = array();
66         
67         /**
68          * Node data
69          * 
70          * @var string
71          */
72         public $data = '';
73
74         /**
75          * Constructor
76          *
77          * @param string $name
78          * @param string $ns
79          * @param array  $attrs
80          * @param string $data
81          */
82         public function __construct($name, $ns = '', $attrs = array(), $data = '') {
83                 $this->name = strtolower($name);
84                 $this->ns   = $ns;
85                 if(is_array($attrs) && count($attrs)) {
86                         foreach($attrs as $key => $value) {
87                                 $this->attrs[strtolower($key)] = $value;
88                         }
89                 }
90                 $this->data = $data;
91         }
92
93         /**
94          * Dump this XML Object to output.
95          *
96          * @param integer $depth
97          */
98         public function printObj($depth = 0) {
99                 print str_repeat("\t", $depth) . $this->name . " " . $this->ns . ' ' . $this->data;
100                 print "\n";
101                 foreach($this->subs as $sub) {
102                         $sub->printObj($depth + 1);
103                 }
104         }
105
106         /**
107          * Return this XML Object in xml notation
108          *
109          * @param string $str
110          */
111         public function toString($str = '') {
112                 $str .= "<{$this->name} xmlns='{$this->ns}' ";
113                 foreach($this->attrs as $key => $value) {
114                         if($key != 'xmlns') {
115                                 $value = htmlspecialchars($value);
116                                 $str .= "$key='$value' ";
117                         }
118                 }
119                 $str .= ">";
120                 foreach($this->subs as $sub) {
121                         $str .= $sub->toString();
122                 }
123                 $body = htmlspecialchars($this->data);
124                 $str .= "$body</{$this->name}>";
125                 return $str;
126         }
127
128         /**
129          * Has this XML Object the given sub?
130          * 
131          * @param string $name
132          * @return boolean
133          */
134         public function hasSub($name) {
135                 foreach($this->subs as $sub) {
136                         if($sub->name == $name) return true;
137                 }
138                 return false;
139         }
140
141         /**
142          * Return a sub
143          *
144          * @param string $name
145          * @param string $attrs
146          * @param string $ns
147          */
148         public function sub($name, $attrs = null, $ns = null) {
149                 foreach($this->subs as $sub) {
150                         if($sub->name == $name) {
151                                 return $sub;
152                         }
153                 }
154         }
155 }