Add support for URL anchors to Horde_Url.
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 8 Jun 2010 20:21:35 +0000 (14:21 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 8 Jun 2010 21:16:21 +0000 (15:16 -0600)
framework/Url/lib/Horde/Url.php
framework/Url/package.xml

index 427263b..641b752 100644 (file)
@@ -56,6 +56,13 @@ class Horde_Url
     public $pathInfo;
 
     /**
+     * The anchor string.
+     *
+     * @var string
+     */
+    public $anchor = '';
+
+    /**
      * Constructor.
      *
      * @param string $url   The basic URL, with or without query parameters.
@@ -64,17 +71,23 @@ class Horde_Url
      */
     public function __construct($url, $raw = null)
     {
-        /* @todo Remove if all code has been moved to Horde_Url. */
         if ($url instanceof Horde_Url) {
-            $this->url = $url->url;
+            $this->anchor = $url->anchor;
             $this->parameters = $url->parameters;
             $this->pathInfo = $url->pathInfo;
             $this->raw = is_null($raw) ? $url->raw : $raw;
+            $this->url = $url->url;
             return;
         }
 
-        if (strpos($url, '?') !== false) {
-            list($url, $query) = explode('?', $url);
+        if (($pos = strrpos($url, '#')) !== false) {
+            $this->anchor = urldecode(substr($url, $pos + 1));
+            $url = substr($url, 0, $pos);
+        }
+
+        if (($pos = strrpos($url, '?')) !== false) {
+            $query = substr($url, $pos + 1);
+            $url = substr($url, 0, $pos);
 
             /* Check if the argument separator has been already
              * htmlentities-ized in the URL. */
@@ -208,6 +221,9 @@ class Horde_Url
         if (count($url_params)) {
             $url .= '?' . implode($raw ? '&' : '&amp;', $url_params);
         }
+        if ($this->anchor) {
+            $url .= '#' . rawurlencode($this->anchor);
+        }
 
         return $url;
     }
index a7c0bee..ab63d51 100644 (file)
@@ -29,7 +29,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Added Horde_Url::uriB64Encode() and Horde_Url::uriB64Decode().
+ <notes>* Add support for URL anchors.
+ * Added Horde_Url::uriB64Encode() and Horde_Url::uriB64Decode().
  * Initial package.
  </notes>
  <contents>