php namespace ZF Net XMPP XML class Object public attrs public id publ

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
<?php
namespace ZF\Net\XMPP\XML;
class Object {
public $attrs;
public $id;
public $name;
public $value = '';
public $ns;
public $ns_prefix;
public $parent;
public $elements = array();
public function __construct($tag_name, $ns_prefix, $ns, $attrs, $parent) {
$this->name = $tag_name;
$this->attrs = $attrs;
$this->ns = $ns;
$this->ns_prefix = $ns_prefix;
$this->parent = $parent;
if (isset($this->attrs['id']))
$this->id = $this->attrs['id'];
}
public function attr($name) {
$name = strtoupper($name);
return isset($this->attrs[$name]) ? $this->attrs[$name] : NULL;
}
public function toString() {
$str = "";
$this->_toString($str);
return $str;
}
protected function _toString(&$str) {
$str .= '<'.($this->ns_prefix ? $this->ns_prefix.":" : "").$this->name;
foreach ($this->attrs as $k => $v)
$str .= ' '.$k.'="'.htmlspecialchars($v, ENT_QUOTES).'"';
if (!strlen($this->value) && !$this->elements) {
$str .= '/>';
return;
}
$str .= ">";
if (strlen($this->value) > 0)
$str .= htmlspecialchars($this->value, ENT_QUOTES);
if ($this->elements) {
foreach ($this->elements as $e)
$e->_toString($str);
}
$str .= '</'.$this->name.'>';
}
public function __toString() {
return $this->toString();
}
public function findById($id) {
foreach ($this->elements as $e) {
if ($e->id == $id)
return $e;
if ($e->elements && ($res = $e->findById($id)))
return $res;
}
return NULL;
}
public function findByName($name, $rex = false) {
$res = array();
$name = strtoupper($name);
$this->_findByField($res, "name", $name, $rex);
return $res;
}
public function findByNS($name, $rex = false) {
$res = array();
$this->_findByField($res, "ns", $name, $rex);
return $res;
}
protected function _findByField(&$res, $field, &$name, $rex = false) {
foreach ($this->elements as $e) {
if (($rex && preg_match($name, $e->$field) > 0) || (!$rex && $e->$field == $name))
$res[] = $e;
if ($e->elements)
$e->_findByField($res, $field, $name, $rex);
}
}
public function findByAttr($attr, $name, $rex = false) {
$res = array();
$this->_findByAttr($res, $attr, $name, $rex);
return $res;
}
protected function _findByAttr(&$res, &$attr, &$name, $rex = false) {
foreach ($this->elements as $e) {
if (isset($this->attrs[$attr]) && ($rex && preg_match($name, $this->attrs[$attr]) > 0) || (!$rex && $this->attrs[$attr] == $name))
$res[] = $e;
if ($e->elements)
$e->_findByAttr($res, $name);
}
}
}