Completed
Push — master ( e1b3cc...52b7dd )
by Enrico
03:03
created

LocalBusiness::addAddressDescription()   B

Complexity

Conditions 9
Paths 20

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9

Importance

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