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