/
AnyDataset.php
360 lines (323 loc) · 9.54 KB
/
AnyDataset.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
namespace ByJG\AnyDataset\Core;
use ByJG\AnyDataset\Core\Exception\DatabaseException;
use ByJG\AnyDataset\Core\Formatter\XmlFormatter;
use ByJG\Util\XmlUtil;
use InvalidArgumentException;
/**
* AnyDataset is a simple way to store data using only XML file.
* Your structure is hierarquical and each "row" contains "fields" but these structure can vary for each row.
* Anydataset files have extension ".anydata.xml" and have many classes to put and get data into anydataset xml file.
* Anydataset class just read and write files. To search elements you need use AnyIterator
* and IteratorFilter. Each row have a class Row.
* XML Structure
* <code>
* <anydataset>
* <row>
* <field name="fieldname1">value of fieldname 1</field>
* <field name="fieldname2">value of fieldname 2</field>
* <field name="fieldname3">value of fieldname 3</field>
* </row>
* <row>
* <field name="fieldname1">value of fieldname 1</field>
* <field name="fieldname4">value of fieldname 4</field>
* </row>
* </anydataset>
* </code>
* How to use:
* <code>
* $any = new AnyDataset();
* </code>
*
*@see Row
* @see AnyIterator
* @see IteratorFilter
*/
class AnyDataset
{
/**
* Internal structure represent the current Row
*
* @var Row[]
*/
private $collection;
/**
* Current node anydataset works
* @var int
*/
private $currentRow;
/**
* Path to anydataset file
* @var string|null
*/
private $filename;
/**
* @param null|string $filename
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
public function __construct($filename = null)
{
$this->collection = array();
$this->currentRow = -1;
$this->filename = null;
$this->defineSavePath($filename, function () {
if (!is_null($this->filename)) {
$this->createFrom($this->filename);
}
});
}
/**
* @return string|null
*/
public function getFilename()
{
return $this->filename;
}
/**
*
* @param string|null $file
* @param mixed $closure
* @return void
*/
private function defineSavePath($file, $closure)
{
if (!is_null($file)) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if (empty($ext) && substr($file, 0, 6) !== "php://") {
$file .= '.anydata.xml';
}
$this->filename = $file;
}
$closure();
}
/**
* Private method used to read and populate anydataset class from specified file
*
* @param string $filepath Path and Filename to be read
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
private function createFrom($filepath)
{
if (file_exists($filepath)) {
$anyDataSet = XmlUtil::createXmlDocumentFromFile($filepath);
$this->collection = array();
$rows = $anyDataSet->getElementsByTagName("row");
foreach ($rows as $row) {
$sr = new Row();
$fields = $row->getElementsByTagName("field");
foreach ($fields as $field) {
$attr = $field->attributes->getNamedItem("name");
if (is_null($attr) || is_null($attr->nodeValue)) {
throw new InvalidArgumentException('Malformed anydataset file ' . basename($filepath));
}
$sr->addField($attr->nodeValue, $field->nodeValue);
}
$sr->acceptChanges();
$this->collection[] = $sr;
}
$this->currentRow = count($this->collection) - 1;
}
}
/**
* Returns the AnyDataset XML representative structure.
*
* @return string XML String
* @throws \ByJG\Util\Exception\XmlUtilException
*/
public function xml()
{
return (new XmlFormatter($this->getIterator()))->toText();
}
/**
* @param string|null $filename
* @return void
* @throws DatabaseException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
public function save($filename = null)
{
$this->defineSavePath($filename, function () {
if (is_null($this->filename)) {
throw new DatabaseException("No such file path to save anydataset");
}
(new XmlFormatter($this->getIterator()))->saveToFile($this->filename);
});
}
/**
* Append one row to AnyDataset.
*
* @param Row|array|\stdClass|object|null $singleRow
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function appendRow($singleRow = [])
{
if (!empty($singleRow)) {
if ($singleRow instanceof Row) {
$this->collection[] = $singleRow;
$singleRow->acceptChanges();
} elseif (is_array($singleRow)) {
$this->collection[] = new Row($singleRow);
} else {
throw new InvalidArgumentException("You must pass an array or a Row object");
}
} else {
$singleRow = new Row();
$this->collection[] = $singleRow;
$singleRow->acceptChanges();
}
$this->currentRow = count($this->collection) - 1;
}
/**
* Enter description here...
*
* @param GenericIterator $iterator
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function import($iterator)
{
foreach ($iterator as $singleRow) {
$this->appendRow($singleRow);
}
}
/**
* Insert one row before specified position.
*
* @param int $rowNumber
* @param Row|array|\stdClass|object $row
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function insertRowBefore($rowNumber, $row)
{
if ($rowNumber > count($this->collection)) {
$this->appendRow($row);
} else {
$singleRow = $row;
if (!($row instanceof Row)) {
$singleRow = new Row($row);
}
/**
* @psalm-suppress InvalidPropertyAssignmentValue
*/
array_splice($this->collection, $rowNumber, 0, '');
/**
* @psalm-suppress InvalidPropertyAssignmentValue
*/
$this->collection[$rowNumber] = $singleRow;
}
}
/**
*
* @param mixed $row
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function removeRow($row = null)
{
if (is_null($row)) {
$row = $this->currentRow;
}
if ($row instanceof Row) {
$iPos = 0;
foreach ($this->collection as $sr) {
if ($sr->toArray() == $row->toArray()) {
$this->removeRow($iPos);
break;
}
$iPos++;
}
return;
}
if ($row == 0) {
$this->collection = array_slice($this->collection, 1);
} else {
$this->collection = array_slice($this->collection, 0, $row) + array_slice($this->collection, $row);
}
}
/**
* Add a single string field to an existing row
*
* @param string $name - Field name
* @param string $value - Field value
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function addField($name, $value)
{
if ($this->currentRow < 0) {
$this->appendRow();
}
$this->collection[$this->currentRow]->addField($name, $value);
}
/**
* Get an Iterator filtered by an IteratorFilter
* @param IteratorFilter $itf
* @return GenericIterator
*/
public function getIterator(IteratorFilter $itf = null)
{
if (is_null($itf)) {
return new AnyIterator($this->collection);
}
return new AnyIterator($itf->match($this->collection));
}
/**
* Undocumented function
*
* @param string $fieldName
* @param IteratorFilter $itf
* @return array
*/
public function getArray($fieldName, $itf = null)
{
$iterator = $this->getIterator($itf);
$result = array();
foreach ($iterator as $singleRow) {
$result[] = $singleRow->get($fieldName);
}
return $result;
}
/**
*
* @param string $field
* @return void
*/
public function sort($field)
{
if (count($this->collection) == 0) {
return;
}
$this->collection = $this->quickSortExec($this->collection, $field);
}
/**
* @param Row[] $seq
* @param string $field
* @return array
*/
protected function quickSortExec($seq, $field)
{
if (!count($seq)) {
return $seq;
}
$key = $seq[0];
$left = $right = array();
$cntSeq = count($seq);
for ($i = 1; $i < $cntSeq; $i ++) {
if ($seq[$i]->get($field) <= $key->get($field)) {
$left[] = $seq[$i];
} else {
$right[] = $seq[$i];
}
}
return array_merge(
$this->quickSortExec($left, $field),
[ $key ],
$this->quickSortExec($right, $field)
);
}
}