public function testContent() { $_SERVER['HTTP_ACCEPT'] = "text/html, application/json;q=0.8, text/csv;q=0.7"; $ln = new \tdt\negotiators\ContentNegotiator(); $this->assertEquals("html", $ln->pop()); $this->assertEquals("json", $ln->pop()); $this->assertEquals("csv", $ln->pop()); }
/** * sets the requested format in the factory from the request URL * @param string $urlformat The format of the request i.e. json,xml,.... */ public function setFormat($urlformat) { //We define the format like this: // * Check if $urlformat has been set // - if not: probably something fishy happened, set format as error for logging purpose // - else if is about: do content negotiation // - else check if format exists // × throw exception when it doesn't // × if it does, set $this->format with ucfirst //first, let's be sure about the case of the format $urlformat = strtoupper($urlformat); if (strtoupper($urlformat) == "ABOUT" || $urlformat == "") { //urlformat can be empty on SPECTQL query $cn = new \tdt\negotiators\ContentNegotiator(); $format = strtoupper($cn->pop()); while (!$this->formatExists($format) && $cn->hasNext()) { $format = strtoupper($cn->pop()); if ($format == "*") { $format == "XML"; } } if (!$this->formatExists($format)) { throw new TDTException(451, array($format)); // could not find a suitible format } $this->format = $format; //We've found our format through about, so let's set the header for content-location to the right one //to do this we're building our current URL and changing .about in .format $format = strtoupper($this->format); $pageURL = 'http'; if (isset($_SERVER["HTTPS"])) { $pageURL .= "s"; } $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; } $contentlocation = str_ireplace(".about", "." . $format, $pageURL); header("Content-Location:" . $contentlocation); } else { if ($this->formatExists($urlformat)) { $this->format = $urlformat; } else { throw new TDTException(451, array($urlformat)); } } }