|
1
|
|
|
<?php |
|
2
|
|
|
namespace BOTK\Model; |
|
3
|
|
|
|
|
4
|
|
|
use BOTK\Exceptions\DataModelException; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* An ibrid class that merge the semantic of schema:organization, schema:place and schema:geo, |
|
9
|
|
|
* it is similar to schema:LocalBusiness. |
|
10
|
|
|
* Allows the bulk setup of properties |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class AbstractModel |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
static protected $DEFAULT_OPTIONS = array ( |
|
16
|
|
|
); |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
protected $data; |
|
20
|
|
|
protected $options; |
|
21
|
|
|
protected $rdf =null; //lazy created |
|
22
|
|
|
protected $tripleCount=0; //lazy created |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public function __construct(array $data = array(), array $options = array()) |
|
26
|
|
|
{ |
|
27
|
|
|
// ensure proper defaults exists |
|
28
|
|
|
$this->options = array_merge(static::$DEFAULT_OPTIONS, $options); |
|
29
|
|
|
|
|
30
|
|
|
// set default values |
|
31
|
|
|
foreach( $this->options as $property=>$option){ |
|
32
|
|
|
if(empty($data[$property]) && isset($this->options[$property]['default'])){ |
|
33
|
|
|
$data[$property] = $this->options[$property]['default']; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// ensure data are sanitized and validated |
|
38
|
|
|
$sanitizedData = array(); |
|
39
|
|
|
foreach( $data as $property=>$value) { |
|
40
|
|
|
|
|
41
|
|
|
// property must exist |
|
42
|
|
|
if(!isset($this->options[$property])){ |
|
43
|
|
|
throw new DataModelException("Unknown property $property"); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// apply filters to not empty variables |
|
47
|
|
|
if( $value ){ |
|
48
|
|
|
if(isset($this->options[$property]['filter'])){ |
|
49
|
|
|
$sanitizedValue = filter_var($value, $this->options[$property]['filter'], $this->options[$property]); |
|
50
|
|
|
if(!$sanitizedValue){ |
|
51
|
|
|
throw new DataModelException("Invalid property value for $property ($value)"); |
|
52
|
|
|
} |
|
53
|
|
|
$sanitizedData[$property]=$sanitizedValue; |
|
54
|
|
|
} else { |
|
55
|
|
|
$sanitizedData[$property]=$value; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
$this->data = $sanitizedData; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
public function asArray() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->data; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
public function getOptions() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->options; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
abstract public function asTurtle(); |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
public function getTripleCount() |
|
79
|
|
|
{ |
|
80
|
|
|
// triple count is computed during rdf creation |
|
81
|
|
|
if (is_null($this->rdf)){ |
|
82
|
|
|
$this->asTurtle(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this->tripleCount; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
public function __toString() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->asTurtle(); |
|
92
|
|
|
} |
|
93
|
|
|
} |