-
Notifications
You must be signed in to change notification settings - Fork 503
/
YamlDataset.php
70 lines (55 loc) · 1.6 KB
/
YamlDataset.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
<?php
namespace Ushahidi\Tests\Integration\Bootstrap\Database\Dataset;
class YamlDataset extends AbstractDataset
{
protected $tables = [];
protected $parser;
public function __construct($yamlFile, $parser = null)
{
if ($parser == null) {
$parser = new SymfonyYamlParser();
}
$this->parser = $parser;
$this->addYamlFile($yamlFile);
}
public function addYamlFile($yamlFile)
{
$data = $this->parser->parseYaml($yamlFile);
foreach ($data as $tableName => $rows) {
if (!isset($rows)) {
$rows = [];
}
if (!\is_array($rows)) {
continue;
}
if (!\array_key_exists($tableName, $this->tables)) {
$columns = $this->getColumns($rows);
$tableMetaData = new DefaultTableMetadata(
$tableName,
$columns
);
$this->tables[$tableName] = new DefaultTable(
$tableMetaData
);
}
foreach ($rows as $row) {
$this->tables[$tableName]->addRow($row);
}
}
}
protected function createIterator($reverse = false)
{
return new DefaultTableIterator(
$this->tables,
$reverse
);
}
private function getColumns($rows)
{
$columns = [];
foreach ($rows as $row) {
$columns = \array_merge($columns, \array_keys($row));
}
return \array_values(\array_unique($columns));
}
}