Example #1
0
 private function getBestFormat($app)
 {
     $negotiator = new \Negotiation\FormatNegotiator();
     $acceptHeader = $app->request->headers->get('accept');
     $priorities = array('application/json', 'application/xml', 'text/plain', '*/*');
     return $negotiator->getBestFormat($acceptHeader, $priorities);
 }
Example #2
0
 /**
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     // Use content negotiation to determine the correct format to return
     $negotiator = new \Negotiation\FormatNegotiator();
     $acceptHeader = Request::header('accept');
     // Set priorities to json (if the app requests json provide it in favor of html)
     $priorities = array('application/json', 'text/html', '*/*');
     // Get the best appropriate content type
     $result = $negotiator->getBest($acceptHeader, $priorities);
     // Default to html if $result is not set
     $val = "text/html";
     if (isset($result)) {
         $val = $result->getValue();
     }
     // See what kind of content we should return
     switch ($val) {
         case "text/html":
             // In case we want to return html, just let
             // Laravel render the view and send the headers
             return Response::view('route.planner')->header('Content-Type', "text/html")->header('Vary', 'accept');
             break;
         case "application/json":
         default:
             // In case we want to return JSON(LD) or something else, generate
             // our JSON by calling our static function 'getJSON()'
             return Response::make($this::getJSON())->header('Content-Type', "application/json")->header('Vary', 'accept');
             break;
     }
 }
Example #3
0
 protected function negotiateType()
 {
     $this->content_return_type = 'html';
     //Default
     if ($this->getHeader('ACCEPT', false)) {
         $negotiator = new \Negotiation\FormatNegotiator();
         $acceptable_types = array('html', '*/*');
         $acceptable_types = array('html', 'json', 'xml', '*/*');
         $this->content_return_type = $negotiator->getBestFormat($this->getHeader('ACCEPT'), $acceptable_types);
         if ($this->content_return_type == '*/*') {
             $this->content_return_type = 'html';
         }
     }
 }
Example #4
0
 /**
  * Handles json encoding, adding the content type headers and optional callback function.
  * @param array $data the data to be returned.
  */
 private function returnJson($data)
 {
     if (filter_input(INPUT_GET, 'callback', FILTER_SANITIZE_STRING)) {
         header("Content-type: application/javascript; charset=utf-8");
         // wrap with JSONP callback
         echo filter_input(INPUT_GET, 'callback', FILTER_UNSAFE_RAW) . "(" . json_encode($data) . ");";
     } else {
         // negotiate suitable format
         $negotiator = new \Negotiation\FormatNegotiator();
         $priorities = array('application/json', 'application/ld+json');
         $best = filter_input(INPUT_SERVER, 'HTTP_ACCEPT', FILTER_SANITIZE_STRING) ? $negotiator->getBest(filter_input(INPUT_SERVER, 'HTTP_ACCEPT', FILTER_SANITIZE_STRING), $priorities) : null;
         $format = $best !== null ? $best->getValue() : $priorities[0];
         header("Content-type: {$format}; charset=utf-8");
         header("Vary: Accept");
         // inform caches that we made a choice based on Accept header
         echo json_encode($data);
     }
 }
Example #5
0
<?php

