1 | <?php |
||
20 | class FactsFactory implements FactsFactoryInterface { |
||
21 | |||
22 | protected $profile; |
||
23 | protected $modelClass; |
||
24 | protected $counter = array( |
||
25 | 'triple' => 0, // rdf triples in facts |
||
26 | 'error' => 0, // facts contains error |
||
27 | 'insane' => 0, // raw data unaccepted |
||
28 | 'entity' => 0, // raw data processed |
||
29 | ); |
||
30 | |||
31 | |||
32 | 1 | public function __construct( array $profile =array() ) |
|
33 | { |
||
34 | $defaults = array( |
||
35 | 1 | 'model' => 'SampleSchemaThing', |
|
36 | 'modelOptions' => array(), |
||
37 | 1 | 'entityThreshold' => 100, // min numbers of entity that trigger error resilence computation. |
|
38 | 1 | 'resilienceToErrors' => 0.3, // if more than 30% of error throws a TooManyErrorException |
|
39 | 1 | 'resilienceToInsanes' => 0.9, // if more than 90% of unacceptable data throws a TooManyErrorException |
|
40 | 'source' => null, |
||
41 | 1 | 'datamapper' => function($rawdata){return $rawdata;}, |
|
42 | 1 | 'dataCleaner' => get_class().'::REMOVE_EMPTY', |
|
43 | 1 | 'factsErrorDetector' => get_class().'::NOT_EMPTY_FACTS', |
|
44 | 1 | 'rawdataSanitizer' => function($rawdata){return is_array($rawdata)?$rawdata:false;}, |
|
45 | ); |
||
46 | 1 | $this->profile = array_merge($defaults,$profile); |
|
47 | 1 | $this->modelClass = class_exists($this->profile['model']) |
|
48 | ?$this->profile['model'] |
||
49 | 1 | :('\BOTK\Model\\'.$this->profile['model']); |
|
50 | |||
51 | 1 | if( !class_exists($this->modelClass) || !is_subclass_of($this->modelClass, '\BOTK\ModelInterface')){ |
|
|
|||
52 | throw new \InvalidArgumentException("The provided model ({$this->profile['model']} is unknown"); |
||
53 | } |
||
54 | 1 | if( !is_callable($this->profile['datamapper'])) { |
|
55 | throw new \InvalidArgumentException("Invalid datamapper callback"); |
||
56 | } |
||
57 | 1 | if( !is_callable($this->profile['dataCleaner'])) { |
|
58 | throw new \InvalidArgumentException("Invalid dataCleaner callback"); |
||
59 | } |
||
60 | 1 | if( !is_callable($this->profile['rawdataSanitizer'])) { |
|
61 | throw new \InvalidArgumentException("Invalid rawdataSanitizer callback"); |
||
62 | } |
||
63 | 1 | if( !is_callable($this->profile['factsErrorDetector'])) { |
|
64 | throw new \InvalidArgumentException("Invalid factsErrorDetector callback"); |
||
65 | } |
||
66 | 1 | } |
|
67 | |||
68 | |||
69 | /** |
||
70 | * two level filter array, a default for dataCleaner callback |
||
71 | */ |
||
72 | 1 | public static function REMOVE_EMPTY( array $data) |
|
80 | |||
81 | /** |
||
82 | * a default for dataValidator callback |
||
83 | */ |
||
84 | 1 | public static function NOT_EMPTY_FACTS( \BOTK\ModelInterface $data) |
|
88 | |||
89 | |||
90 | /** |
||
91 | * create facts from rawdata. Please nothe that null facts does not means always an error (i.e. no facts is a fact). |
||
92 | * if you do not want empty facts use dataValidator |
||
93 | */ |
||
94 | 1 | public function factualize($rawData) |
|
134 | |||
135 | |||
136 | public function generateLinkedDataHeader() |
||
140 | |||
141 | |||
142 | public function generateLinkedDataFooter() |
||
165 | |||
166 | |||
167 | public function addToCounter($counter,$val=1) |
||
174 | |||
175 | |||
176 | public function getCounters() |
||
180 | |||
181 | } |