LinkageSchema.php
Go to the documentation of this file.
00001 <?php 00002 00005 00026 class LinkageSchema 00027 { 00029 public $version; 00030 00032 public $linkedType; 00033 00035 public $prefixes; 00036 00038 public $propertyX = array(); 00039 00041 public $typeX = array(); 00042 00043 function __construct() { } 00044 00045 function __destruct() { } 00046 00057 public function setVersion($version) 00058 { 00059 $this->version = $version; 00060 } 00061 00072 public function setLinkedType($linkedType) 00073 { 00074 $this->linkedType = $linkedType; 00075 } 00076 00088 public function setPrefix($prefix, $uri) 00089 { 00090 if(!is_array($this->prefixes)) 00091 { 00092 $this->prefixes = array( $prefix => $uri ); 00093 } 00094 else 00095 { 00096 $this->prefixes[$prefix] = $uri; 00097 } 00098 } 00099 00112 public function setPropertyX($property, $mapTo, &$error) 00113 { $this->addProperty($this->propertyX[$property], $mapTo, $error); } 00114 00128 public function setTypeX($type, $mapTo, $add, &$error) { $this->addType($this->typeX[$type], $mapTo, $add, $error); } 00129 00130 private function addProperty(&$property, $mapTo, &$error) 00131 { 00132 // Check for a prefix to create the full URI of the type 00133 $maptToUri = $mapTo; 00134 00135 if(substr($mapTo, 0, 7) != "http://" && ($pos = strpos($mapTo, ":")) !== FALSE) 00136 { 00137 $prefix = substr($mapTo, 0, $pos); 00138 00139 if(!isset($this->prefixes[$prefix])) 00140 { 00141 $error = "The prefix used '$prefix:' is undefined in the linkage file."; 00142 return (FALSE); 00143 } 00144 else 00145 { 00146 $maptToUri = $this->prefixes[$prefix] . substr($mapTo, $pos + 1, strlen($mapTo) - ($pos + 1)); 00147 } 00148 } 00149 00150 if(!is_array($property)) 00151 { 00152 $property = array( array( "mapTo" => $maptToUri ) ); 00153 } 00154 else 00155 { 00156 // Make sure the property doesn't already exist 00157 $reject = FALSE; 00158 00159 foreach($property as $map) 00160 { 00161 if($map["mapTo"] == $maptToUri) 00162 { 00163 $reject = TRUE; 00164 break; 00165 } 00166 } 00167 00168 if($reject === FALSE) 00169 { 00170 array_push($property, array( "mapTo" => $maptToUri )); 00171 } 00172 } 00173 } 00174 00175 private function addType(&$type, $mapTo, $add, &$error) 00176 { 00177 // case unsensitive; 00178 $type = strtolower($type); 00179 00180 // Check for a prefix to create the full URI of the type 00181 $mapToUri = $mapTo; 00182 00183 if(substr($mapTo, 0, 7) != "http://" && ($pos = strpos($mapTo, ":")) !== FALSE) 00184 { 00185 $prefix = substr($mapTo, 0, $pos); 00186 00187 if(!isset($this->prefixes[$prefix])) 00188 { 00189 $error = "The prefix used '$prefix:' is undefined in the linkage file."; 00190 return (FALSE); 00191 } 00192 else 00193 { 00194 $mapToUri = $this->prefixes[$prefix] . substr($mapTo, $pos + 1, strlen($mapTo) - ($pos + 1)); 00195 } 00196 } 00197 00198 $adds = array(); 00199 00200 foreach($add as $key => $value) 00201 { 00202 $k; 00203 $v; 00204 00205 if(($pos = strpos($key, ":")) !== FALSE) 00206 { 00207 $prefix = substr($key, 0, $pos); 00208 00209 if(!isset($this->prefixes[$prefix])) 00210 { 00211 $error = "The prefix used '$prefix:' is undefined in the linkage file."; 00212 return (FALSE); 00213 } 00214 else 00215 { 00216 $k = $this->prefixes[$prefix] . substr($key, $pos + 1, strlen($key) - ($pos + 1)); 00217 } 00218 } 00219 00220 if(($pos = strpos($value, ":")) !== FALSE) 00221 { 00222 $prefix = substr($value, 0, $pos); 00223 00224 if(!isset($this->prefixes[$prefix])) 00225 { 00226 $error = "The prefix used '$prefix:' is undefined in the linkage file."; 00227 return (FALSE); 00228 } 00229 else 00230 { 00231 $v = $this->prefixes[$prefix] . substr($value, $pos + 1, strlen($value) - ($pos + 1)); 00232 } 00233 } 00234 00235 $adds[$k] = $v; 00236 } 00237 00238 if(!is_array($type)) 00239 { 00240 $type = array( array ("mapTo" => $mapToUri, "add" => $adds) ); 00241 } 00242 else 00243 { 00244 // Make sure the property doesn't already exist 00245 $reject = FALSE; 00246 00247 foreach($type as $map) 00248 { 00249 if($map["mapTo"] == $maptToUri) 00250 { 00251 $reject = TRUE; 00252 break; 00253 } 00254 } 00255 00256 if($reject === FALSE) 00257 { 00258 array_push($type, array ("mapTo" => $maptToUri, "add" => $add)); 00259 } 00260 } 00261 } 00262 00271 public function generateJsonSerialization() 00272 { 00273 $schema = "{\n"; 00274 00275 if($this->version != "") 00276 { 00277 $schema .= " \"version\": \"" . $this->version . "\",\n"; 00278 } 00279 00280 if($this->linkedType != "") 00281 { 00282 $schema .= " \"linkedType\": \"" . $this->linkedType . "\",\n"; 00283 } 00284 00285 if(count($this->prefixes) > 0) 00286 { 00287 $schema .= " \"prefixList\": {\n"; 00288 00289 foreach($this->prefixes as $prefix => $uri) 00290 { 00291 $schema .= " \"" . $prefix . "\": \"" . $uri . "\",\n"; 00292 } 00293 00294 $schema = substr($schema, 0, strlen($schema) - 2) . "\n"; 00295 00296 $schema .= " },\n"; 00297 } 00298 00299 if(count($this->propertyX) > 0) 00300 { 00301 $schema .= " \"attributeList\": {\n"; 00302 00303 foreach($this->propertyX as $property => $maps) 00304 { 00305 $schema .= " \"" . $property . "\": {\n"; 00306 00307 // Could be extended to create arrays of "mapTo" (if more than one property mapTo this attribute) 00308 $schema .= " \"mapTo\": \"" . $maps[0]["mapTo"] . "\"\n"; 00309 00310 $schema .= " },\n"; 00311 } 00312 00313 $schema = substr($schema, 0, strlen($schema) - 2) . "\n"; 00314 00315 $schema .= " },\n"; 00316 } 00317 00318 if(count($this->typeX) < 1) 00319 { 00320 $schema = substr($schema, 0, strlen($schema) - 2) . "\n"; 00321 } 00322 00323 if(count($this->typeX) > 0) 00324 { 00325 $schema .= " \"typeList\": {\n"; 00326 00327 foreach($this->typeX as $type => $maps) 00328 { 00329 $schema .= " \"" . $type . "\": {\n"; 00330 00331 // Could be extended to create arrays of "mapTo" (if more than one property mapTo this attribute) 00332 $schema .= " \"mapTo\": \"" . $maps[0]["mapTo"] . "\"\n"; 00333 00334 $schema .= " },\n"; 00335 } 00336 00337 $schema = substr($schema, 0, strlen($schema) - 2) . "\n"; 00338 00339 $schema .= " }\n"; 00340 } 00341 00342 $schema .= "}\n"; 00343 00344 return ($schema); 00345 } 00346 } 00347 00349 00350 ?>
