1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace EngineWorks\Pivot; |
4
|
|
|
|
5
|
|
|
use SimpleXMLElement; |
6
|
|
|
|
7
|
|
|
class PivotFile |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
private $filename; |
11
|
|
|
|
12
|
|
|
/** @var Pivot */ |
13
|
|
|
private $pivot; |
14
|
|
|
|
15
|
14 |
|
public function __construct(Pivot $pivot) |
16
|
|
|
{ |
17
|
14 |
|
$this->filename = ''; |
18
|
14 |
|
$this->pivot = $pivot; |
19
|
14 |
|
} |
20
|
|
|
|
21
|
11 |
|
public function open(string $filename) |
22
|
|
|
{ |
23
|
11 |
|
if (! is_file($filename)) { |
24
|
|
|
throw new PivotException("Pivot file $filename does not exists"); |
25
|
|
|
} |
26
|
|
|
try { |
27
|
11 |
|
$xml = new SimpleXMLElement( |
28
|
|
|
file_get_contents($filename), |
29
|
11 |
|
LIBXML_NOERROR & LIBXML_NOWARNING & LIBXML_NONET, |
30
|
11 |
|
false |
31
|
|
|
); |
32
|
11 |
|
$this->fromXML($xml); |
33
|
11 |
|
$this->filename = $filename; |
34
|
|
|
} catch (\Throwable $ex) { |
35
|
|
|
throw new PivotException('Cannot open the pivot file: ' . $ex->getMessage(), 0, $ex); |
36
|
|
|
} |
37
|
11 |
|
} |
38
|
|
|
|
39
|
2 |
|
public function save() |
40
|
|
|
{ |
41
|
2 |
|
$filename = $this->getFilename(); |
42
|
2 |
|
if ('' === $filename) { |
43
|
1 |
|
throw new PivotException('Cannot save a the pivot file without a file name'); |
44
|
|
|
} |
45
|
1 |
|
$this->saveAs($filename, true); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
2 |
|
public function saveAs(string $filename, bool $override) |
49
|
|
|
{ |
50
|
2 |
|
if (file_exists($filename) && ! $override) { |
51
|
|
|
throw new PivotException('The pivot file already exists'); |
52
|
|
|
} |
53
|
|
|
try { |
54
|
2 |
|
$this->toXML()->asXML($filename); |
55
|
|
|
} catch (\Throwable $ex) { |
56
|
|
|
throw new PivotException('Cannot save the pivot file: ' . $ex->getMessage(), 0, $ex); |
57
|
|
|
} |
58
|
2 |
|
$this->filename = $filename; |
59
|
2 |
|
} |
60
|
|
|
|
61
|
1 |
|
public function getPivot() : Pivot |
62
|
|
|
{ |
63
|
1 |
|
return $this->pivot; |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
public function getFilename() : string |
67
|
|
|
{ |
68
|
5 |
|
return $this->filename; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Configure the current pivot from a SimpleXMLElement |
73
|
|
|
* @param SimpleXMLElement $xml |
74
|
|
|
*/ |
75
|
11 |
|
private function fromXML(SimpleXMLElement $xml) |
76
|
|
|
{ |
77
|
11 |
|
$this->pivot->reset(); |
78
|
11 |
|
if (isset($xml->source)) { |
79
|
11 |
|
$this->pivot->setSource((string) $xml->source); |
80
|
|
|
} |
81
|
11 |
|
if (isset($xml->sourcefield)) { |
82
|
11 |
|
foreach ($xml->sourcefield as $node) { |
83
|
11 |
|
$this->pivot->addSourceField( |
84
|
11 |
|
(string) $node['fieldname'], |
85
|
11 |
|
(string) $node['caption'], |
86
|
11 |
|
(string) $node['type'] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
11 |
|
if (isset($xml->filter)) { |
91
|
5 |
|
foreach ($xml->filter as $node) { |
92
|
5 |
|
$operator = (string) $node['operator']; |
93
|
5 |
|
if (Filter::IN === $operator) { |
94
|
5 |
|
$arguments = []; |
95
|
5 |
|
if (isset($node->argument)) { |
96
|
5 |
|
foreach ($node->argument as $argument) { |
97
|
5 |
|
$arguments[] = (string) $argument; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} else { |
101
|
4 |
|
$arguments = (isset($node['singleargument'])) ? (string) $node['singleargument'] : ''; |
102
|
|
|
} |
103
|
5 |
|
$this->pivot->addFilter((string) $node['fieldname'], $operator, $arguments); |
104
|
|
|
} |
105
|
|
|
} |
106
|
11 |
|
if (isset($xml->column)) { |
107
|
7 |
|
foreach ($xml->column as $node) { |
108
|
7 |
|
$this->pivot->addColumn((string) $node['fieldname']); |
109
|
|
|
} |
110
|
|
|
} |
111
|
11 |
|
if (isset($xml->row)) { |
112
|
9 |
|
foreach ($xml->row as $node) { |
113
|
9 |
|
$this->pivot->addRow((string) $node['fieldname']); |
114
|
|
|
} |
115
|
|
|
} |
116
|
11 |
|
if (isset($xml->aggregate)) { |
117
|
11 |
|
foreach ($xml->aggregate as $node) { |
118
|
11 |
|
$this->pivot->addAggregate( |
119
|
11 |
|
(string) $node['fieldname'], |
120
|
11 |
|
(string) $node['asname'], // this was set to an empty string |
121
|
11 |
|
(string) $node['caption'], |
122
|
11 |
|
(string) $node['group'], |
123
|
11 |
|
(string) $node['decimals'], |
124
|
11 |
|
(string) $node['order'] |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
11 |
|
if (isset($xml->info)) { |
129
|
4 |
|
foreach ($xml->info as $node) { |
130
|
4 |
|
$this->pivot->setInfo((string) $node['name'], (string) $node); |
131
|
|
|
} |
132
|
|
|
} |
133
|
11 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the pivot information as an xml node |
137
|
|
|
* @return SimpleXMLElement |
138
|
|
|
*/ |
139
|
2 |
|
private function toXML() : SimpleXMLElement |
140
|
|
|
{ |
141
|
2 |
|
$xml = new SimpleXMLElement('<' . 'pivot/>'); |
142
|
2 |
|
$xml->addChild('source', Utils::escapeXml($this->pivot->getSource())); |
143
|
2 |
|
foreach ($this->pivot->getSourceFields() as $item) { |
144
|
2 |
|
$node = $xml->addChild('sourcefield'); |
145
|
2 |
|
$node->addAttribute('fieldname', $item['fieldname']); |
146
|
2 |
|
$node->addAttribute('caption', $item['caption']); |
147
|
2 |
|
$node->addAttribute('type', $item['type']); |
148
|
|
|
} |
149
|
2 |
|
foreach ($this->pivot->getCurrentFilters() as $item) { |
150
|
1 |
|
$node = $xml->addChild('filter'); |
151
|
1 |
|
$node->addAttribute('fieldname', $item['fieldname']); |
152
|
1 |
|
$node->addAttribute('operator', $item['operator']); |
153
|
1 |
|
$arguments = $item['arguments']; |
154
|
1 |
|
if (is_array($arguments)) { |
155
|
1 |
|
foreach ($arguments as $argument) { |
156
|
1 |
|
$node->addChild('argument', Utils::escapeXml($argument)); |
157
|
|
|
} |
158
|
|
|
} else { |
159
|
1 |
|
$node->addAttribute('singleargument', (string) $arguments); |
160
|
|
|
} |
161
|
|
|
} |
162
|
2 |
|
foreach ($this->pivot->getCurrentColumns() as $item) { |
163
|
2 |
|
$xml->addChild('column')->addAttribute('fieldname', $item['fieldname']); |
164
|
|
|
} |
165
|
2 |
|
foreach ($this->pivot->getCurrentRows() as $item) { |
166
|
2 |
|
$xml->addChild('row')->addAttribute('fieldname', $item['fieldname']); |
167
|
|
|
} |
168
|
2 |
|
foreach ($this->pivot->getCurrentAggregates() as $item) { |
169
|
2 |
|
$node = $xml->addChild('aggregate'); |
170
|
2 |
|
$node->addAttribute('fieldname', $item['fieldname']); |
171
|
2 |
|
$node->addAttribute('caption', $item['caption']); |
172
|
2 |
|
$node->addAttribute('asname', $item['asname']); // this line was commented |
173
|
2 |
|
$node->addAttribute('group', $item['group']); |
174
|
2 |
|
$node->addAttribute('decimals', (string) $item['decimals']); |
175
|
2 |
|
$node->addAttribute('order', $item['order']); |
176
|
|
|
} |
177
|
|
|
// information |
178
|
2 |
|
$info = ['description', 'author', 'created']; |
179
|
2 |
|
foreach ($info as $name) { |
180
|
2 |
|
$xml->addChild('info', Utils::escapeXml($this->pivot->getInfo($name)))->addAttribute('name', $name); |
181
|
|
|
} |
182
|
2 |
|
return $xml; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|