Completed
Push — master ( 52b7dd...834530 )
by Enrico
02:14
created

AbstractModel::asTurtle()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
namespace BOTK\Model;
3
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
		'uri'				=> array(
34
								'filter'    => FILTER_SANITIZE_URL,
35
                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
36
			                   ),
37
		'base'				=> array(
38
								'default'	=> 'http://linkeddata.center/botk/resource/',
39
								'filter'    => FILTER_SANITIZE_URL,
40
                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
41
			                   ),
42
		'id'				=> array(		
43
								'filter'    => FILTER_CALLBACK,
44
		                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ID',
45
                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
46
			                   ),
47
		'page'				=> array(	
48
								'filter'    => FILTER_SANITIZE_URL,
49
                            	'flags'  	=> FILTER_FORCE_ARRAY,
50
			                   ),
51
		'homepage'			=> array(	
52
								'filter'    => FILTER_SANITIZE_URL,
53
                            	'flags'  	=> FILTER_FORCE_ARRAY,
54
			                   ),
55
	);
56
	
57
	protected $options ;
58
	protected $vocabulary = array(
59
		'rdf'		=> 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
60
		'rdfs'		=> 'http://www.w3.org/2000/01/rdf-schema#',
61
		'owl'		=> 'http://www.w3.org/2002/07/owl#',
62
		'xsd' 		=> 'http://www.w3.org/2001/XMLSchema#',
63
		'dct' 		=> 'http://purl.org/dc/terms/',
64
		'void' 		=> 'http://rdfs.org/ns/void#',
65
		'prov' 		=> 'http://www.w3.org/ns/prov#',
66
		'schema'	=> 'http://schema.org/',
67
		'wgs' 		=> 'http://www.w3.org/2003/01/geo/wgs84_pos#',
68
		'foaf' 		=> 'http://xmlns.com/foaf/0.1/',
69
		'dq'		=> 'http://purl.org/linked-data/cube#',
70
		'daq'		=> 'http://purl.org/eis/vocab/daq#',
71
		'botk' 		=> 'http://http://linkeddata.center/botk/v1#',
72
	);
73
	
74
	protected $data;
75
	protected $rdf =null; //lazy created
76
	protected $tripleCount=0; //lazy created
77
	protected $uniqueIdGenerator=null; // dependency injections
78
	protected $droppedFields = array();
79
	
80
	abstract public function asTurtle();
81
82 26
	protected function mergeOptions( array $options1, array $options2 )
83
	{
84 26
    	foreach($options2 as $property=>$option){
85
			
86 14
			$options1[$property]=isset($options1[$property])
87 2
				?array_merge($options1[$property], $option)
88 14
				:$option;
89
    	}
90
		
91 26
		return $options1;
92
	}
93
94 26
    public function __construct(array $data = array(), array $customOptions = array()) 
95
    {
96 26
		$options = $this->mergeOptions(self::$DEFAULT_OPTIONS,$customOptions);
97
		
98
		// set default values
99 26
		foreach( $options as $property=>$option){	
100 26
			if(empty($data[$property]) && isset($option['default'])){
101 26
				$data[$property] = $option['default'];
102
			}
103
		}
104
105
		// ensure data are sanitized and validated
106 26
		$sanitizedData = array_filter( filter_var_array($data, $options));
107
		
108
		// find and register dropped fields
109 26
		foreach($data as $property=>$value){
110 26
			if($value && empty($sanitizedData[$property])){
111 26
				$this->droppedFields[]=$property;
112
			}
113
		}
114
115 26
		$this->options = $options;
116 26
		$this->data = $sanitizedData;
117
		$this->setIdGenerator(function($data){return uniqid();});
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118 26
    }
119
	
120
	
121 1
	public function getDroppedFields()
122
	{
123 1
		return $this->droppedFields;
124
	}
125
126
	
127
	/**
128
	 * dependecy injection setter 
129
	 */
130 26
	public function setIdGenerator($generator)
131
	{
132 26
		assert( is_callable($generator));
133 26
		$this->uniqueIdGenerator = $generator;
134
		
135 26
		return $this;
136
	}
137
138
139
	/**
140
	 * a generic implementation that use uri, base and id property (all optionals)
141
	 */
142 8
	public function getUri()
143
	{
144 8
		if(!empty($this->data['uri'])){
145 2
			$uri =  $this->data['uri'];
146 6
		} elseif(!empty($this->data['base'])) {
147 6
			$idGenerator=$this->uniqueIdGenerator;
148 6
			$uri = $this->data['base'];
149 6
			$uri.=empty($this->data['id'])?$idGenerator($this->data):$this->data['id'];
150
		} else{
151
			$idGenerator=$this->uniqueIdGenerator;
152
			$uri = 'urn:local:botk:'.$idGenerator($this->data);
153
		}
154
		
155 8
		return $uri;
156
	}
157
		
158
159 10
	public function asArray()
160
	{
161 10
		return $this->data;
162
	}
163
164
	
165 2
	public function getOptions()
166
	{
167 2
		return $this->options;
168
	}
169
170
171 3
	public function getVocabularies()
172
	{
173 3
		return $this->vocabulary;
174
	}
175
	
176
	
177 1
	public function setVocabulary($prefix,$ns)
178
	{
179 1
		$this->vocabulary[$prefix] = $ns;
180 1
	}
181
	
182
	
183 1
	public function unsetVocabulary($prefix)
184
	{
185 1
		unset($this->vocabulary[$prefix]);
186 1
	}	
187
	
188
	
189 3
	public function getTurtleHeader($base=null)
190
	{
191 3
		$header = empty($base)?'': "@base <$base> .\n";
192 3
		foreach( $this->vocabulary as $prefix=>$ns ){
193 3
			$header.="@prefix $prefix: <$ns> .\n";
194
		}
195
		
196 3
		return $header;
197
	}
198
199
	
200 4
	public function getTripleCount()
201
	{
202
		// triple count is computed during rdf creation
203 4
		if (is_null($this->rdf)){
204 1
			$this->asTurtle();
205
		}
206
		
207 4
		return $this->tripleCount;
208
	}
209
	
210
		
211 1
	public function __toString() 
212
	{
213 1
		return $this->getTurtleHeader() ."\n". $this->asTurtle();
214
	}
215
}