dmBridge PHP API
DMHTTPResponseFactory.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00013 abstract class DMHTTPResponseFactory {
00014 
00015    private static function getBestRepresentation(DMHTTPRequest $req) {
00016       $route = $req->getURI()->getRoute();
00017       $best_rep = null;
00018       if ($route) {
00019          $best_rep = $route->getBestRepresentation(
00020                $req->getURI()->getExtension());
00021       } else {
00022          $best_rep = $req->getURI()->getExtension();
00023          if (!$best_rep) {
00024             $best_rep = $req->getURI()->isHTTPAPI() ? "xml" : "html";
00025          }
00026       }
00027       return $best_rep;
00028    }
00029 
00033    public static function getRepresentation() {
00034       $type = new DMMediaType("text", "plain");
00035       $best_rep = self::getBestRepresentation(DMHTTPRequest::getCurrent());
00036 
00037       switch ($best_rep) {
00038       case "atom":
00039          $type = new DMMediaType("application", "atom+xml");
00040          break;
00041       case "html":
00042          $type = new DMMediaType("text", "html");
00043          break;
00044       case "json":
00045          $type = new DMMediaType("application", "json");
00046          break;
00047       case "jsonp":
00048          $type = new DMMediaType("text", "javascript");
00049          break;
00050       case "sitemap":
00051          $type = new DMMediaType("application", "xml");
00052          break;
00053       case "xml":
00054          $type = new DMMediaType("application", "xml");
00055          break;
00056       }
00057 
00058       $rep = new DMHTTPRepresentation();
00059       $rep->setMediaType($type);
00060       return $rep;
00061    }
00062 
00066    public static function getTransformer() {
00067       $version = DMHTTPRequest::getCurrent()->getURI()->getVersion();
00068       $version = $version
00069          ? $version : DMBridgeVersion::getLatestHTTPAPIVersion();
00070 
00071       $best_rep = self::getBestRepresentation(DMHTTPRequest::getCurrent());
00072 
00073       switch ($best_rep) {
00074       case "atom":
00075          return new DMAtomRepresentationTransformer();
00076          break;
00077       case "json":
00078          $class = 'DMJSONRepresentationTransformerV' . $version;
00079          return new $class;
00080          break;
00081       case "jsonp":
00082          $class = 'DMJSONPRepresentationTransformerV' . $version;
00083          return new $class;
00084          break;
00085       case "sitemap":
00086          return new DMSitemapRepresentationTransformer();
00087          break;
00088       case "xml":
00089          $class = 'DMXMLRepresentationTransformerV' . $version;
00090          return new $class;
00091          break;
00092       }
00093       return null;
00094    }
00095 
00096 }
 All Data Structures Functions Variables