dmBridge PHP API
DMURI.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 
00020 class DMURI {
00021 
00025    private $fragment;
00026 
00030    private $host;
00031 
00035    private $password;
00036 
00040    private $path = "/";
00041 
00045    private $port = 80;
00046 
00050    protected $query = array();
00051 
00055    private $scheme = "http";
00056 
00060    private $user;
00061 
00066    public static function getLocalURIWithPath($path) {
00067       $uri = new DMURI(DMHTTPRequest::getCurrent()->getURI());
00068       $uri->setPath($path);
00069       return $uri;
00070    }
00071 
00076    public function __construct($uri = null) {
00077       if ($uri) {
00078          $this->setString($uri);
00079       }
00080    }
00081 
00086    public function __toString() {
00087       return (string) $this->getAbsoluteURIAsString();
00088    }
00089 
00094    public function equals($obj) {
00095       if (!$obj instanceof DMURI) {
00096          return false;
00097       }
00098       return ($obj->__toString() == $this->__toString());
00099    }
00100 
00105    public function getAbsoluteURIAsString() {
00106       if (!$this->getScheme() || !$this->getHost()) {
00107          return null;
00108       }
00109 
00110       $uri = $this->getScheme() . "://";
00111       if ($this->getUser()) {
00112          $uri .= $this->getUser();
00113       }
00114       if ($this->getPassword()) {
00115          $uri .= ":" . $this->getPassword();
00116       }
00117       if ($this->getUser() || $this->getPassword()) {
00118          $uri .= "@";
00119       }
00120       $uri .= $this->getHost();
00121       if ($this->getPort() != 80) {
00122          $uri .= ":" . $this->getPort();
00123       }
00124       $uri .= $this->getPath();
00125       if (count($this->getQuery())) {
00126          $uri .= "?" . $this->getQueryString();
00127       }
00128       if ($this->getFragment()) {
00129          $uri .= "#" . $this->getFragment();
00130       }
00131       return $uri;
00132    }
00133 
00139    public function getAbsoluteHostURI() {
00140       $uri = $this->getScheme() . "://";
00141       $uri .= $this->getHost();
00142       if ($this->getPort() != 80) {
00143          $uri .= ":" . $this->getPort();
00144       }
00145       return new DMURI($uri);
00146    }
00147 
00151    public function getFragment() {
00152       return $this->fragment;
00153    }
00154 
00158    public function setFragment($fragment) {
00159       $this->fragment = $fragment;
00160    }
00161 
00165    public function getHost() {
00166       return $this->host;
00167    }
00168 
00172    public function setHost($host) {
00173       $this->host = $host;
00174    }
00175 
00179    public function getPassword() {
00180       return $this->password;
00181    }
00182 
00186    public function setPassword($password) {
00187       $this->password = $password;
00188    }
00189 
00193    public function getPath() {
00194       return $this->path;
00195    }
00196 
00200    public function setPath($path) {
00201       $this->path = $path;
00202    }
00203 
00207    public function getPathComponents() {
00208       return explode("/", trim($this->path, "/"));
00209    }
00210 
00214    public function getPort() {
00215       return $this->port;
00216    }
00217 
00221    public function setPort($port) {
00222       $this->port = (int) $port;
00223    }
00224 
00233    public function addQueryValue($key, $value) {
00234       if (!is_null($value)) {
00235          $this->query[] = array(
00236             'key' => (string) $key,
00237             'value' => (string) $value
00238          );
00239       }
00240    }
00241 
00249    public function getQuery() {
00250       return $this->query;
00251    }
00252 
00257    public function getQueryString() {
00258       $query = array();
00259       foreach ($this->query as $var) {
00260          $query[] = urlencode($var['key']) . "=" . urlencode($var['value']);
00261       }
00262       return implode("&", $query);
00263    }
00264 
00268    public function setQueryString($str) {
00269       $pairs = explode("&", $str);
00270       foreach ($pairs as $pair) {
00271          $kv = explode("=", $pair);
00272          if (count($kv) == 2) {
00273             $this->addQueryValue($kv[0], $kv[1]);
00274          }
00275       }
00276    }
00277 
00286    public function setQueryValue($key, $value) {
00287       $this->unsetQueryKey($key);
00288       $this->addQueryValue($key, $value);
00289    }
00290 
00294    public function getScheme() {
00295       return $this->scheme;
00296    }
00297 
00301    public function setScheme($scheme) {
00302       $this->scheme = $scheme;
00303    }
00304 
00309    public function setString($str) {
00310       $tmp = parse_url($str);
00311 
00312       if (!is_array($tmp)) {
00313          throw new DMIllegalArgumentException(
00314                DMLocalizedString::getString("INVALID_URL"));
00315       }
00316 
00317       if (array_key_exists("scheme", $tmp)) {
00318          $this->setScheme($tmp['scheme']);
00319       }
00320       if (array_key_exists("host", $tmp)) {
00321          $this->setHost($tmp['host']);
00322       }
00323       if (array_key_exists("port", $tmp)) {
00324          $this->setPort($tmp['port']);
00325       }
00326       if (array_key_exists("user", $tmp)) {
00327          $this->setUser($tmp['user']);
00328       }
00329       if (array_key_exists("pass", $tmp)) {
00330          $this->setPassword($tmp['pass']);
00331       }
00332       if (array_key_exists("path", $tmp)) {
00333          $this->setPath($tmp['path']);
00334       }
00335       $this->unsetQuery();
00336       if (array_key_exists("query", $tmp)) {
00337          $q = explode("&", $tmp['query']);
00338          foreach ($q as $kv) {
00339             $parts = explode("=", $kv);
00340             if (count($parts) == 2) {
00341                $this->addQueryValue(
00342                      urldecode($parts[0]), urldecode($parts[1]));
00343             }
00344          }
00345       }
00346       if (array_key_exists("fragment", $tmp)) {
00347          $this->setFragment($tmp['fragment']);
00348       }
00349    }
00350 
00354    public function getUser() {
00355       return $this->user;
00356    }
00357 
00361    public function setUser($user) {
00362       $this->user = $user;
00363    }
00364 
00368    public function isValid() {
00369       $valid = true;
00370       if (!filter_var($this->__toString(), FILTER_VALIDATE_URL,
00371             FILTER_FLAG_SCHEME_REQUIRED)) {
00372          $valid = false;
00373       }
00374       if (!$this->getScheme()) {
00375          $valid = false;
00376       }
00377       if ($this->getPassword() && !$this->getUser()) {
00378          $valid = false;
00379       }
00380       return $valid;
00381    }
00382 
00389    public function getQueryValue($key) {
00390       foreach ($this->query as $q) {
00391          if ($q['key'] == $key) {
00392             return $q['value'];
00393          }
00394       }
00395       return null;
00396    }
00397 
00404    public function getQueryValues($key) {
00405       $values = array();
00406       foreach ($this->query as $q) {
00407          if ($q['key'] == $key) {
00408             $values[] = $q['value'];
00409          }
00410       }
00411       return $values;
00412    }
00413 
00417    public function unsetQuery() {
00418       $this->query = array();
00419    }
00420 
00426    public function unsetQueryKey($key) {
00427       $count = count($this->query);
00428       for ($i = 0; $i < $count; $i++) {
00429          if (array_key_exists($i, $this->query)
00430                && $this->query[$i]['key'] == $key) {
00431             unset($this->query[$i]);
00432          }
00433       }
00434    }
00435 
00436 }
 All Data Structures Functions Variables