1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: namespace DedicatedApi\Xmlrpc;
16:
17: class Message
18: {
19: public $message;
20: public $messageType;
21: public $faultCode;
22: public $faultString;
23: public $methodName;
24: public $params;
25:
26: protected $arrayStructs = array();
27: protected $arrayStructsTypes = array();
28: protected $currentStructName = array();
29: protected $param;
30: protected $value;
31: protected $currentTag;
32: protected $currentTagContents;
33:
34: protected $parser;
35:
36: function __construct ($message)
37: {
38: $this->message = $message;
39: }
40:
41: function parse()
42: {
43:
44:
45: $this->message = preg_replace('/<\?xml(.*)?\?'.'>/', '', $this->message);
46: if (trim($this->message) == '')
47: {
48: return false;
49: }
50: $this->parser = xml_parser_create();
51:
52: xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
53:
54: xml_set_object($this->parser, $this);
55: xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
56: xml_set_character_data_handler($this->parser, 'cdata');
57: if (!xml_parse($this->parser, $this->message))
58: {
59: 60: 61:
62: return false;
63: }
64: xml_parser_free($this->parser);
65:
66: if ($this->messageType == 'fault')
67: {
68: $this->faultCode = $this->params[0]['faultCode'];
69: $this->faultString = $this->params[0]['faultString'];
70: }
71: return true;
72: }
73:
74: function tag_open($parser, $tag, $attr)
75: {
76: $this->currentTag = $tag;
77: switch ($tag)
78: {
79: case 'methodCall':
80: case 'methodResponse':
81: case 'fault':
82: $this->messageType = $tag;
83: break;
84:
85: case 'data':
86: $this->arrayStructsTypes[] = 'array';
87: $this->arrayStructs[] = array();
88: break;
89: case 'struct':
90: $this->arrayStructsTypes[] = 'struct';
91: $this->arrayStructs[] = array();
92: break;
93: }
94: }
95:
96: function cdata($parser, $cdata)
97: {
98: $this->currentTagContents .= $cdata;
99: }
100:
101: function tag_close($parser, $tag)
102: {
103: $valueFlag = false;
104: switch ($tag)
105: {
106: case 'int':
107: case 'i4':
108: $value = (int)trim($this->currentTagContents);
109: $this->currentTagContents = '';
110: $valueFlag = true;
111: break;
112: case 'double':
113: $value = (double)trim($this->currentTagContents);
114: $this->currentTagContents = '';
115: $valueFlag = true;
116: break;
117: case 'string':
118: $value = (string)trim($this->currentTagContents);
119: $this->currentTagContents = '';
120: $valueFlag = true;
121: break;
122: case 'dateTime.iso8601':
123: $value = new Date(trim($this->currentTagContents));
124:
125: $this->currentTagContents = '';
126: $valueFlag = true;
127: break;
128: case 'value':
129:
130: if (trim($this->currentTagContents) != '') {
131: $value = (string)$this->currentTagContents;
132: $this->currentTagContents = '';
133: $valueFlag = true;
134: }
135: break;
136: case 'boolean':
137: $value = (boolean)trim($this->currentTagContents);
138: $this->currentTagContents = '';
139: $valueFlag = true;
140: break;
141: case 'base64':
142: $value = base64_decode($this->currentTagContents);
143: $this->currentTagContents = '';
144: $valueFlag = true;
145: break;
146:
147: case 'data':
148: case 'struct':
149: $value = array_pop($this->arrayStructs);
150: array_pop($this->arrayStructsTypes);
151: $valueFlag = true;
152: break;
153: case 'member':
154: array_pop($this->currentStructName);
155: break;
156: case 'name':
157: $this->currentStructName[] = trim($this->currentTagContents);
158: $this->currentTagContents = '';
159: break;
160: case 'methodName':
161: $this->methodName = trim($this->currentTagContents);
162: $this->currentTagContents = '';
163: break;
164: }
165:
166: if ($valueFlag)
167: {
168: 169: 170: 171: 172:
173: if (count($this->arrayStructs) > 0)
174: {
175:
176: if ($this->arrayStructsTypes[count($this->arrayStructsTypes)-1] == 'struct')
177: {
178:
179: $this->arrayStructs[count($this->arrayStructs)-1][$this->currentStructName[count($this->currentStructName)-1]] = $value;
180: }
181: else
182: {
183:
184: $this->arrayStructs[count($this->arrayStructs)-1][] = $value;
185: }
186: }
187: else
188: {
189:
190: $this->params[] = $value;
191: }
192: }
193: }
194: }
195:
196: ?>