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
|
|
|
protected $storage = null; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
public static function factory(array $options) |
17
|
|
|
{ |
18
|
|
|
return new static($options); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
public function __construct(array $options, object $storage = null) |
23
|
|
|
{ |
24
|
|
|
$defaults = array( |
25
|
|
|
'factsProfile' => array(), |
26
|
|
|
'missingFactsIsError' => true, // if a missing fact must considered an error |
27
|
|
|
'bufferSize' => 2000, |
28
|
|
|
'skippFirstLine' => true, |
29
|
|
|
'fieldDelimiter' => ',', |
30
|
|
|
'silent' => false, |
31
|
|
|
'inputStream' => STDIN, |
32
|
|
|
'outputStream' => STDOUT, |
33
|
|
|
); |
34
|
|
|
$this->options = array_merge($defaults,$options); |
35
|
|
|
$this->factsFactory = new \BOTK\FactsFactory($options['factsProfile']); |
36
|
|
|
$this->storage= is_null($storage)?(new \stdClass):$storage; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
protected function readRawData() |
41
|
|
|
{ |
42
|
|
|
$this->currentRow++; |
43
|
|
|
return fgetcsv($this->options['inputStream'], $this->options['bufferSize'], $this->options['fieldDelimiter']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function message($message) |
47
|
|
|
{ |
48
|
|
|
if (!$this->options['silent']){ |
49
|
|
|
fputs($this->options['outputStream'], $message); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getStorage() |
54
|
|
|
{ |
55
|
|
|
return $this->storage; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
public function setStorage($data=[]) |
60
|
|
|
{ |
61
|
|
|
$this->storage = $data; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function run() |
66
|
|
|
{ |
67
|
|
|
while ($rawdata = $this->readRawData()) { |
68
|
|
|
if($this->currentRow==1) { |
69
|
|
|
fputs($this->options['outputStream'], $this->factsFactory->generateLinkedDataHeader()); |
70
|
|
|
if ( $this->options['skippFirstLine']){ |
71
|
|
|
$this->message ("# Header skipped\n"); |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
try { |
76
|
|
|
$facts =$this->factsFactory->factualize($rawdata, $this->storage ); |
77
|
|
|
// on first line write headers |
78
|
|
|
fputs($this->options['outputStream'], $facts->asTurtleFragment() ); |
79
|
|
|
fputs($this->options['outputStream'], "\n"); |
80
|
|
|
$droppedFields = $facts->getDroppedFields(); |
81
|
|
|
if(!empty($droppedFields) && $this->options['missingFactsIsError']) { |
82
|
|
|
$this->message ("\n# WARNING MISSING FACT on row {$this->currentRow}: dropped ".implode(",", $droppedFields)."\n"); |
83
|
|
|
$this->factsFactory->addToCounter('error'); |
84
|
|
|
} |
85
|
|
|
} catch (\BOTK\Exception\Warning $e) { |
86
|
|
|
$this->message ("\n# WARNING on row {$this->currentRow}: ".$e->getMessage()."\n"); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
fputs( $this->options['outputStream'], $this->factsFactory->generateLinkedDataFooter()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|