Passed
Push — master ( c4a85f...fccae8 )
by
unknown
01:31
created

LocalBusiness::addAddressDescription()   B

Complexity

Conditions 9
Paths 20

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9

Importance

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