Bug #9455: Add ability to return relative URL when converting to string
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 21 Jan 2011 19:44:37 +0000 (12:44 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 21 Jan 2011 19:51:53 +0000 (12:51 -0700)
framework/Url/lib/Horde/Url.php
framework/Url/package.xml
framework/Url/test/Horde/Url/TostringTest.php [new file with mode: 0644]

index 578c03a..bdfa902 100644 (file)
@@ -213,12 +213,13 @@ class Horde_Url
     /**
      * Creates the full URL string.
      *
-     * @param boolean $raw  Whether to output the URL in the raw URL format or
-     *                      HTML-encoded.
+     * @param boolean $raw   Whether to output the URL in the raw URL format
+     *                       or HTML-encoded.
+     * @param boolean $full  Output the full URL?
      *
      * @return string  The string representation of this object.
      */
-    public function toString($raw = false)
+    public function toString($raw = false, $full = true)
     {
         if ($this->toStringCallback) {
             $callback = $this->toStringCallback;
@@ -243,7 +244,10 @@ class Horde_Url
             }
         }
 
-        $url = $this->url;
+        $url = $full
+            ? $this->url
+            : parse_url($this->url, PHP_URL_PATH);
+
         if (strlen($this->pathInfo)) {
             $url = rtrim($url, '/');
             $url .= '/' . $this->pathInfo;
index 4b28cb5..a4936cc 100644 (file)
@@ -28,7 +28,8 @@
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
  <notes>
-* Add Horde_Url_Exception::.
+ * Add ability to return relative URL when converting to string.
+ * Add Horde_Url_Exception::.
  * Add Horde_Url::redirect().
  * Add Horde_Url::unique().
  * Add support for callback function for toString conversion.
@@ -56,6 +57,7 @@
       <file name="RawTest.php" role="test" />
       <file name="RedirectTest.php" role="test" />
       <file name="RemoveTest.php" role="test" />
+      <file name="TostringTest.php" role="test" />
      </dir> <!-- /test/Horde/Url -->
     </dir> <!-- /test/Horde -->
    </dir> <!-- /test -->
@@ -86,6 +88,7 @@
    <install as="Horde/Url/RawTest.php" name="test/Horde/Url/RawTest.php" />
    <install as="Horde/Url/RedirectTest.php" name="test/Horde/Url/RedirectTest.php" />
    <install as="Horde/Url/RemoveTest.php" name="test/Horde/Url/RemoveTest.php" />
+   <install as="Horde/Url/TostringTest.php" name="test/Horde/Url/TostringTest.php" />
   </filelist>
  </phprelease>
  <changelog>
diff --git a/framework/Url/test/Horde/Url/TostringTest.php b/framework/Url/test/Horde/Url/TostringTest.php
new file mode 100644 (file)
index 0000000..b8944cb
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+/**
+ * @author     Michael Slusarz <slusarz@horde.org>
+ * @license    http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @category   Horde
+ * @package    Url
+ * @subpackage UnitTests
+ */
+
+class Horde_Url_TostringTest extends PHPUnit_Framework_TestCase
+{
+    public function testFullUrl()
+    {
+        $url = new Horde_Url('http://example.com/test?foo=1&bar=2');
+        $this->assertEquals(
+            '/test?foo=1&bar=2',
+            $url->toString(true, false)
+        );
+        $this->assertEquals(
+            'http://example.com/test?foo=1&bar=2',
+            $url->toString(true, true)
+        );
+    }
+}