Completed
Push — master ( 66c020...88a4bc )
by Enrico
01:49
created

Product::asTurtleFragment()   C

Complexity

Conditions 15
Paths 2

Size

Total Lines 72
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 15

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 14
cts 14
cp 1
rs 5.4849
c 0
b 0
f 0
cc 15
eloc 41
nc 2
nop 0
crap 15

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace BOTK\Model;
3
4
5
/**
6
 * An ibrid class that merge the semantic of schema:organization, schema:place and schema:geo, 
7
 * it is similar to schema:LocalBusiness.
8
 * Allows the bulk setup of properties
9
 */
10
class Product extends Thing 
11
{
12
13
	protected static $DEFAULT_OPTIONS = array (
14
        'brand' => array(	
15
			'filter'    => FILTER_CALLBACK,
16
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
17
			),
18
        'category' => array(
19
			'filter'    => FILTER_DEFAULT,
20
			'flags'  	=> FILTER_FORCE_ARRAY,
21
			),
22
        'color' => array(
23
			'filter'    => FILTER_DEFAULT,
24
			'flags'  	=> FILTER_FORCE_ARRAY,
25
			),
26
        'depth' => array(
27
			'filter'    => FILTER_CALLBACK,
28
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_RANGE',
29
			'flags'  	=> FILTER_REQUIRE_SCALAR
30
			),
31
        'gtin13' => array(
32
			'filter'    => FILTER_CALLBACK,
33
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_GTIN13',
34
			'flags'  	=> FILTER_FORCE_ARRAY,
35
			),
36
        'gtin8' => array(
37
			'filter'    => FILTER_CALLBACK,
38
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_GTIN8',
39
			'flags'  	=> FILTER_FORCE_ARRAY,
40
			),
41
        'height' => array(
42
			'filter'    => FILTER_CALLBACK,
43
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_RANGE',
44
			'flags'  	=> FILTER_FORCE_ARRAY,
45
			),
46
        'isAccessoryOrSparePartFor' => array(	
47
			'filter'    => FILTER_CALLBACK,
48
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
49
			'flags'  	=> FILTER_FORCE_ARRAY,
50
			),
51
        'isConsumableFor' => array(	
52
			'filter'    => FILTER_CALLBACK,
53
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
54
			'flags'  	=> FILTER_FORCE_ARRAY,
55
			),
56
        'isRelatedTo' => array(	
57
			'filter'    => FILTER_CALLBACK,
58
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
59
			'flags'  	=> FILTER_FORCE_ARRAY,
60
			),
61
        'isSimilarTo' => array(	
62
			'filter'    => FILTER_CALLBACK,
63
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
64
			'flags'  	=> FILTER_FORCE_ARRAY,
65
			),
66
        'itemCondition' => array(
67
			'filter'    => FILTER_CALLBACK,
68
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
69
			'flags'  	=> FILTER_REQUIRE_SCALAR
70
			),
71
        'manufacturer' => array(	
72
			'filter'    => FILTER_CALLBACK,
73
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
74
			'flags'  	=> FILTER_REQUIRE_SCALAR,
75
			),
76
        'material' => array(	
77
			'filter'    => FILTER_CALLBACK,
78
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
79
			'flags'  	=> FILTER_REQUIRE_SCALAR,
80
			),
81
        'mpn' => array(
82
			'filter'    => FILTER_DEFAULT,
83
			'flags'  	=> FILTER_FORCE_ARRAY,
84
			),
85
        'productionDate' => array(
86
			'filter'    => FILTER_CALLBACK,
87
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_DATETIME',
88
			'flags'  	=> FILTER_REQUIRE_SCALAR,
89
			), 
90
        'purchaseDate' => array(
91
			'filter'    => FILTER_CALLBACK,
92
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_DATETIME',
93
			'flags'  	=> FILTER_REQUIRE_SCALAR,
94
			), 
95
        'releaseDate' => array(
96
			'filter'    => FILTER_CALLBACK,
97
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_DATETIME',
98
			'flags'  	=> FILTER_REQUIRE_SCALAR,
99
			), 
100
        'review' => array(
101
			'filter'    => FILTER_DEFAULT,
102
			'flags'  	=> FILTER_FORCE_ARRAY,
103
			),
104
        'sku' => array(
105
			'filter'    => FILTER_DEFAULT,
106
			'flags'  	=> FILTER_FORCE_ARRAY,
107
			),
108
        'weight' => array(
109
			'filter'    => FILTER_CALLBACK,
110
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_RANGE',
111
			'flags'  	=> FILTER_REQUIRE_SCALAR
112
			),
113
        'width' => array(
114
			'filter'    => FILTER_CALLBACK,
115
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_RANGE',
116
			'flags'  	=> FILTER_REQUIRE_SCALAR
117
			),
118
		);
119
120
	
121 2
	public function asTurtleFragment()
122
	{
123 2
		static $uriVars= array( 'brand','isAccessoryOrSparePartFor','isConsumableFor','isRelatedTo','isSimilarTo','itemCondition', 'manufacturer','material');
124 2
		static $stringVars= array('category','color','gtin13','gtin8','mpn','review','sku');
125 2
		static $dateVars= array('productionDate','purchaseDate','releaseDate');
126
		
127 2
		if(is_null($this->rdf)) {
128
			
129 2
			$productUri = $this->getUri();
130 2
			$turtleString=parent::asTurtleFragment();
131 2
			$tripleCounter = $this->tripleCount;
132
			
133
			// define $_ as a macro to write simple rdf
134 2
			$_= function($format, $var,$sanitize=true) use(&$turtleString, &$tripleCounter){
135 1
				foreach((array)$var as $v){
136 1
					if($var){
137 1
						$turtleString.= sprintf($format,$sanitize?\BOTK\Filters::FILTER_SANITIZE_TURTLE_STRING($v):$v);
138 1
						$tripleCounter++;
139
					}
140
				}
141 2
			};
142
143
	 		// serialize uri properies
144
			$turtleString.="<$productUri> ";
145
			foreach ($uriVars as $uriVar) {
146
				if(!empty($this->data[$uriVar])){
147
					$_("schema:$uriVar <%s>;", $this->data[$uriVar],false);	
148
				}
149
			}
150
			
151
			// serialize string properies		
152
			foreach ($stringVars as $stringVar) {
153
				if(!empty($this->data[$stringVar])){
154
					$_("schema:$stringVar \"%s\";", $this->data[$stringVar]);	
155
				}
156
			}
157
			
158
			// serialize date properies
159
			foreach ($dateVars as $dateVar ) {
160
				if(!empty($this->data[$stringVar])){
161
					$_("schema:$dateVar \"%s\"^^xsd:dateTime;", $this->data[$dateVar]);	
162
				}
163
			}
164
			
165
			$turtleString.='a schema:Product.';
166
			$tripleCounter++;	
167
			
168
			// serialize quantitative values		
169
			$statVars = array('depth','height','weight','width' );
170
			foreach ( $statVars as $statVar) {
171
				if(!empty($this->data[$statVar])&& ($range=\BOTK\Filters::PARSE_QUANTITATIVE_VALUE($this->data[$statVar])) ){
172
					list($min,$max)=$range;
173
					$base = $this->data['base'];
174
					if( $min===$max){
175
						$statUri =  "{$base}{$min}";			
176
						$turtleString.= "<$productUri> schema:$statVar <$statUri>.<$statUri> schema:value $min .";	
177
						$tripleCounter +=3;					
178
					} else {
179
						$statUri =  "{$base}{$min}to{$max}";			
180
						$turtleString.= "<$productUri> schema:$statVar <$statUri>.<$statUri> schema:minValue $min ;schema:maxValue $max .";	
181
						$tripleCounter +=4;											
182
					}
183
				}		
184
			}
185
186
			$this->rdf = $turtleString;
187
			$this->tripleCount = $tripleCounter;
188
		}
189
190
		return $this->rdf;
191
192
	}
193
194
}