Skip to content

Commit e3b657e

Browse files
Merge pull request #1 from tdt/development
Graph formatter behaviour
2 parents 9c02563 + 9780201 commit e3b657e

File tree

8 files changed

+204
-193
lines changed

8 files changed

+204
-193
lines changed

src/tdt/formatters/AStrategy.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,21 @@ public function execute() {
3939
header("Expires: Sun, 19 Nov 1978 04:59:59 GMT");
4040

4141
$this->printHeader();
42-
$this->printBody();
42+
43+
if (!$this->isObjectAGraph())
44+
$this->printBody();
45+
else
46+
$this->printGraph();
47+
}
48+
49+
/*
50+
* This function checks wether the object to print is an RDF graph or not
51+
*/
52+
protected function isObjectAGraph() {
53+
foreach ($this->objectToPrint as $prop)
54+
return ($prop instanceof \ARC2_RDFParser);
55+
56+
return false;
4357
}
4458

4559
/**
@@ -52,4 +66,11 @@ abstract public function printHeader();
5266
*/
5367
abstract public function printBody();
5468

69+
/**
70+
* This function will print the body of the responsemessage when the object is a graph.
71+
*/
72+
public function printGraph(){
73+
throw new \Exception("This resource does not contain semantic information");
74+
}
75+
5576
}

src/tdt/formatters/Formatter.php

