|
1
|
|
|
<?php |
|
2
|
|
|
namespace BOTK\Model; |
|
3
|
|
|
|
|
4
|
|
|
class Thing extends AbstractModel implements \BOTK\ModelInterface |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
protected static $DEFAULT_OPTIONS = array( |
|
8
|
|
|
'uri' => array( |
|
9
|
|
|
'filter' => FILTER_CALLBACK, |
|
10
|
|
|
'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
|
11
|
|
|
'flags' => FILTER_REQUIRE_SCALAR, |
|
12
|
|
|
), |
|
13
|
|
|
'base' => array( |
|
14
|
|
|
'default' => 'urn:local:', |
|
15
|
|
|
'filter' => FILTER_CALLBACK, |
|
16
|
|
|
'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
|
17
|
|
|
'flags' => FILTER_REQUIRE_SCALAR, |
|
18
|
|
|
), |
|
19
|
|
|
'id' => array( |
|
20
|
|
|
'filter' => FILTER_CALLBACK, |
|
21
|
|
|
'options' => '\BOTK\Filters::FILTER_SANITIZE_ID', |
|
22
|
|
|
'flags' => FILTER_REQUIRE_SCALAR, |
|
23
|
|
|
), |
|
24
|
|
|
'page' => array( |
|
25
|
|
|
'filter' => FILTER_CALLBACK, |
|
26
|
|
|
'options' => '\BOTK\Filters::FILTER_SANITIZE_HTTP_URL', |
|
27
|
|
|
'flags' => FILTER_FORCE_ARRAY, |
|
28
|
|
|
), |
|
29
|
|
|
'homepage' => array( |
|
30
|
|
|
'filter' => FILTER_CALLBACK, |
|
31
|
|
|
'options' => '\BOTK\Filters::FILTER_SANITIZE_HTTP_URL', |
|
32
|
|
|
'flags' => FILTER_FORCE_ARRAY, |
|
33
|
|
|
), |
|
34
|
|
|
'disambiguatingDescription'=> array( |
|
35
|
|
|
'filter' => FILTER_DEFAULT, |
|
36
|
|
|
'flags' => FILTER_FORCE_ARRAY, |
|
37
|
|
|
), |
|
38
|
|
|
'subject' => array( |
|
39
|
|
|
'filter' => FILTER_CALLBACK, |
|
40
|
|
|
'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
|
41
|
|
|
'flags' => FILTER_FORCE_ARRAY, |
|
42
|
|
|
), |
|
43
|
|
|
'image' => array( |
|
44
|
|
|
'filter' => FILTER_CALLBACK, |
|
45
|
|
|
'options' => '\BOTK\Filters::FILTER_SANITIZE_HTTP_URL', |
|
46
|
|
|
'flags' => FILTER_FORCE_ARRAY, |
|
47
|
|
|
), |
|
48
|
|
|
'sameas' => array( |
|
49
|
|
|
'filter' => FILTER_CALLBACK, |
|
50
|
|
|
'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
|
51
|
|
|
'flags' => FILTER_FORCE_ARRAY, |
|
52
|
|
|
), |
|
53
|
|
|
'name' => array( |
|
54
|
|
|
'filter' => FILTER_DEFAULT, |
|
55
|
|
|
'flags' => FILTER_FORCE_ARRAY, |
|
56
|
|
|
), |
|
57
|
|
|
'alternateName' => array( |
|
58
|
|
|
'filter' => FILTER_DEFAULT, |
|
59
|
|
|
'flags' => FILTER_FORCE_ARRAY, |
|
60
|
|
|
), |
|
61
|
|
|
'description' => array( |
|
62
|
|
|
'filter' => FILTER_DEFAULT, |
|
63
|
|
|
'flags' => FILTER_FORCE_ARRAY, |
|
64
|
|
|
), |
|
65
|
|
|
'similarName' => array( |
|
66
|
|
|
'filter' => FILTER_CALLBACK, |
|
67
|
|
|
'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
|
68
|
|
|
'flags' => FILTER_FORCE_ARRAY |
|
69
|
|
|
), |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* a generic implementation that use uri, base and id property (all optionals) |
|
75
|
|
|
*/ |
|
76
|
14 |
|
public function getUri() |
|
77
|
|
|
{ |
|
78
|
14 |
|
if(!empty($this->data['uri'])){ |
|
79
|
6 |
|
$uri = $this->data['uri']; |
|
80
|
8 |
|
} elseif(!empty($this->data['base'])) { |
|
81
|
8 |
|
$idGenerator=$this->uniqueIdGenerator; |
|
82
|
8 |
|
$uri = $this->data['base']; |
|
83
|
8 |
|
$uri.=empty($this->data['id'])?$idGenerator($this->data):$this->data['id']; |
|
84
|
|
|
} else{ |
|
85
|
|
|
$idGenerator=$this->uniqueIdGenerator; |
|
86
|
|
|
$uri = 'urn:local:botk:'.$idGenerator($this->data); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
14 |
|
return $uri; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
10 |
|
public function asTurtleFragment() |
|
94
|
|
|
{ |
|
95
|
10 |
|
if(is_null($this->rdf)) { |
|
96
|
10 |
|
$uri = $this->getUri(); |
|
97
|
|
|
|
|
98
|
|
|
// define $_ as a macro to write simple rdf |
|
99
|
10 |
|
$_= function($format, $var,$sanitize=true) use(&$turtleString, &$tripleCounter){ |
|
100
|
8 |
|
foreach((array)$var as $v){ |
|
101
|
8 |
|
if($var){ |
|
102
|
8 |
|
$turtleString.= sprintf($format,$sanitize?\BOTK\Filters::FILTER_SANITIZE_TURTLE_STRING($v):$v); |
|
103
|
8 |
|
$tripleCounter++; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
10 |
|
}; |
|
107
|
|
|
|
|
108
|
|
|
// serialize uri properies |
|
109
|
|
|
$tripleCounter =0; |
|
|
|
|
|
|
110
|
|
|
$turtleString="<$uri> "; |
|
|
|
|
|
|
111
|
|
|
foreach (array( |
|
112
|
|
|
'page' => 'foaf:page', |
|
113
|
|
|
'homepage' => 'foaf:homepage', |
|
114
|
|
|
'subject' => 'skos:subject', |
|
115
|
|
|
'image' => 'schema:image', |
|
116
|
|
|
'sameAs' => 'owl:sameAs', |
|
117
|
|
|
) as $uriVar=>$property) { |
|
118
|
|
|
if(!empty($this->data[$uriVar])){ |
|
119
|
|
|
$_("$property <%s>;", $this->data[$uriVar],false); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// serialize string properies |
|
124
|
|
|
foreach(array( |
|
125
|
|
|
'id' => 'dct:identifier', |
|
126
|
|
|
'disambiguatingDescription' => 'schema:disambiguatingDescription', |
|
127
|
|
|
'name' => 'schema:name', |
|
128
|
|
|
'alternateName' => 'schema:alternateName', |
|
129
|
|
|
'description' => 'schema:description', |
|
130
|
|
|
) as $stringVar=>$property) { |
|
131
|
|
|
if(!empty($this->data[$stringVar])){ |
|
132
|
|
|
$_("$property \"%s\";", $this->data[$stringVar]); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if($tripleCounter){ |
|
137
|
|
|
$this->rdf = substr($turtleString, 0, -1).'.'; |
|
138
|
|
|
$this->tripleCount = $tripleCounter; |
|
139
|
|
|
} else { |
|
140
|
|
|
$this->rdf = ''; // no serialize if uri has no attributes |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this->rdf; |
|
146
|
|
|
} |
|
147
|
|
|
} |
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope