|
1
|
|
|
<?php |
|
2
|
|
|
namespace EWW\Dpf\Services\Api; |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of the TYPO3 CMS project. |
|
6
|
|
|
* |
|
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
9
|
|
|
* of the License, or any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please read the |
|
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
13
|
|
|
* |
|
14
|
|
|
* The TYPO3 project - inspiring people to share! |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
use EWW\Dpf\Domain\Model\Document; |
|
18
|
|
|
|
|
19
|
|
|
class DocumentToJsonMapper |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $mapping; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \DOMXpath |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $xpath; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getMapping(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->mapping; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $mapping |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setMapping(string $mapping): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->mapping = $mapping; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Gets a json representation of the given document |
|
49
|
|
|
* |
|
50
|
|
|
* @param Document $document |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getJson(Document $document) |
|
54
|
|
|
{ |
|
55
|
|
|
$internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData()); |
|
56
|
|
|
$this->xpath = $internalFormat->getXpath(); |
|
57
|
|
|
$mapping = json_decode($this->getMapping(), true); |
|
58
|
|
|
$data = $this->crawl($mapping); |
|
59
|
|
|
return json_encode($data); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Crawls the given configuration data (an array representation of json data) and creates the document-json by |
|
64
|
|
|
* using the mapping information inside this configuration. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $elements |
|
67
|
|
|
* @param \DOMNode $parentNode |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function crawl($elements, \DOMNode $parentNode = null) |
|
71
|
|
|
{ |
|
72
|
|
|
$branch = []; |
|
73
|
|
|
|
|
74
|
|
|
foreach ($elements as $index => $items) { |
|
75
|
|
|
|
|
76
|
|
|
if ($index == "_mapping") { |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (array_key_exists(0, $items)) { |
|
81
|
|
|
$items = $items[0]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$mapping = $items["_mapping"]; |
|
85
|
|
|
|
|
86
|
|
|
if (is_array($items) && sizeof($items) - (array_key_exists("_mapping", $items)? 1 : 0) > 0) { |
|
87
|
|
|
|
|
88
|
|
|
if (empty($parentNode) || $parentNode instanceof \DOMElement) { |
|
89
|
|
|
|
|
90
|
|
|
$nodes = $this->xpath->query($mapping, $parentNode); |
|
91
|
|
|
|
|
92
|
|
|
if ($nodes->length == 1) { |
|
93
|
|
|
$branch[$index] = $this->crawl($items, $nodes->item(0)); |
|
94
|
|
|
} else { |
|
95
|
|
|
foreach ($nodes as $node) { |
|
96
|
|
|
$branch[$index][] = $this->crawl($items, $node); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} else { |
|
102
|
|
|
if (empty($parentNode) || $parentNode instanceof \DOMElement) { |
|
103
|
|
|
|
|
104
|
|
|
$nodes = $this->xpath->query($mapping, $parentNode); |
|
105
|
|
|
|
|
106
|
|
|
if ($nodes->length == 1) { |
|
107
|
|
|
$branch[$index] = trim($nodes->item(0)->nodeValue); |
|
108
|
|
|
} else { |
|
109
|
|
|
foreach ($nodes as $k => $node) { |
|
110
|
|
|
$branch[$index][] = trim($node->nodeValue); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $branch; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|