dmBridge PHP API
DMRoute.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 
00018 final class DMRoute {
00019 
00023    private $available_representations = array();
00024 
00028    private $controller;
00029 
00033    private $is_module_route = false;
00034 
00038    private $method;
00039 
00044    private $parameters;
00045 
00046 
00054    public function __construct($controller, $method) {
00055       $this->setController($controller);
00056       $this->setMethod($method);
00057    }
00058 
00062    public function getAvailableRepresentations() {
00063       if (count($this->available_representations)) {
00064          return $this->available_representations;
00065       }
00066       return array(
00067          "json" => new DMMediaType("application", "json"),
00068          "xml" => new DMMediaType("application", "xml")
00069       );
00070    }
00071 
00075    public function setAvailableRepresentations(array $representations) {
00076       $this->available_representations = $representations;
00077    }
00078 
00084    public function getBestRepresentation($requested_representation = null) {
00085       $representation = null;
00086 
00087       // try to honor any explicit requests from URL extensions
00088       if ($requested_representation) {
00089          foreach ($this->getAvailableRepresentations() as $ext => $mime) {
00090             if ($requested_representation == $ext) {
00091                $representation = $ext;
00092                break;
00093             }
00094          }
00095       } else {
00096          // try to honor the best match from the Accept header
00097          $accept = content_negotiation::mime_all_negotiation();
00098          if (is_array($accept)) {
00099             foreach ($accept['type'] as $amime) {
00100                foreach ($this->getAvailableRepresentations() as $key => $fmime) {
00101                   if ((string) $amime == (string) $fmime) { // supported
00102                      $representation = $key;
00103                      break(2);
00104                   }
00105                }
00106             }
00107          } else {
00108             $reps = $this->getAvailableRepresentations();
00109             if (count($reps)) {
00110                $representation = key($reps);
00111             }
00112          }
00113       }
00114       return $representation ? $representation : "html";
00115    }
00116 
00120    public function getController() {
00121       return $this->controller;
00122    }
00123 
00127    public function setController($name) {
00128       $this->controller = $name;
00129    }
00130 
00135    public function equals($object) {
00136       if (!$object instanceof DMRoute) {
00137          return false;
00138       }
00139       if ($this->getController() == $object->getController()
00140             && $this->getMethod() == $object->getMethod()
00141             && $this->getParameters() == $object->getParameters()) {
00142          return true;
00143       }
00144       return false;
00145    }
00146 
00150    public function getMethod() {
00151       return $this->method;
00152    }
00153 
00157    public function setMethod($method) {
00158       $this->method = DMString::underscoreToCamelCase($method);
00159    }
00160 
00164    public function isModuleRoute() {
00165       return $this->is_module_route;
00166    }
00167 
00171    public function setModuleRoute($bool) {
00172       $this->is_module_route = (bool) $bool;
00173    }
00174 
00178    public function getParameters() {
00179       return $this->parameters;
00180    }
00181 
00185    public function setParameters($params) {
00186       $this->parameters = $params;
00187    }
00188 
00193    public function isValid() {
00194       return method_exists($this->getController(), $this->getMethod());
00195    }
00196 
00197 }
 All Data Structures Functions Variables