Completed
Push — master ( 1450b1...d28de4 )
by Enrico
02:24
created

AbstractModel::setVocabulary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 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 static $VOCABULARY  = array(
58
		'rdf'		=> 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
59
		'rdfs'		=> 'http://www.w3.org/2000/01/rdf-schema#',
60
		'owl'		=> 'http://www.w3.org/2002/07/owl#',
61
		'xsd' 		=> 'http://www.w3.org/2001/XMLSchema#',
62
		'dct' 		=> 'http://purl.org/dc/terms/',
63
		'void' 		=> 'http://rdfs.org/ns/void#',
64
		'prov' 		=> 'http://www.w3.org/ns/prov#',
65
		'schema'	=> 'http://schema.org/',
66
		'wgs' 		=> 'http://www.w3.org/2003/01/geo/wgs84_pos#',
67
		'foaf' 		=> 'http://xmlns.com/foaf/0.1/',
68
		'dq'		=> 'http://purl.org/linked-data/cube#',
69
		'daq'		=> 'http://purl.org/eis/vocab/daq#',
70
	);
71
	
72
	protected $options ;
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
83 24
	protected function mergeOptions( array $options1, array $options2 )
84
	{
85 24
    	foreach($options2 as $property=>$option){
86
			
87 24
			$options1[$property]=isset($options1[$property])
88 12
				?array_merge($options1[$property], $option)
89 24
				:$option;
90
    	}
91
		
92 24
		return $options1;
93
	}
94
95
96 24
    public function __construct(array $data = array(), array $customOptions = array()) 
97
    {  			
98 24
    	$baseOptions = $this->mergeOptions(self::$DEFAULT_OPTIONS,static::$DEFAULT_OPTIONS);
99 24
		$options = $this->mergeOptions($baseOptions,$customOptions);
100
		
101
		// set default values
102 24
		foreach( $options as $property=>$option){	
103 24
			if(empty($data[$property]) && isset($option['default'])){
104 24
				$data[$property] = $option['default'];
105
			}
106
		}
107
108
		// ensure data are sanitized and validated
109 24
		$sanitizedData = array_filter( filter_var_array($data, $options));
110
		
111
		// find and register dropped fields
112 24
		foreach($data as $property=>$value){
113 24
			if($value && empty($sanitizedData[$property])){
114 24
				$this->droppedFields[]=$property;
115
			}
116
		}
117
118 24
		$this->options = $options;
119 24
		$this->data = $sanitizedData;
120
		$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...
121 24
    }
122
	
123
124
125 4
	public static function getVocabularies()
126
	{
127 4
		return array_merge(self::$VOCABULARY, static::$VOCABULARY);
128
	}
129
130
	
131 3
	public static function getTurtleHeader($base=null)
132
	{
133 3
		$vocabulariers = static::getVocabularies();
134 3
		$header = empty($base)?'': "@base <$base> .\n";
135 3
		foreach( $vocabulariers as $prefix=>$ns ){
136 3
			$header.="@prefix $prefix: <$ns> .\n";
137
		}
138
		
139 3
		return $header;
140
	}
141
142
	
143 1
	public function getDroppedFields()
144
	{
145 1
		return $this->droppedFields;
146
	}
147
148
	
149
	/**
150
	 * dependecy injection setter 
151
	 */
152 24
	public function setIdGenerator($generator)
153
	{
154 24
		assert( is_callable($generator));
155 24
		$this->uniqueIdGenerator = $generator;
156
		
157 24
		return $this;
158
	}
159
160
161
	/**
162
	 * a generic implementation that use uri, base and id property (all optionals)
163
	 */
164 8
	public function getUri()
165
	{
166 8
		if(!empty($this->data['uri'])){
167 2
			$uri =  $this->data['uri'];
168 6
		} elseif(!empty($this->data['base'])) {
169 6
			$idGenerator=$this->uniqueIdGenerator;
170 6
			$uri = $this->data['base'];
171 6
			$uri.=empty($this->data['id'])?$idGenerator($this->data):$this->data['id'];
172
		} else{
173
			$idGenerator=$this->uniqueIdGenerator;
174
			$uri = 'urn:local:botk:'.$idGenerator($this->data);
175
		}
176
		
177 8
		return $uri;
178
	}
179
		
180
181 10
	public function asArray()
182
	{
183 10
		return $this->data;
184
	}
185
186
	
187 2
	public function getOptions()
188
	{
189 2
		return $this->options;
190
	}
191
192
	
193 4
	public function getTripleCount()
194
	{
195
		// triple count is computed during rdf creation
196 4
		if (is_null($this->rdf)){
197 1
			$this->asTurtle();
198
		}
199
		
200 4
		return $this->tripleCount;
201
	}
202
	
203
		
204 1
	public function __toString() 
205
	{
206 1
		return $this->getTurtleHeader() ."\n". $this->asTurtle();
207
	}
208
}