forked from yiisoft/yii2-httpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlParser.php
46 lines (42 loc) · 1.09 KB
/
XmlParser.php
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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\httpclient;
use yii\base\Object;
/**
* XmlParser parses HTTP message content as XML.
*
* @author Paul Klimov <[email protected]>
* @since 2.0
*/
class XmlParser extends Object implements ParserInterface
{
/**
* @inheritdoc
*/
public function parse(Response $response)
{
return $this->convertXmlToArray($response->getContent());
}
/**
* Converts XML document to array.
* @param string|\SimpleXMLElement $xml xml to process.
* @return array XML array representation.
*/
protected function convertXmlToArray($xml)
{
if (!is_object($xml)) {
$xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
}
$result = (array) $xml;
foreach ($result as $key => $value) {
if (is_object($value)) {
$result[$key] = $this->convertXmlToArray($value);
}
}
return $result;
}
}