Lines changed: 127 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This file contains the Formatter
45
*
@@ -7,174 +8,215 @@
78
* @author Jan Vansteenlandt <[email protected]>
89
* @author Pieter Colpaert <[email protected]>
910
*/
11+
1012
namespace tdt\formatters;
13+
1114
use tdt\exceptions\TDTException;
12-
class Formatter{
15+
16+
class Formatter {
1317

1418
private $format;
1519

16-
public function __construct($format = ""){
20+
public function __construct($format = "") {
1721
$this->setFormat($format);
1822
}
1923

2024
/**
2125
* sets the requested format in the factory from the request URL
2226
* @param string $urlformat The format of the request i.e. json,xml,....
2327
*/
24-
public function setFormat($urlformat){
28+
public function setFormat($urlformat) {
2529
//We define the format like this:
2630
// * Check if $urlformat has been set
2731
// - if not: probably something fishy happened, set format as error for logging purpose
2832
// - else if is about: do content negotiation
2933
// - else check if format exists
3034
// × throw exception when it doesn't
3135
// × if it does, set $this->format with ucfirst
32-
3336
//first, let's be sure about the case of the format
3437
$urlformat = strtoupper($urlformat);
3538

36-
if(strtoupper($urlformat) == "ABOUT" || $urlformat == "" ){ //urlformat can be empty on SPECTQL query
37-
39+
if (strtoupper($urlformat) == "ABOUT" || $urlformat == "") { //urlformat can be empty on SPECTQL query
3840
$cn = new \tdt\negotiators\ContentNegotiator();
3941
$format = strtoupper($cn->pop());
40-
while(!$this->formatExists($format) && $cn->hasNext()){
42+
while (!$this->formatExists($format) && $cn->hasNext()) {
4143
$format = strtoupper($cn->pop());
42-
if($format == "*"){
44+
if ($format == "*") {
4345
$format == "XML";
4446
}
4547
}
46-
if(!$this->formatExists($format)){
47-
throw new TDTException(451,array($format)); // could not find a suitible format
48+
if (!$this->formatExists($format)) {
49+
throw new TDTException(451, array($format)); // could not find a suitible format
4850
}
4951
$this->format = $format;
5052
//We've found our format through about, so let's set the header for content-location to the right one
5153
//to do this we're building our current URL and changing .about in .format
52-
$format= strtoupper($this->format);
54+
$format = strtoupper($this->format);
5355
$pageURL = 'http';
54-
if (isset($_SERVER["HTTPS"])) {$pageURL .= "s";}
56+
if (isset($_SERVER["HTTPS"])) {
57+
$pageURL .= "s";
58+
}
5559
$pageURL .= "://";
5660
if ($_SERVER["SERVER_PORT"] != "80") {
57-
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
61+
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
5862
} else {
59-
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
63+
$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
6064
}
6165
$contentlocation = str_ireplace(".about", "." . $format, $pageURL);
6266
header("Content-Location:" . $contentlocation);
63-
}else if($this->formatExists($urlformat)){
67+
} else if ($this->formatExists($urlformat)) {
6468
$this->format = $urlformat;
65-
}else{
66-
throw new TDTException(451,array($urlformat));
69+
} else {
70+
throw new TDTException(451, array($urlformat));
6771
}
6872
}
6973

70-
private function formatExists($format){
74+
private function formatExists($format) {
7175
return class_exists("\\tdt\\formatters\\strategies\\$format");
7276
}
7377

7478
/*
7579
* This function has to create a strategy and print everything using this strategy.
7680
*/
77-
public function execute($rootname, $thing){
81+
82+
public function execute($rootname, $thing) {
7883
$format = "\\tdt\\formatters\\strategies\\" . $this->format;
79-
$strategy = new $format($rootname,$thing);
84+
$strategy = new $format($rootname, $thing);
85+
86+
//Didn't really make sense to split the formats up, so for the moment this is commented
8087
/**
8188
* Check if which formatter we're dealing with (normal object formatter or ARC grap formatter)
8289
* According to the result of this control check, convert (if necessary the object to the appropriate object structure e.g. from graph to php object or vice versa)
8390
*/
84-
85-
if(!$this->isObjectAGraph($thing)){
86-
if(array_key_exists('tdt\\formatters\\interfaces\\iSemanticFormatter',class_implements($strategy))){
91+
/*if (!$this->isObjectAGraph($thing)) {
92+
if (array_key_exists('tdt\\formatters\\interfaces\\iSemanticFormatter', class_implements($strategy))) {
8793
$thing = $this->convertPHPObjectToARC($thing);
8894
}
89-
}else {
90-
if(!array_key_exists('tdt\\formatters\\interfaces\\iSemanticFormatter',class_implements($strategy))){
95+
} else {
96+
if (!array_key_exists('tdt\\formatters\\interfaces\\iSemanticFormatter', class_implements($strategy))) {
9197
$thing = $this->convertARCToPHPObject($thing);
9298
}
93-
}
99+
}*/
94100

95101
// remake the formatting strategy
96-
$strategy = new $format($rootname,$thing);
102+
//$strategy = new $format($rootname, $thing);
97103
$strategy->execute();
98104
}
99105

100-
/**
101-
* TODO Miel: implement these functions
102-
*/
103-
104-
protected function isObjectAGraph($object){
105-
foreach ($object as $class => $prop)
106-
return ($prop instanceof \ARC2_RDFParser);
107-
108-
return false;
109-
}
110-
111-
protected function convertPHPObjectToARC($object){
112-
//REWRITE
113-
114-
// //Unwrap the object
115-
// foreach ($this->objectToPrint as $class => $prop){
116-
// if (is_a($prop,"MemModel")){
117-
// $this->objectToPrint = $prop;
118-
// break;
119-
// }
106+
//This logic has moved to AStrategy and will probably stay there. For now, keep it commented.
107+
108+
// protected function isObjectAGraph($object) {
109+
// foreach ($object as $class => $prop)
110+
// return ($prop instanceof \ARC2_RDFParser);
111+
//
112+
// return false;
113+
// }
114+
//
115+
// protected function convertPHPObjectToARC($object) {
116+
// //REWRITE
117+
//// //Unwrap the object
118+
//// foreach ($this->objectToPrint as $class => $prop){
119+
//// if (is_a($prop,"MemModel")){
120+
//// $this->objectToPrint = $prop;
121+
//// break;
122+
//// }
123+
//// }
124+
//// //When the objectToPrint has a MemModel, it is already an RDF model and is ready for serialisation.
125+
//// //Else it's retrieved data of which we need to build an rdf output
126+
//// if (!is_a($this->objectToPrint,"MemModel")) {
127+
//// $outputter = new RDFOutput();
128+
//// $this->objectToPrint = $outputter->buildRdfOutput($this->objectToPrint);
129+
//// }
130+
////
131+
//// // Import Package Syntax
132+
//// include_once(RDFAPI_INCLUDE_DIR . PACKAGE_SYNTAX_N3);
133+
////
134+
//// $ser = new N3Serializer();
135+
////
136+
//// $rdf = $ser->serialize($this->objectToPrint);
137+
//
138+
// throw new \Exception("This resource does not contain semantic information");
139+
//
140+
//
141+
// return $object;
142+
// }
143+
//
144+
// protected function convertARCToPHPObject($graph) {
145+
// foreach ($graph as $class => &$prop) {
146+
// //$graph->$class = $object;
147+
// $index = $prop->getSimpleIndex();
148+
//
149+
// //$result = $this->stripObject($index);
150+
// $result = $index;
151+
// $graph->$class = $result;
152+
// return $graph;
120153
// }
121-
// //When the objectToPrint has a MemModel, it is already an RDF model and is ready for serialisation.
122-
// //Else it's retrieved data of which we need to build an rdf output
123-
// if (!is_a($this->objectToPrint,"MemModel")) {
124-
// $outputter = new RDFOutput();
125-
// $this->objectToPrint = $outputter->buildRdfOutput($this->objectToPrint);
154+
// return $graph;
155+
// }
156+
//
157+
// private function stripObject($obj, $result = array()) {
158+
//
159+
// foreach ($obj as $key => $value) {
160+
//
161+
// $new_key = $this->stripURI($key);
162+
// $result[$new_key] = array();
163+
//
164+
// if (is_array($value))
165+
// $result[$new_key] = $this->stripObject($value, $result[$new_key]);
166+
// else
167+
// $result[$new_key] = $this->stripURI($value);
126168
// }
127169
//
128-
// // Import Package Syntax
129-
// include_once(RDFAPI_INCLUDE_DIR . PACKAGE_SYNTAX_N3);
170+
// return $result;
171+
// }
130172
//
131-
// $ser = new N3Serializer();
173+
// private function stripURI($uri) {
174+
// $pos = strrpos($uri, "#");
132175
//
133-
// $rdf = $ser->serialize($this->objectToPrint);
134-
135-
return $object;
136-
}
137-
138-
protected function convertARCToPHPObject($graph){
139-
foreach ($graph as $class => &$prop){
140-
$graph->$class = $prop->getTriples();
141-
return $graph;
142-
}
143-
return $graph;
144-
}
145-
176+
// if (!$pos)
177+
// $pos = strrpos($uri, "/");
178+
//
179+
//
180+
// if (!$pos)
181+
// $pos = strrpos($uri, ":");
182+
//
183+
// if (!$pos)
184+
// return $uri;
185+
//
186+
// return substr($uri, $pos+1);
187+
// }
146188

147189
/**
148190
* Returns the format that has been set by the request
149191
* @return A format object
150192
*/
151-
public function getFormat(){
152-
return $this->format;
153-
}
193+
public function getFormat() {
194+
return $this->format;
195+
}
154196

155-
public function getFormatterDocumentation(){
156-
$doc = array();
197+
public function getFormatterDocumentation() {
198+
$doc = array();
157199
//open the custom directory and loop through it
158-
if ($handle = opendir(__DIR__ . '/strategies')) {
159-
while (false !== ($formatter = readdir($handle))) {
200+
if ($handle = opendir(__DIR__ . '/strategies')) {
201+
while (false !== ($formatter = readdir($handle))) {
160202

161-
$filenameparts = explode(".", $formatter);
162-
$formattername = $filenameparts[0];
203+
$filenameparts = explode(".", $formatter);
204+
$formattername = $filenameparts[0];
163205
//if the object read is a directory and the configuration methods file exists, then add it to the installed formatters
164-
$classname = "tdt\\formatters\\strategies\\" . $formattername;
206+
$classname = "tdt\\formatters\\strategies\\" . $formattername;
165207

166-
if ($formatter != "." && $formatter != ".." && class_exists($classname)) {
208+
if ($formatter != "." && $formatter != ".." && class_exists($classname)) {
167209

168-
if (is_subclass_of($classname, "tdt\\formatters\\AStrategy")) {
210+
if (is_subclass_of($classname, "tdt\\formatters\\AStrategy")) {
169211

170212
/*
171213
* Get the name without Formatter as $formattername
172214
*/
173-
/*$matches = array();
174-
preg_match('/(.*)Formatter.*', $classname, $matches);
175-
if (isset($matches[1])) {
176-
$formattername = $matches[1];
177-
}*/
215+
/* $matches = array();
216+
preg_match('/(.*)Formatter.*', $classname, $matches);
217+
if (isset($matches[1])) {
218+
$formattername = $matches[1];
219+
} */
178220

179221
/*
180222
* Remove the namespace if present from the formattername

0 commit comments

Comments
 (0)