1
|
|
|
<?php |
2
|
|
|
namespace BOTK; |
3
|
|
|
|
4
|
|
|
class FactsFactory implements FactsFactoryInterface { |
5
|
|
|
|
6
|
|
|
protected $profile; |
7
|
|
|
protected $tripleCount =0; |
8
|
|
|
protected $errors = array(); |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
public function __construct( array $profile ) |
12
|
|
|
{ |
13
|
|
|
assert(!empty($profile['model']) && class_exists('\BOTK\Model\\'.$profile['model'])); |
14
|
|
|
assert(isset($profile['datamapper']) && is_callable($profile['datamapper'])); |
15
|
|
|
assert(isset($profile['options']) && is_array($profile['options'])); |
16
|
|
|
$this->profile = $profile; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* two level filter array |
22
|
|
|
*/ |
23
|
|
|
public function removeEmpty( array $data ) |
24
|
|
|
{ |
25
|
|
|
$a = array(); |
26
|
|
|
foreach ($data as $key => $value) { |
27
|
|
|
$a[$key] = is_array($value)?array_filter($value):$value; |
28
|
|
|
} |
29
|
|
|
return array_filter($a); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function factualize( array $rawData ) |
34
|
|
|
{ |
35
|
|
|
$datamapper = $this->profile['datamapper']; |
36
|
|
|
$class = '\BOTK\Model\\'.$this->profile['model']; |
37
|
|
|
$data =$this->removeEmpty($datamapper($rawData)); |
38
|
|
|
return new $class($data,$this->profile['options']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
public function generateLinkedDataHeader() |
43
|
|
|
{ |
44
|
|
|
$class = '\BOTK\Model\\'.$this->profile['model']; |
45
|
|
|
$model = new $class(); |
46
|
|
|
return $model->getTurtleHeader(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function generateLinkedDataFooter() |
51
|
|
|
{ |
52
|
|
|
$errorCount = count($this->errors); |
53
|
|
|
return "\n#\n# Generated {$this->tripleCount} triples and $errorCount errors\n#\n"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function addToTripleCounter( $triplesCount) |
57
|
|
|
{ |
58
|
|
|
$this->tripleCount += intval($triplesCount); |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
public function getTripleCount() |
64
|
|
|
{ |
65
|
|
|
return $this->tripleCount; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function addError($error) |
70
|
|
|
{ |
71
|
|
|
$this->errors[]= (string) $error; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |