1
|
|
|
<?php |
2
|
|
|
namespace BOTK; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* just a simple example class to integrate FactsFactory and a Data Model |
6
|
|
|
*/ |
7
|
|
|
class SimpleCsvGateway |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
protected $options; |
11
|
|
|
protected $factsFactory; |
12
|
|
|
protected $currentRow = 0; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
public static function factory(array $options) |
16
|
|
|
{ |
17
|
|
|
return new SimpleCsvGateway($options); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
public function __construct(array $options) |
22
|
|
|
{ |
23
|
|
|
$defaults = array( |
24
|
|
|
'factsProfile' => array(), |
25
|
|
|
'missingFactsIsError' => true, // if a missing fact must considered an error |
26
|
|
|
'bufferSize' => 2000, |
27
|
|
|
'skippFirstLine' => true, |
28
|
|
|
'fieldDelimiter' => ',', |
29
|
|
|
); |
30
|
|
|
$this->options = array_merge($defaults,$options); |
31
|
|
|
$this->factsFactory = new \BOTK\FactsFactory($options['factsProfile']); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
protected function readRawData() |
36
|
|
|
{ |
37
|
|
|
$this->currentRow++; |
38
|
|
|
return fgetcsv(STDIN, $this->options['bufferSize'], $this->options['fieldDelimiter']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
public function run() |
43
|
|
|
{ |
44
|
|
|
echo $this->factsFactory->generateLinkedDataHeader(); |
45
|
|
|
|
46
|
|
|
while ($rawdata = $this->readRawData()) { |
47
|
|
|
if($this->currentRow==1 && $this->options['skippFirstLine']){ |
48
|
|
|
echo "# Header skipped\n"; |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
try { |
52
|
|
|
$facts =$this->factsFactory->factualize($rawdata); |
53
|
|
|
echo $facts->asTurtleFragment(), "\n"; |
54
|
|
|
$droppedFields = $facts->getDroppedFields(); |
55
|
|
|
if(!empty($droppedFields) && $this->options['missingFactsIsError']) { |
56
|
|
|
echo "\n# WARNING MISSING FACT on row {$this->currentRow}: dropped ".implode(",", $droppedFields)."\n"; |
57
|
|
|
$this->factsFactory->addToCounter('error'); |
58
|
|
|
} |
59
|
|
|
} catch (\BOTK\Exception\Warning $e) { |
60
|
|
|
echo "\n# WARNING on row {$this->currentRow}: ".$e->getMessage()."\n"; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
echo $this->factsFactory->generateLinkedDataFooter(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|