Completed
Push — master ( b4cb56...2f2064 )
by Enrico
01:36
created

Thing   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 132
ccs 22
cts 24
cp 0.9167
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getUri() 0 15 4
C asTurtleFragment() 0 42 7
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
	 		// serialize uri properies
99 10
			$this->rdf = "<$uri> ";
100
			foreach (array(
101 10
				'page' 			=> 'foaf:page',
102
				'homepage'		=> 'foaf:homepage',
103
				'subject'		=> 'skos:subject',
104
				'image'			=> 'schema:image',
105
				'sameAs'		=> 'owl:sameAs',	
106
			) as $uriVar=>$property) {
107 10
				if(!empty($this->data[$uriVar])){
108 10
					$this->addFragment("$property <%s>;", $this->data[$uriVar],false);	
109
				}
110
			}
111
			
112
			// serialize string properies
113
			foreach(array(
114 10
				'id'						=> 'dct:identifier',
115
				'disambiguatingDescription'	=> 'schema:disambiguatingDescription',
116
				'name'						=> 'schema:name',
117
				'alternateName'				=> 'schema:alternateName',
118
				'description'				=> 'schema:description',
119
			) as $stringVar=>$property) {
120 10
				if(!empty($this->data[$stringVar])){
121 10
					$this->addFragment("$property \"%s\";", $this->data[$stringVar]);	
122
				}
123
			}
124
			
125 10
			if($this->tripleCount){
126 8
				$this->rdf = substr($this->rdf, 0, -1).'.';
127
			} else {
128 2
				$this->rdf = ''; // no serialize if uri has no attributes
129
			}
130
			
131
		}
132
133 10
		return $this->rdf;
134
	}
135
}