From 7a42b804b9c6737e2a4a1d6680bb1d147b1c2f41 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Mon, 12 Jul 2010 19:49:03 -0600 Subject: [PATCH] Add support for callback when converting URL to string. --- framework/Url/lib/Horde/Url.php | 15 +++++++++++++++ framework/Url/package.xml | 3 ++- framework/Url/test/Horde/Url/CallbackTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 framework/Url/test/Horde/Url/CallbackTest.php diff --git a/framework/Url/lib/Horde/Url.php b/framework/Url/lib/Horde/Url.php index 75201eafc..9ba6adb25 100644 --- a/framework/Url/lib/Horde/Url.php +++ b/framework/Url/lib/Horde/Url.php @@ -64,6 +64,13 @@ class Horde_Url public $anchor = ''; /** + * A callback function to use when converting to a string. + * + * @var callback + */ + public $toStringCallback; + + /** * Constructor. * * @param string $url The basic URL, with or without query parameters. @@ -200,6 +207,14 @@ class Horde_Url */ public function toString($raw = false) { + if ($this->toStringCallback) { + $callback = $this->toStringCallback; + $this->toStringCallback = null; + $ret = call_user_func($callback, $this); + $this->toStringCallback = $callback; + return $ret; + } + $url_params = array(); foreach ($this->parameters as $parameter => $value) { if (is_array($value)) { diff --git a/framework/Url/package.xml b/framework/Url/package.xml index ab63d5167..7f3a43fd5 100644 --- a/framework/Url/package.xml +++ b/framework/Url/package.xml @@ -29,7 +29,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> beta LGPL - * Add support for URL anchors. + * Add support for callback function for toString conversion. + * Add support for URL anchors. * Added Horde_Url::uriB64Encode() and Horde_Url::uriB64Decode(). * Initial package. diff --git a/framework/Url/test/Horde/Url/CallbackTest.php b/framework/Url/test/Horde/Url/CallbackTest.php new file mode 100644 index 000000000..1d7c11a0e --- /dev/null +++ b/framework/Url/test/Horde/Url/CallbackTest.php @@ -0,0 +1,24 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @category Horde + * @package Url + * @subpackage UnitTests + */ + +class Horde_Url_CallbackTest extends PHPUnit_Framework_TestCase +{ + public function testRemoveRaw() + { + $url = new Horde_Url('test?bar=2'); + $url->toStringCallback = array($this, 'callbackToString'); + $this->assertEquals('FOOtest?bar=2BAR', (string)$url); + } + + public function callbackToString($url) + { + return 'FOO' . (string)$url . 'BAR'; + } + +} -- 2.11.0