1 | <?php |
||
4 | abstract class AbstractModel |
||
5 | { |
||
6 | |||
7 | /** |
||
8 | * |
||
9 | * MUST be redefined by concrete class with the model schema |
||
10 | * Each array element is composed by a propery name and and property options. |
||
11 | * Property option is an array with following (optional) fields: |
||
12 | * 'default' a value to be used for the propery |
||
13 | * 'filter' a php filter |
||
14 | * 'options' php filter options |
||
15 | * 'flags' php filter flags |
||
16 | * |
||
17 | * Example:array ( |
||
18 | * 'legalName' => array( |
||
19 | * 'filter' => FILTER_CALLBACK, |
||
20 | * 'options' => '\BOTK\Filters::FILTER_NORMALIZZE_ADDRESS', |
||
21 | * ), |
||
22 | * 'alternateName' => array( |
||
23 | 'flags' => FILTER_FORCE_ARRAY, |
||
24 | * ), |
||
25 | * 'postalCode' => array( // italian rules |
||
26 | * 'filter' => FILTER_VALIDATE_REGEXP, |
||
27 | * 'options' => array('regexp'=>'/^[0-9]{5}$/'), |
||
28 | * 'flags' => FILTER_REQUIRE_SCALAR, |
||
29 | * ), |
||
30 | * ) |
||
31 | */ |
||
32 | protected static $DEFAULT_OPTIONS = array( |
||
33 | 'base' => array( |
||
34 | 'default' => 'urn:resource:', |
||
35 | 'filter' => FILTER_CALLBACK, |
||
36 | 'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
||
37 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
38 | ), |
||
39 | ); |
||
40 | |||
41 | |||
42 | /** |
||
43 | * known vocabularies |
||
44 | */ |
||
45 | protected static $VOCABULARY = array( |
||
46 | 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', |
||
47 | 'xsd' => 'http://www.w3.org/2001/XMLSchema#', |
||
48 | ); |
||
49 | |||
50 | |||
51 | protected $options ; |
||
52 | |||
53 | protected $data; |
||
54 | protected $rdf =null; //lazy created |
||
55 | protected $tripleCount=0; //lazy created |
||
56 | protected $uniqueIdGenerator=null; // dependency injections |
||
57 | protected $droppedFields = array(); |
||
58 | |||
59 | |||
60 | 9 | protected static function mergeOptions( array $options1, array $options2 ) |
|
71 | |||
72 | |||
73 | 9 | protected static function constructOptions() |
|
83 | |||
84 | |||
85 | /** |
||
86 | * Do not call directlty constructor, use fromArray or other factory methodsinstead |
||
87 | */ |
||
88 | 9 | protected function __construct(array $data = array(), array $customOptions = array()) |
|
114 | |||
115 | |||
116 | |||
117 | /** |
||
118 | * Create an instance from an associative array |
||
119 | */ |
||
120 | 9 | public static function fromArray(array $data, array $customOptions = array()) |
|
124 | |||
125 | |||
126 | /** |
||
127 | * Create an instance from an generic standard object |
||
128 | */ |
||
129 | 1 | public static function fromStdObject( \stdClass $obj, array $customOptions = array()) |
|
133 | |||
134 | |||
135 | 4 | public static function getVocabularies() |
|
145 | |||
146 | |||
147 | 3 | public static function getTurtleHeader($base=null) |
|
157 | |||
158 | |||
159 | public function getDroppedFields() |
||
163 | |||
164 | |||
165 | /** |
||
166 | * dependecy injection setter |
||
167 | */ |
||
168 | 9 | public function setIdGenerator($generator) |
|
175 | |||
176 | |||
177 | /** |
||
178 | * returns an uri |
||
179 | */ |
||
180 | 3 | public function getUri($id=null) |
|
181 | { |
||
182 | 3 | assert(!empty($this->data['base'])) ; |
|
183 | |||
184 | 3 | if (empty($id)) { |
|
185 | 3 | $idGenerator=$this->uniqueIdGenerator; |
|
186 | 3 | $id=$idGenerator($this->data); |
|
187 | } |
||
188 | |||
189 | 3 | return $this->data['base'] . $id; |
|
190 | } |
||
191 | |||
192 | |||
193 | 1 | public function getOptions() |
|
197 | |||
198 | |||
199 | 1 | public function getTripleCount() |
|
200 | { |
||
201 | // triple count is computed during rdf creation |
||
202 | 1 | if (!empty($this->data) && is_null($this->rdf)){ |
|
203 | 1 | $this->asTurtleFragment(); |
|
204 | } |
||
205 | |||
206 | 1 | return $this->tripleCount; |
|
207 | } |
||
208 | |||
209 | |||
210 | 2 | public function asArray() |
|
214 | |||
215 | |||
216 | |||
217 | 1 | public function asStdObject() |
|
221 | |||
222 | |||
223 | /** |
||
224 | * metadata not yet implemented |
||
225 | */ |
||
226 | 1 | public function asLinkedData() |
|
230 | |||
231 | |||
232 | 1 | public function asString() |
|
236 | |||
237 | |||
238 | 1 | public function __toString() |
|
242 | |||
243 | |||
244 | /** |
||
245 | * adds a turtle fragment managing cardinality > 1 |
||
246 | */ |
||
247 | 1 | protected function addFragment($format, $var, $sanitize=true){ |
|
248 | 1 | foreach((array)$var as $v){ |
|
249 | 1 | if($var){ |
|
250 | 1 | $this->rdf .= sprintf($format, $sanitize?\BOTK\Filters::FILTER_SANITIZE_TURTLE_STRING($v):$v); |
|
251 | 1 | $this->tripleCount++; |
|
252 | } |
||
253 | } |
||
254 | 1 | } |
|
255 | |||
256 | /** |
||
257 | * this must be implemented |
||
258 | */ |
||
259 | abstract public function asTurtleFragment(); |
||
260 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.