|
1 | 1 | <?php |
| 2 | + |
2 | 3 | /** |
3 | 4 | * This file contains the Formatter |
4 | 5 | * |
|
7 | 8 | * @author Jan Vansteenlandt <[email protected]> |
8 | 9 | * @author Pieter Colpaert <[email protected]> |
9 | 10 | */ |
| 11 | + |
10 | 12 | namespace tdt\formatters; |
| 13 | + |
11 | 14 | use tdt\exceptions\TDTException; |
12 | | -class Formatter{ |
| 15 | + |
| 16 | +class Formatter { |
13 | 17 |
|
14 | 18 | private $format; |
15 | 19 |
|
16 | | - public function __construct($format = ""){ |
| 20 | + public function __construct($format = "") { |
17 | 21 | $this->setFormat($format); |
18 | 22 | } |
19 | 23 |
|
20 | 24 | /** |
21 | 25 | * sets the requested format in the factory from the request URL |
22 | 26 | * @param string $urlformat The format of the request i.e. json,xml,.... |
23 | 27 | */ |
24 | | - public function setFormat($urlformat){ |
| 28 | + public function setFormat($urlformat) { |
25 | 29 | //We define the format like this: |
26 | 30 | // * Check if $urlformat has been set |
27 | 31 | // - if not: probably something fishy happened, set format as error for logging purpose |
28 | 32 | // - else if is about: do content negotiation |
29 | 33 | // - else check if format exists |
30 | 34 | // × throw exception when it doesn't |
31 | 35 | // × if it does, set $this->format with ucfirst |
32 | | - |
33 | 36 | //first, let's be sure about the case of the format |
34 | 37 | $urlformat = strtoupper($urlformat); |
35 | 38 |
|
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 |
38 | 40 | $cn = new \tdt\negotiators\ContentNegotiator(); |
39 | 41 | $format = strtoupper($cn->pop()); |
40 | | - while(!$this->formatExists($format) && $cn->hasNext()){ |
| 42 | + while (!$this->formatExists($format) && $cn->hasNext()) { |
41 | 43 | $format = strtoupper($cn->pop()); |
42 | | - if($format == "*"){ |
| 44 | + if ($format == "*") { |
43 | 45 | $format == "XML"; |
44 | 46 | } |
45 | 47 | } |
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 |
48 | 50 | } |
49 | 51 | $this->format = $format; |
50 | 52 | //We've found our format through about, so let's set the header for content-location to the right one |
51 | 53 | //to do this we're building our current URL and changing .about in .format |
52 | | - $format= strtoupper($this->format); |
| 54 | + $format = strtoupper($this->format); |
53 | 55 | $pageURL = 'http'; |
54 | | - if (isset($_SERVER["HTTPS"])) {$pageURL .= "s";} |
| 56 | + if (isset($_SERVER["HTTPS"])) { |
| 57 | + $pageURL .= "s"; |
| 58 | + } |
55 | 59 | $pageURL .= "://"; |
56 | 60 | 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"]; |
58 | 62 | } else { |
59 | | - $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; |
| 63 | + $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; |
60 | 64 | } |
61 | 65 | $contentlocation = str_ireplace(".about", "." . $format, $pageURL); |
62 | 66 | header("Content-Location:" . $contentlocation); |
63 | | - }else if($this->formatExists($urlformat)){ |
| 67 | + } else if ($this->formatExists($urlformat)) { |
64 | 68 | $this->format = $urlformat; |
65 | | - }else{ |
66 | | - throw new TDTException(451,array($urlformat)); |
| 69 | + } else { |
| 70 | + throw new TDTException(451, array($urlformat)); |
67 | 71 | } |
68 | 72 | } |
69 | 73 |
|
70 | | - private function formatExists($format){ |
| 74 | + private function formatExists($format) { |
71 | 75 | return class_exists("\\tdt\\formatters\\strategies\\$format"); |
72 | 76 | } |
73 | 77 |
|
74 | 78 | /* |
75 | 79 | * This function has to create a strategy and print everything using this strategy. |
76 | 80 | */ |
77 | | - public function execute($rootname, $thing){ |
| 81 | + |
| 82 | + public function execute($rootname, $thing) { |
78 | 83 | $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 |
80 | 87 | /** |
81 | 88 | * Check if which formatter we're dealing with (normal object formatter or ARC grap formatter) |
82 | 89 | * 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) |
83 | 90 | */ |
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))) { |
87 | 93 | $thing = $this->convertPHPObjectToARC($thing); |
88 | 94 | } |
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))) { |
91 | 97 | $thing = $this->convertARCToPHPObject($thing); |
92 | 98 | } |
93 | | - } |
| 99 | + }*/ |
94 | 100 |
|
95 | 101 | // remake the formatting strategy |
96 | | - $strategy = new $format($rootname,$thing); |
| 102 | + //$strategy = new $format($rootname, $thing); |
97 | 103 | $strategy->execute(); |
98 | 104 | } |
99 | 105 |
|
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; |
120 | 153 | // } |
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); |
126 | 168 | // } |
127 | 169 | // |
128 | | -// // Import Package Syntax |
129 | | -// include_once(RDFAPI_INCLUDE_DIR . PACKAGE_SYNTAX_N3); |
| 170 | +// return $result; |
| 171 | +// } |
130 | 172 | // |
131 | | -// $ser = new N3Serializer(); |
| 173 | +// private function stripURI($uri) { |
| 174 | +// $pos = strrpos($uri, "#"); |
132 | 175 | // |
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 | +// } |
146 | 188 |
|
147 | 189 | /** |
148 | 190 | * Returns the format that has been set by the request |
149 | 191 | * @return A format object |
150 | 192 | */ |
151 | | - public function getFormat(){ |
152 | | - return $this->format; |
153 | | - } |
| 193 | + public function getFormat() { |
| 194 | + return $this->format; |
| 195 | + } |
154 | 196 |
|
155 | | - public function getFormatterDocumentation(){ |
156 | | - $doc = array(); |
| 197 | + public function getFormatterDocumentation() { |
| 198 | + $doc = array(); |
157 | 199 | //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))) { |
160 | 202 |
|
161 | | - $filenameparts = explode(".", $formatter); |
162 | | - $formattername = $filenameparts[0]; |
| 203 | + $filenameparts = explode(".", $formatter); |
| 204 | + $formattername = $filenameparts[0]; |
163 | 205 | //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; |
165 | 207 |
|
166 | | - if ($formatter != "." && $formatter != ".." && class_exists($classname)) { |
| 208 | + if ($formatter != "." && $formatter != ".." && class_exists($classname)) { |
167 | 209 |
|
168 | | - if (is_subclass_of($classname, "tdt\\formatters\\AStrategy")) { |
| 210 | + if (is_subclass_of($classname, "tdt\\formatters\\AStrategy")) { |
169 | 211 |
|
170 | 212 | /* |
171 | 213 | * Get the name without Formatter as $formattername |
172 | 214 | */ |
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 | + } */ |
178 | 220 |
|
179 | 221 | /* |
180 | 222 | * Remove the namespace if present from the formattername |
|
0 commit comments