|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PEIP\Translator; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the PEIP package. |
|
7
|
|
|
* (c) 2009-2016 Timo Michna <timomichna/yahoo.de> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* XMLArrayTranslator |
|
15
|
|
|
* Abstract base class for transformers. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Timo Michna <timomichna/yahoo.de> |
|
18
|
|
|
* @extends PEIP\Pipe\Pipe |
|
19
|
|
|
* @implements \PEIP\INF\Transformer\Transformer, \PEIP\INF\Event\Connectable, \PEIP\INF\Channel\SubscribableChannel, \PEIP\INF\Channel\Channel, \PEIP\INF\Handler\Handler, \PEIP\INF\Message\MessageBuilder |
|
20
|
|
|
*/ |
|
21
|
|
|
class XMLArrayTranslator |
|
22
|
|
|
{ |
|
23
|
|
|
public static function translate($content) |
|
24
|
|
|
{ |
|
25
|
|
|
try { |
|
26
|
|
|
$node = simplexml_load_string($content); |
|
27
|
|
|
// fix for hhvm |
|
28
|
|
|
if (!($node instanceof \SimpleXMLElement)) { |
|
29
|
|
|
throw new \Exception('loading XML failed'); |
|
30
|
|
|
} |
|
31
|
|
|
} catch (\Exception $e) { |
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return self::doTranslate($node); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected static function doTranslate(\SimpleXMLElement $node) |
|
39
|
|
|
{ |
|
40
|
|
|
$array = []; |
|
41
|
|
|
$array['type'] = $node['type'] |
|
42
|
|
|
? (string) $node['type'] |
|
43
|
|
|
: (string) $node->getName(); |
|
44
|
|
|
$value = (string) $node; |
|
45
|
|
|
if ($value != '') { |
|
46
|
|
|
$array['value'] = $value; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
foreach ($node->attributes() as $name => $value) { |
|
50
|
|
|
$array[$name] = (string) $value; |
|
51
|
|
|
} |
|
52
|
|
|
foreach ($node->children() as $nr => $child) { |
|
53
|
|
|
$name = $child->getName(); |
|
54
|
|
|
$res = self::doTranslate($child); |
|
55
|
|
|
|
|
56
|
|
|
if (isset($array[$name])) { |
|
57
|
|
|
if (is_string($array[$name])) { |
|
58
|
|
|
$array[$name] = [ |
|
59
|
|
|
[ |
|
60
|
|
|
'type' => $name, |
|
61
|
|
|
'value' => $array[$name], |
|
62
|
|
|
], |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
} else { |
|
66
|
|
|
$array[$name] = []; |
|
67
|
|
|
} |
|
68
|
|
|
$array[$name][] = $res; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $array; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|