Completed
Push — master ( 0ab6e5...627489 )
by
unknown
03:10
created

LocalBusiness::asTurtleFragment()   F

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 76
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 49
CRAP Score 33.8646

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 49
cts 54
cp 0.9074
rs 2.5387
c 0
b 0
f 0
cc 33
eloc 53
nc 100663297
nop 0
crap 33.8646

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 LocalBusiness extends AbstractModel implements \BOTK\ModelInterface 
11
{
12
	protected static $DEFAULT_OPTIONS = array (
13
		'businessType'		=> array(		
14
								// additional types  as extension of schema:LocalBusiness
15
			'filter'    => FILTER_DEFAULT,
16
			'flags'  	=> FILTER_FORCE_ARRAY,
17
			),
18
		'taxID'				=> array(	
19
			'filter'    => FILTER_CALLBACK,
20
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_TOKEN',
21
			'flags'  	=> FILTER_REQUIRE_SCALAR,
22
			),
23
		'vatID'				=> array(	// italian rules
24
			'filter'    => FILTER_VALIDATE_REGEXP,
25
			'options' 	=> array('regexp'=>'/^[0-9]{11}$/'),
26
			'flags'  	=> FILTER_REQUIRE_SCALAR,
27
			),
28
		'legalName'			=> array(
29
			'filter'    => FILTER_CALLBACK,
30
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
31
			'flags'  	=> FILTER_REQUIRE_SCALAR,
32
			),
33
		'businessName'		=> array(
34
								// a schema:alternateName for schema:PostalAddress
35
			'filter'    => FILTER_DEFAULT,
36
			'flags'  	=> FILTER_FORCE_ARRAY,
37
			),
38
		'addressDescription'=> array(	//	
39
			'filter'    => FILTER_CALLBACK,
40
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
41
			'flags'  	=> FILTER_REQUIRE_SCALAR,
42
			),
43
		'addressCountry'	=> array(
44
			'default'	=> 'IT',		
45
			'filter'    => FILTER_VALIDATE_REGEXP,
46
			'options' 	=> array('regexp'=>'/^[A-Z]{2}$/'),
47
			'flags'  	=> FILTER_REQUIRE_SCALAR,
48
			),
49
		'addressLocality'	=> array(	
50
			'filter'    => FILTER_CALLBACK,
51
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
52
			'flags'  	=> FILTER_REQUIRE_SCALAR,
53
			),
54
		'addressRegion'		=> array(	
55
			'filter'    => FILTER_CALLBACK,
56
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
57
			'flags'  	=> FILTER_REQUIRE_SCALAR,
58
			),
59
		'streetAddress'		=> array(	
60
			'filter'    => FILTER_CALLBACK,
61
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ADDRESS',
62
			'flags'  	=> FILTER_REQUIRE_SCALAR,
63
			),
64
		'postalCode'		=> array(	// italian rules
65
			'filter'    => FILTER_VALIDATE_REGEXP,
66
			'options' 	=> array('regexp'=>'/^[0-9]{5}$/'),
67
			'flags'  	=> FILTER_REQUIRE_SCALAR,
68
			),
69
		'telephone'			=> array(	
70
			'filter'    => FILTER_CALLBACK,	
71
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE',
72
			'flags'  	=> FILTER_FORCE_ARRAY,
73
			),
74
		'faxNumber'			=> array(	
75
			'filter'    => FILTER_CALLBACK,
76
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE',
77
			'flags'  	=> FILTER_FORCE_ARRAY,
78
			),
79
		'email'				=> array(	
80
			'filter'    => FILTER_CALLBACK,
81
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_EMAIL',
82
			'flags'  	=> FILTER_FORCE_ARRAY,
83
			),
84
		'lat'				=> array( 
85
			'filter'    => FILTER_CALLBACK,
86
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_GEO',
87
			),
88
		'long'				=> array( 
89
			'filter'    => FILTER_CALLBACK,
90
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_GEO',
91
			),
92
		'similarStreet'		=> array(	
93
			'filter'    => FILTER_CALLBACK,
94
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
95
			'flags'  	=> FILTER_FORCE_ARRAY,
96
			),
97
		'hasMap'			=> array(	
98
			'filter'    => FILTER_CALLBACK,
99
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_HTTP_URL',
100
			'flags'  	=> FILTER_FORCE_ARRAY,
101
			),
102
		'aggregateRatingValue'	=> array(	
103
			'filter'    => FILTER_VALIDATE_FLOAT,
104
			'flags'  	=> FILTER_REQUIRE_SCALAR,
105
			),
106
		'openingHours'		   => array(	
107
			'filter'    => FILTER_DEFAULT,
108
			'flags'  	=> FILTER_REQUIRE_SCALAR,
109
			),
110
		'near'				=> array(	
111
			'filter'    => FILTER_CALLBACK,
112
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
113
			'flags'  	=> FILTER_FORCE_ARRAY,
114
			),
115
		'similarName'		=> array(	
116
			'filter'    => FILTER_CALLBACK,
117
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
118
			'flags'  	=> FILTER_FORCE_ARRAY,
119
			),
120
		'numberOfEmployees'	  => array(	
121
			'filter'    => FILTER_VALIDATE_REGEXP,
122
			'options' 	=> array('regexp'=>'/^[0-9]+\s*-?\s*[0-9]*$/'),
123
			'flags'  	=> FILTER_REQUIRE_SCALAR,
124
			)
125
126
		);
127
128
129
	/**
130
	 * Redefine protected constructor to add address description as dynamic property
131
	 */
132 13
	protected function __construct(array $data = array(), array $customOptions = array()) 
133
	{
134 13
		parent::__construct($data, $customOptions);
135 13
		$this->addAddressDescription();
136 13
	}
137
	
138
	
139
	/**
140
	 * If not existing, create an address description as a normalized address from following data properties:
141
	 * 		'addressLocality',
142
	 * 		'addressRegion',
143
	 * 		'streetAddress',
144
	 * 		'postalCode',
145
	 */
146 13
	private function addAddressDescription()
147
	{	
148 13
		extract($this->data);
149
150 13
		if(empty($addressDescription)){
151 10
			if( !empty($streetAddress) && ( !empty($addressLocality) || !empty($postalCode))){
152 4
				$addressDescription = "$streetAddress ,";
153 4
				if(!empty($postalCode)) { $addressDescription.= " $postalCode";}
154 4
				if(!empty($addressLocality)) { $addressDescription.= " $addressLocality"; }
155 4
				if(!empty($addressRegion)) { $addressDescription.= " ($addressRegion)"; }
156
			} else {
157 6
				$addressDescription = null;
158
			}
159
		}
160
		
161 13
		$addressDescription = \BOTK\Filters::FILTER_SANITIZE_ADDRESS($addressDescription);
162
		
163 13
		if(!empty($addressDescription)){
164 7
			$this->data['addressDescription']=$addressDescription;
165
		}
166 13
	}
167
	
168
	
169 4
	public function asTurtleFragment()
170
	{
171 4
		if(is_null($this->rdf)) {
172 4
			extract($this->data);
173
174
			// create uris
175 4
			$organizationUri = $this->getUri();
176 4
			$addressUri = $organizationUri.'_address';
177 4
			$numberOfEmployeesUri = $organizationUri.'_nEmployees';
178 4
			$geoUri = ( !empty($lat) && !empty($long) )?"geo:$lat,$long":null;
179
			
180 4
			$tripleCounter =0;
181 4
			$turtleString='';
182
			
183
			// define $_ as a macro to write simple rdf
184 4
			$_= function($format, $var,$sanitize=true) use(&$turtleString, &$tripleCounter){
185 4
				foreach((array)$var as $v){
186 4
					if($var){
187 4
						$turtleString.= sprintf($format,$sanitize?\BOTK\Filters::FILTER_SANITIZE_TURTLE_STRING($v):$v);
188 4
						$tripleCounter++;
189
					}
190
				}
191 4
			};
192
193
	 		// serialize schema:LocalBusiness
194 4
			$_('<%s> a schema:LocalBusiness;', $organizationUri);
195 4
			!empty($businessType) 		&& $_('a %s;', $businessType);
196 4
			!empty($id) 				&& $_('dct:identifier "%s";', $id);
197 4
			!empty($vatID) 				&& $_('schema:vatID "%s";', $vatID); 
198 4
			!empty($taxtID) 			&& $_('schema:taxtID "%s";', $taxID);
199 4
			!empty($legalName)			&& $_('schema:legalName "%s";', $legalName);
200 4
			!empty($businessName) 		&& $_('schema:alternateName "%s";', $businessName);
201 4
			!empty($telephone) 			&& $_('schema:telephone "%s";', $telephone);
202 4
			!empty($faxNumber) 			&& $_('schema:faxNumber "%s";', $faxNumber);
203 4
			!empty($openingHours)		&& $_('schema:openingHours "%s";', $openingHours);
204 4
			!empty($disambiguatingDescription)&& $_('schema:disambiguatingDescription "%s";', $disambiguatingDescription);
205 4
			!empty($aggregateRatingValue)&& $_('schema:aggregateRating [a schema:AggregateRating; schema:ratingValue "%s"^^xsd:float];', $aggregateRatingValue);
206 4
			!empty($page) 				&& $_('foaf:page <%s>;', $page,false);
207 4
			!empty($email) 				&& $_('schema:email "%s";', $email);
208 4
			!empty($homepage) 			&& $_('foaf:homepage <%s>;', $homepage,false);
209 4
			!empty($geoUri) 			&& $_('schema:geo <%s>;', $geoUri,false);
210 4
			!empty($hasMap) 			&& $_('schema:hasMap <%s>;', $hasMap,false);
211 4
			$_('schema:address <%s>. ', $addressUri);
212
			
213
			// serialize schema:PostalAddress 
214 4
			$_('<%s> a schema:PostalAddress;', $addressUri);
215 4
			!empty($addressDescription) && $_('schema:description "%s";', $addressDescription);
216 4
			!empty($streetAddress) 		&& $_('schema:streetAddress "%s";', $streetAddress);
217 4
			!empty($postalCode) 		&& $_('schema:postalCode "%s";', $postalCode);
218 4
			!empty($addressLocality) 	&& $_('schema:addressLocality "%s";', $addressLocality);
219 4
			!empty($addressRegion) 		&& $_('schema:addressRegion "%s";', $addressRegion);
220 4
			!empty($numberOfEmployees) 	&& $_('schema:numberOfEmployees <%s>;', $numberOfEmployeesUri,false);
221 4
			$_('schema:addressCountry "%s". ', $addressCountry);
222
223
			// serialize schema:GeoCoordinates
224 4
			if( !empty($geoUri)){
225 2
				$_('<%s> a schema:GeoCoordinates;', $geoUri); 
226 2
				$_('wgs:lat "%s"^^xsd:float;', $lat);
227 2
				$_('wgs:long "%s"^^xsd:float . ', $long); 
228
			}
229
			
230 4
			if(!empty($numberOfEmployees) 	&& 	preg_match('/^([0-9]+)\s*-?\s*([0-9])*$/', $numberOfEmployees, $matches)){	
231
232
				$_('<%s> a schema:QuantitativeValue;', $numberOfEmployeesUri);
233
				$minValue =  (int) $matches[1];
234
				$maxValue = empty($matches[2])? $minValue : (int) $matches[2];
235
				$_('schema:minValue %s ;', $minValue);
236
				$_('schema:maxValue %s .', $maxValue);
237
			}
238
239 4
			$this->rdf = $turtleString;
240 4
			$this->tripleCount = $tripleCounter;
241
		}
242
243 4
		return $this->rdf;
244
	}
245
246
}