Completed
Pull Request — master (#8)
by Enrico
02:00
created

BusinessContact   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 32
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 157
ccs 20
cts 20
cp 1
rs 9.6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B makePersonNameDescription() 0 13 5
F asTurtleFragment() 0 56 27
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 BusinessContact extends AbstractModel implements \BOTK\ModelInterface 
11
{
12
13
	protected static $DEFAULT_OPTIONS = array (
14
		'personType'		=> array(		
15
			// additional types  as extension of schema:Person
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
		'alternateName'		=> array(
25
			'filter'    => FILTER_CALLBACK,
26
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_PERSON_NAME',
27
			'flags'  	=> FILTER_FORCE_ARRAY,
28
			),
29
		'givenName'				=> array(
30
			'filter'    => FILTER_CALLBACK,
31
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_PERSON_NAME',
32
			'flags'  	=> FILTER_REQUIRE_SCALAR,
33
			),
34
		'familyName'				=> array(
35
			'filter'    => FILTER_CALLBACK,
36
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_PERSON_NAME',
37
			'flags'  	=> FILTER_REQUIRE_SCALAR,
38
			),
39
		'additionalName'			=> array(
40
			'filter'    => FILTER_CALLBACK,
41
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_PERSON_NAME',
42
			'flags'  	=> FILTER_REQUIRE_SCALAR,
43
			),
44
		'jobTitle'	=> array(
45
			'filter'    => FILTER_DEFAULT,
46
			'flags'  	=> FILTER_FORCE_ARRAY,
47
			),
48
		'honorificPrefix'	=> array(
49
			'filter'    => FILTER_DEFAULT,
50
			'flags'  	=> FILTER_FORCE_ARRAY,
51
			),
52
		'honorificSuffix'	=> array(
53
			'filter'    => FILTER_DEFAULT,
54
			'flags'  	=> FILTER_FORCE_ARRAY,
55
			),
56
		'gender'	=> array(	
57
			'filter'    => FILTER_CALLBACK,
58
			'options'    => '\BOTK\Filters::FILTER_SANITIZE_GENDER',
59
			'flags'  	=> FILTER_REQUIRE_SCALAR,
60
			),
61
		'worksFor'	=> array(		
62
			'filter'    => FILTER_CALLBACK,
63
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
64
			'flags'  	=> FILTER_REQUIRE_SCALAR,
65
			),
66
		'telephone'			=> array(	
67
			'filter'    => FILTER_CALLBACK,	
68
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE',
69
			'flags'  	=> FILTER_FORCE_ARRAY,
70
			),
71
		'email'				=> array(	
72
			'filter'    => FILTER_CALLBACK,
73
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_EMAIL',
74
			'flags'  	=> FILTER_FORCE_ARRAY,
75
			),
76
		'spokenLanguage'			=> array(	
77
			'filter'    => FILTER_CALLBACK,
78
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_LANGUAGE',
79
			'flags'  	=> FILTER_FORCE_ARRAY,
80
			),
81
		'hasOptInOptOutDate'				=> array(	
82
			'filter'    => FILTER_CALLBACK,
83
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_DATETIME',
84
			'flags'  	=> FILTER_REQUIRE_SCALAR,
85
			),
86
		'privacyFlag'		=> array(	
87
			'filter'    => FILTER_CALLBACK,
88
			'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_BOOLEAN',
89
			'flags'  	=> FILTER_REQUIRE_SCALAR,
90
			),
91
		);
92
93
94 4
	private function makePersonNameDescription()
95
	{	
96 4
		extract($this->data);
97
			
98 4
		if(empty($alternateName)){
99 3
			$alternateName ='';
100 3
			if(!empty($givenName)) { $alternateName.= "$givenName ";}						
101 3
			if(!empty($additionalName)) { $alternateName.= "$additionalName ";}		
102 3
			if(!empty($familyName)) { $alternateName.= "$familyName ";}
103
		}
104
		
105 4
		return \BOTK\Filters::FILTER_SANITIZE_PERSON_NAME($alternateName);
106
	}
107
	
108
	
109 4
	public function asTurtleFragment()
110
	{
111 4
		if(is_null($this->rdf)) {
112 4
			extract($this->data);
113
114
			// create uris
115 4
			$personUri = $this->getUri();
116
			
117
			// make a default for altenatename (can be empty)
118 4
			$alternateName = $this->makePersonNameDescription();
119
			
120 4
			$tripleCounter =0;
121
			
122
			// define $_ as a macro to write simple rdf
123 4
			$_= function($format, $var,$sanitize=true) use(&$turtleString, &$tripleCounter){
124 4
				foreach((array)$var as $v){
125 4
					if($var){
126 4
						$turtleString.= sprintf($format,$sanitize?\BOTK\Filters::FILTER_SANITIZE_TURTLE_STRING($v):$v);
127 4
						$tripleCounter++;
128
					}
129
				}
130 4
			};
131
132
	 		// serialize schema:LocalBusiness
133
	 		
134
			$turtleString= "<$personUri> ";			
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $turtleString, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
135
			!empty($id) 				&& $_('dct:identifier "%s";', $id); 
136
			!empty($disambiguatingDescription)&& $_('schema:disambiguatingDescription "%s";', $disambiguatingDescription);
137
			!empty($aggregateRatingValue)&& $_('schema:aggregateRating [a schema:AggregateRating; schema:ratingValue "%s"^^xsd:float];', $aggregateRatingValue);
138
			!empty($page) 				&& $_('foaf:page <%s>;', $page,false);
139
			!empty($homepage) 			&& $_('foaf:homepage <%s>;', $homepage,false);
140
			!empty($personType) 		&& $_('a %s;', $personType);
141
			!empty($taxID) 				&& $_('schema:taxID "%s";', $taxID);
142
			!empty($givenName)			&& $_('schema:givenName "%s";', $givenName);
143
			!empty($familyName)			&& $_('schema:familyName "%s";', $familyName);
144
			!empty($additionalName) 	&& $_('schema:additionalName "%s";', $additionalName);
145
			!empty($alternateName) 		&& $_('schema:alternateName "%s";', $alternateName);
146
			!empty($telephone) 			&& $_('schema:telephone "%s";', $telephone);
147
			!empty($faxNumber) 			&& $_('schema:faxNumber "%s";', $faxNumber);
148
			!empty($jobTitle)			&& $_('schema:jobTitle "%s";', $jobTitle);
149
			!empty($honorificPrefix)	&& $_('schema:honorificPrefix "%s";', $honorificPrefix);
150
			!empty($honorificSuffix)	&& $_('schema:honorificSuffix "%s";', $honorificSuffix);
151
			!empty($email) 				&& $_('schema:email "%s";', $email);
152
			!empty($gender)				&& $_('schema:gender "%s";', $gender);
153
			!empty($worksFor)			&& $_('schema:worksFor <%s> ;', $worksFor,false);
154
			!empty($spokenLanguage)		&& $_('botk:spokenLanguage "%s";', $spokenLanguage);
155
			!empty($hasOptInOptOutDate)	&& $_('botk:hasOptInOptOutDate "%s";', $hasOptInOptOutDate);
156
			!empty($privacyFlag)		&& $_('botk:privacyFlag <%s>;', $privacyFlag);		
157
			$_('a schema:Person .', $personUri);
158
159
			$this->rdf = $turtleString;
160
			$this->tripleCount = $tripleCounter;
161
		}
162
163
		return $this->rdf;
164
	}
165
166
}