1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soheilrt\AdobeConnectClient\Client\Converter; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Soheilrt\AdobeConnectClient\Client\Connection\ResponseInterface; |
7
|
|
|
use Soheilrt\AdobeConnectClient\Client\Helpers\StringCaseTransform as SCT; |
8
|
|
|
|
9
|
|
|
class ConverterXML implements ConverterInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
public static function convert(ResponseInterface $response): array |
15
|
|
|
{ |
16
|
|
|
$xml = simplexml_load_string($response->getBody()); |
17
|
|
|
|
18
|
|
|
if ($xml === false) { |
19
|
|
|
throw new InvalidArgumentException('The response body needs be a valid XML'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$result = []; |
23
|
|
|
|
24
|
|
|
foreach ($xml as $element) { |
25
|
|
|
// If it has attributes it's an element |
26
|
|
|
if (!empty($element->attributes())) { |
27
|
|
|
$result[$element->getName()] = static::normalize(json_decode(json_encode($element), true)); |
28
|
|
|
continue; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// if it doesn't have attributes it is a collection |
32
|
|
|
$elementName = SCT::toCamelCase($element->getName()); |
33
|
|
|
$result[$elementName] = []; |
34
|
|
|
|
35
|
|
|
foreach ($element->children() as $elementChild) { |
36
|
|
|
$result[$elementName][] = static::normalize(json_decode(json_encode($elementChild), true)); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Recursive transform the array. |
45
|
|
|
* |
46
|
|
|
* @param array $arr The array piece |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
protected static function normalize($arr): array |
51
|
|
|
{ |
52
|
|
|
$ret = []; |
53
|
|
|
|
54
|
|
|
if (isset($arr['@attributes'])) { |
55
|
|
|
$arr = array_merge($arr, $arr['@attributes']); |
56
|
|
|
unset($arr['@attributes']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($arr as $key => $value) { |
60
|
|
|
if (is_array($value)) { |
61
|
|
|
$value = static::normalize($value); |
62
|
|
|
} |
63
|
|
|
$ret[SCT::toCamelCase($key)] = $value; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $ret; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|