Completed
Push — master ( 081071...48c049 )
by Enrico
01:58
created

LocalBusiness::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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
	);
93
94
95
	/**
96
	 * Redefine protected constructor to add address description as dynamic property
97
	 */
98 13
	protected function __construct(array $data = array(), array $customOptions = array()) 
99
    {
100 13
    	parent::__construct($data, $customOptions);
101 13
		$this->addAddressDescription();
102 13
	}
103
	
104
	
105
	/**
106
	 * If not existing, create an address description as a normalized address from following data properties:
107
	 * 		'addressLocality',
108
	 * 		'addressRegion',
109
	 * 		'streetAddress',
110
	 * 		'postalCode',
111
	 */
112 13
	private function addAddressDescription()
113
	{	
114 13
		extract($this->data);
115
116 13
		if(empty($addressDescription)){
117 10
			if( !empty($streetAddress) && ( !empty($addressLocality) || !empty($postalCode))){
118 4
				$addressDescription = "$streetAddress ,";
119 4
				if(!empty($postalCode)) { $addressDescription.= " $postalCode";}
120 4
				if(!empty($addressLocality)) { $addressDescription.= " $addressLocality"; }
121 4
				if(!empty($addressRegion)) { $addressDescription.= " ($addressRegion)"; }
122
			} else {
123 6
				$addressDescription = null;
124
			}
125
		}
126
		
127 13
		$addressDescription = \BOTK\Filters::FILTER_SANITIZE_ADDRESS($addressDescription);
128
		
129 13
		if(!empty($addressDescription)){
130 7
			$this->data['addressDescription']=$addressDescription;
131
		}
132 13
	}
133
	
134
	
135 4
	public function asTurtleFragment()
136
	{
137 4
		if(is_null($this->rdf)) {
138 4
			extract($this->data);
139
140
			// create uris
141 4
			$organizationUri = $this->getUri();
142 4
			$addressUri = $organizationUri.'_address';
143 4
			$geoUri = ( !empty($lat) && !empty($long) )?"geo:$lat,$long":null;
144
			
145 4
			$tripleCounter =0;
146 4
			$turtleString='';
147
			
148
			// define $_ as a macro to write simple rdf
149 4
			$_= function($format, $var,$sanitize=true) use(&$turtleString, &$tripleCounter){
150 4
				foreach((array)$var as $v){
151 4
					if($var){
152 4
						$turtleString.= sprintf($format,$sanitize?\BOTK\Filters::FILTER_SANITIZE_TURTLE_STRING($v):$v);
153 4
						$tripleCounter++;
154
					}
155
				}
156 4
			};
157
				
158
	 		// serialize schema:LocalBusiness
159 4
 			$_('<%s> a schema:LocalBusiness;', $organizationUri);
160 4
			!empty($businessType) 		&& $_('a %s;', $businessType);
161 4
			!empty($id) 				&& $_('dct:identifier "%s";', $id);
162 4
			!empty($vatID) 				&& $_('schema:vatID "%s";', $vatID); 
163 4
			!empty($taxtID) 			&& $_('schema:taxtID "%s";', $taxID);
164 4
			!empty($legalName)			&& $_('schema:legalName "%s";', $legalName);
165 4
			!empty($businessName) 		&& $_('schema:alternateName "%s";', $businessName);
166 4
			!empty($telephone) 			&& $_('schema:telephone "%s";', $telephone);
167 4
			!empty($faxNumber) 			&& $_('schema:faxNumber "%s";', $faxNumber);
168 4
			!empty($page) 				&& $_('schema:page <%s>;', $page,false);
169 4
			!empty($email) 				&& $_('schema:email "%s";', $email);
170 4
			!empty($homepage) 			&& $_('foaf:homepage <%s>;', $homepage,false);
171 4
			!empty($geoUri) 			&& $_('schema:geo <%s>;', $geoUri);
172 4
			$_('schema:address <%s>. ', $addressUri);
173
			
174
			// serialize schema:PostalAddress 
175 4
			$_('<%s> a schema:PostalAddress;', $addressUri);
176 4
			!empty($addressDescription) && $_('schema:description "%s";', $addressDescription);
177 4
			!empty($streetAddress) 		&& $_('schema:streetAddress "%s";', $streetAddress);
178 4
			!empty($postalCode) 		&& $_('schema:postalCode "%s";', $postalCode);
179 4
			!empty($addressLocality) 	&& $_('schema:addressLocality "%s";', $addressLocality);
180 4
			!empty($addressRegion) 		&& $_('schema:addressRegion "%s";', $addressRegion);
181 4
			$_('schema:addressCountry "%s". ', $addressCountry);
182
183
			// serialize schema:GeoCoordinates
184 4
			if( !empty($geoUri)){
185 2
				$_('<%s> a schema:GeoCoordinates;', $geoUri); 
186 2
				$_('wgs:lat "%s"^^xsd:float;', $lat);
187 2
				$_('wgs:long "%s"^^xsd:float . ', $long); 
188
			}
189
190 4
			$this->rdf = $turtleString;
191 4
			$this->tripleCount = $tripleCounter;
192
		}
193
		
194 4
		return $this->rdf;
195
	}
196
	
197
}