[FEATURE] Integrate possibility to build URL
authorAndreas Wolf <andreas.wolf@ikt-werk.de>
Wed, 8 Feb 2012 13:05:53 +0000 (14:05 +0100)
committerOliver Hader <oliver@typo3.org>
Wed, 8 Feb 2012 18:27:23 +0000 (19:27 +0100)
The PHP function parse_url() allows to split up URLs into
separate parts. However, TYPO3 lacks of the possibility to
build an URL with the same arguments.

Example (round-trip):
$url = t3lib_utility_Http::buildUrl(
  parse_url('http://typo3.org/')
);

Change-Id: Ie56128261527a2b5a568ca7b05f522b13ac3cf98
Resolves: #33767
Releases: 4.7
Reviewed-on: http://review.typo3.org/8909
Reviewed-by: Oliver Hader
Tested-by: Oliver Hader
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
t3lib/utility/class.t3lib_utility_http.php
tests/t3lib/utility/class.t3lib_utility_httpTest.php [new file with mode: 0644]

index e66d5da..aa6925c 100644 (file)
@@ -97,6 +97,20 @@ class t3lib_utility_Http {
 
                exit;
        }
+
+       /**
+        * Builds a URL string from an array with the URL parts, as e.g. output by parse_url().
+        *
+        * @param array $urlParts
+        * @return string
+        * @see http://www.php.net/parse_url
+        */
+       public static function buildUrl(array $urlParts) {
+               return (isset($urlParts['scheme']) ? $urlParts['scheme'] . '://' : '')
+                       . (isset($urlParts['user']) ? $urlParts['user'] . (isset($urlParts['pass']) ? ':' . $urlParts['pass'] : '') . '@' : '')
+                       . (isset($urlParts['host']) ? $urlParts['host'] : '') . (isset($urlParts['path']) ? $urlParts['path'] : '')
+                       . (isset($urlParts['query']) ? '?' . $urlParts['query'] : '') . (isset($urlParts['fragment']) ? '#' . $urlParts['fragment'] : '');
+       }
 }
 
 ?>
\ No newline at end of file
diff --git a/tests/t3lib/utility/class.t3lib_utility_httpTest.php b/tests/t3lib/utility/class.t3lib_utility_httpTest.php
new file mode 100644 (file)
index 0000000..77c2457
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+/***************************************************************
+ * Copyright notice
+ *
+ * (c) 2012 Oliver Hader <oliver.hader@typo3.org>
+ * All rights reserved
+ *
+ * This script is part of the TYPO3 project. The TYPO3 project is
+ * free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The GNU General Public License can be found at
+ * http://www.gnu.org/copyleft/gpl.html.
+ * A copy is found in the textfile GPL.txt and important notices to the license
+ * from the author is found in LICENSE.txt distributed with these scripts.
+ *
+ *
+ * This script is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * Testcase for class t3lib_utility_Http
+ *
+ * @author Oliver Hader <oliver.hader@typo3.org>
+ *
+ * @package TYPO3
+ * @subpackage t3lib
+ */
+
+class t3lib_utility_HttpTest extends tx_phpunit_testcase {
+       /**
+        * @param array $urlParts
+        * @param string $expected
+        * @dataProvider isUrlBuiltCorrectlyDataProvider
+        * @test
+        */
+       public function isUrlBuiltCorrectly(array $urlParts, $expected) {
+               $url = t3lib_utility_Http::buildUrl($urlParts);
+               $this->assertEquals($expected, $url);
+       }
+
+       /**
+        * @return array
+        */
+       public function isUrlBuiltCorrectlyDataProvider() {
+               return array(
+                       'rebuild url withouth scheme' => array(
+                               parse_url('typo3.org/path/index.php'),
+                               'typo3.org/path/index.php',
+                       ),
+                       'rebuild url with scheme' => array(
+                               parse_url('http://typo3.org/path/index.php'),
+                               'http://typo3.org/path/index.php',
+                       ),
+                       'rebuild url with all properties' => array(
+                               parse_url('http://editor:secret@typo3.org/path/index.php?query=data#fragment'),
+                               'http://editor:secret@typo3.org/path/index.php?query=data#fragment',
+                       ),
+                       'url without username, but password' => array(
+                               array(
+                                       'scheme' => 'http',
+                                       'pass' => 'secrept',
+                                       'host' => 'typo3.org'
+                               ),
+                               'http://typo3.org',
+                       )
+               );
+       }
+}
+
+?>
\ No newline at end of file