$loader = (require_once __DIR__ . '/vendor/autoload.php');
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');
$shop = new \Test\Shop();
$formatNegotiator = new \Negotiation\FormatNegotiator();
$formatNegotiator->registerFormat('json', ['application/vnd.meshop+json;version=1', 'application/vnd.meshop+json;version=2'], true);
$formatNegotiator->registerFormat('xml', ['application/vnd.meshop+xml;version=1', 'application/vnd.meshop+xml;version=2'], true);
$shop = new \Negotiation\Stack\Negotiation($shop, $formatNegotiator, null, null, ['format_priorities' => ['application/vnd.meshop+json;version=2', 'application/vnd.meshop+xml;version=2', 'application/vnd.meshop+json;version=1', 'application/vnd.meshop+xml;version=1', 'application/hal+json', 'application/json'], 'language_priorities' => []]);
return $shop;
Example #6
0
 /**
  * Shows a train or train data, based on the accept-header.
  * @param $station_id
  * @param $liveboard_id
  * @return array
  * @throws EasyRdf_Exception
  */
 public function specificTrain($station_id, $liveboard_id)
 {
     $negotiator = new \Negotiation\FormatNegotiator();
     $acceptHeader = Request::header('accept');
     $priorities = array('application/json', 'text/html', '*/*');
     $result = $negotiator->getBest($acceptHeader, $priorities);
     $val = $result->getValue();
     //get the right date-time to query
     $datetime = substr($liveboard_id, 0, 12);
     $datetime = strtotime($datetime);
     $archived = false;
     if ($datetime < strtotime("now")) {
         $archived = true;
     }
     switch ($val) {
         case "text/html":
             // Convert id to string for interpretation by old API
             $stationStringName = \hyperRail\StationString::convertToString($station_id);
             if (!$archived) {
                 // Set up path to old api
                 $URL = "http://api.irail.be/liveboard/?station=" . urlencode($stationStringName->name) . "&date=" . date("mmddyy", $datetime) . "&time=" . date("Hi", $datetime) . "&fast=true&lang=nl&format=json";
                 // Get the contents of this path
                 $data = file_get_contents($URL);
                 // Convert the data to the new liveboard object
                 $newData = \hyperRail\FormatConverter::convertLiveboardData($data, $station_id);
                 // Read new liveboard object and return the page but load data
                 foreach ($newData['@graph'] as $graph) {
                     if (strpos($graph['@id'], $liveboard_id) !== false) {
                         return View::make('stations.departuredetail')->with('station', $graph)->with('departureStation', $stationStringName);
                     }
                 }
                 App::abort(404);
             } else {
                 // If no match is found, attempt to look in the archive
                 // Fetch file using curl
                 $ch = curl_init("http://archive.irail.be/" . 'irail?subject=' . urlencode('http://irail.be/stations/NMBS/' . $station_id . '/departures/' . $liveboard_id));
                 curl_setopt($ch, CURLOPT_HEADER, 0);
                 $request_headers[] = 'Accept: text/turtle';
                 curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 $turtle = curl_exec($ch);
                 curl_close($ch);
                 // Convert turtle to json-ld
                 // Create a new graph
                 $graph = new EasyRdf_Graph();
                 if (empty($_REQUEST['data'])) {
                     // Load the sample information
                     $graph->parse($turtle, 'turtle');
                 }
                 // Export to JSON LD
                 $format = EasyRdf_Format::getFormat('jsonld');
                 $output = $graph->serialise($format);
                 if (!is_scalar($output)) {
                     $output = var_export($output, true);
                 }
                 // First, define the context
                 $context = array("delay" => "http://semweb.mmlab.be/ns/rplod/delay", "platform" => "http://semweb.mmlab.be/ns/rplod/platform", "scheduledDepartureTime" => "http://semweb.mmlab.be/ns/rplod/scheduledDepartureTime", "headsign" => "http://vocab.org/transit/terms/headsign", "routeLabel" => "http://semweb.mmlab.be/ns/rplod/routeLabel", "stop" => array("@id" => "http://semweb.mmlab.be/ns/rplod/stop", "@type" => "@id"), "seeAlso" => array("@id" => "http://www.w3.org/2000/01/rdf-schema#seeAlso", "@type" => "@id"));
                 // Next, encode the context as JSON
                 $jsonContext = json_encode($context);
                 // Compact the JsonLD by using @context
                 $compacted = JsonLD::compact($output, $jsonContext);
                 // Print the resulting JSON-LD!
                 $urlToFind = 'NMBS/' . $station_id . '/departures/' . $liveboard_id;
                 $stationDataFallback = json_decode(JsonLD::toString($compacted, true));
                 foreach ($stationDataFallback->{'@graph'} as $graph) {
                     if (strpos($graph->{'@id'}, $urlToFind) !== false) {
                         return View::make('stations.departurearchive')->with('station', $graph)->with('departureStation', $stationStringName);
                     }
                 }
                 App::abort(404);
             }
             break;
         case "application/json":
         case "application/ld+json":
         default:
             $stationStringName = \hyperRail\StationString::convertToString($station_id);
             if (!$archived) {
                 $URL = "http://api.irail.be/liveboard/?station=" . urlencode($stationStringName->name) . "&date=" . date("mmddyy", $datetime) . "&time=" . date("Hi", $datetime) . "&fast=true&lang=nl&format=json";
                 $data = file_get_contents($URL);
                 $newData = \hyperRail\FormatConverter::convertLiveboardData($data, $station_id);
                 foreach ($newData['@graph'] as $graph) {
                     if (strpos($graph['@id'], $liveboard_id) !== false) {
                         $context = array("delay" => "http://semweb.mmlab.be/ns/rplod/delay", "platform" => "http://semweb.mmlab.be/ns/rplod/platform", "scheduledDepartureTime" => "http://semweb.mmlab.be/ns/rplod/scheduledDepartureTime", "headsign" => "http://vocab.org/transit/terms/headsign", "routeLabel" => "http://semweb.mmlab.be/ns/rplod/routeLabel", "stop" => array("@id" => "http://semweb.mmlab.be/ns/rplod/stop", "@type" => "@id"));
                         return array("@context" => $context, "@graph" => $graph);
                     }
                 }
                 App::abort(404);
             } else {
                 // If no match is found, attempt to look in the archive
                 // Fetch file using curl
                 $ch = curl_init("http://archive.irail.be/" . 'irail?subject=' . urlencode('http://irail.be/stations/NMBS/' . $station_id . '/departures/' . $liveboard_id));
                 curl_setopt($ch, CURLOPT_HEADER, 0);
                 $request_headers[] = 'Accept: text/turtle';
                 curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 $turtle = curl_exec($ch);
                 curl_close($ch);
                 // Convert turtle to json-ld
                 // Create a new graph
                 $graph = new EasyRdf_Graph();
                 if (empty($_REQUEST['data'])) {
                     // Load the sample information
                     $graph->parse($turtle, 'turtle');
                 }
                 // Export to JSON LD
                 $format = EasyRdf_Format::getFormat('jsonld');
                 $output = $graph->serialise($format);
                 if (!is_scalar($output)) {
                     $output = var_export($output, true);
                 }
                 // First, define the context
                 $context = array("delay" => "http://semweb.mmlab.be/ns/rplod/delay", "platform" => "http://semweb.mmlab.be/ns/rplod/platform", "scheduledDepartureTime" => "http://semweb.mmlab.be/ns/rplod/scheduledDepartureTime", "headsign" => "http://vocab.org/transit/terms/headsign", "routeLabel" => "http://semweb.mmlab.be/ns/rplod/routeLabel", "stop" => array("@id" => "http://semweb.mmlab.be/ns/rplod/stop", "@type" => "@id"), "seeAlso" => array("@id" => "http://www.w3.org/2000/01/rdf-schema#seeAlso", "@type" => "@id"));
                 // Next, encode the context as JSON
                 $jsonContext = json_encode($context);
                 // Compact the JsonLD by using @context
                 $compacted = JsonLD::compact($output, $jsonContext);
                 // Print the resulting JSON-LD!
                 $urlToFind = 'NMBS/' . $station_id . '/departures/' . $liveboard_id;
                 $stationDataFallback = json_decode(JsonLD::toString($compacted, true));
                 foreach ($stationDataFallback->{'@graph'} as $graph) {
                     if (strpos($graph->{'@id'}, $urlToFind) !== false) {
                         return array("@context" => $context, "@graph" => $graph);
                     }
                 }
                 App::abort(404);
             }
             break;
     }
 }