浏览代码

Add array verions of Url.

dereuromark 9 年之前
父节点
当前提交
b196517057
共有 2 个文件被更改,包括 74 次插入25 次删除
  1. 45 25
      src/Controller/Component/UrlComponent.php
  2. 29 0
      tests/TestCase/Controller/Component/UrlComponentTest.php

+ 45 - 25
src/Controller/Component/UrlComponent.php

@@ -14,25 +14,21 @@ use Shim\Controller\Component\Component;
 class UrlComponent extends Component {
 
 	/**
-	 * Returns a URL based on provided parameters.
-	 *
-	 * ### Options:
-	 *
-	 * - `fullBase`: If true, the full base URL will be prepended to the result
-	 *
-	 * @param string|array|null $url Either a relative string url like `/products/view/23` or
-	 *    an array of URL parameters. Using an array for URLs will allow you to leverage
-	 *    the reverse routing features of CakePHP.
-	 * @param array $options Array of options
-	 * @return string Full translated URL with base path.
+	 * @param array $url
+	 * @return array
 	 */
-	public function build($url = null, array $options = []) {
-		$defaults = [
-			'fullBase' => false,
-		];
-		$options += $defaults;
+	public function resetArray(array $url) {
+		$url += $this->defaults();
 
-		$url = Router::url($url, $options['fullBase']);
+		return $url;
+	}
+
+	/**
+	 * @param array $url
+	 * @return array
+	 */
+	public function completeArray(array $url) {
+		$url = $this->addQueryStrings($url);
 
 		return $url;
 	}
@@ -56,6 +52,23 @@ class UrlComponent extends Component {
 	}
 
 	/**
+	 * Returns a URL based on provided parameters.
+	 *
+	 * Can only add query strings for array URLs.
+	 *
+	 * @param string|array|null $url URL.
+	 * @param bool $full If true, the full base URL will be prepended to the result
+	 * @return string Full translated URL with base path.
+	 */
+	public function complete($url = null, $full = false) {
+		if (is_array($url)) {
+			$url = $this->addQueryStrings($url);
+		}
+
+		return Router::url($url, $full);
+	}
+
+	/**
 	 * @return array
 	 */
 	public function defaults() {
@@ -68,18 +81,25 @@ class UrlComponent extends Component {
 	/**
 	 * Returns a URL based on provided parameters.
 	 *
-	 * Can only add query strings for array URLs.
+	 * ### Options:
 	 *
-	 * @param string|array|null $url URL.
-	 * @param bool $full If true, the full base URL will be prepended to the result
+	 * - `fullBase`: If true, the full base URL will be prepended to the result
+	 *
+	 * @param string|array|null $url Either a relative string url like `/products/view/23` or
+	 *    an array of URL parameters. Using an array for URLs will allow you to leverage
+	 *    the reverse routing features of CakePHP.
+	 * @param array $options Array of options
 	 * @return string Full translated URL with base path.
 	 */
-	public function complete($url = null, $full = false) {
-		if (is_array($url)) {
-			$url = $this->addQueryStrings($url);
-		}
+	public function build($url = null, array $options = []) {
+		$defaults = [
+			'fullBase' => false,
+		];
+		$options += $defaults;
 
-		return Router::url($url, $full);
+		$url = Router::url($url, $options['fullBase']);
+
+		return $url;
 	}
 
 	/**

+ 29 - 0
tests/TestCase/Controller/Component/UrlComponentTest.php

@@ -73,6 +73,35 @@ class UrlComponentTest extends TestCase {
 	/**
 	 * @return void
 	 */
+	public function testResetArray() {
+		$result = $this->Controller->Url->resetArray(['controller' => 'foobar', 'action' => 'test']);
+		$expected = [
+			'controller' => 'foobar',
+			'action' => 'test',
+			'prefix' => false,
+			'plugin' => false,
+		];
+		$this->assertSame($expected, $result);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testCompleteArray() {
+		$this->Controller->Url->request->query['x'] = 'y';
+
+		$result = $this->Controller->Url->completeArray(['controller' => 'foobar', 'action' => 'test']);
+		$expected = [
+			'controller' => 'foobar',
+			'action' => 'test',
+			'?' => ['x' => 'y']
+		];
+		$this->assertSame($expected, $result);
+	}
+
+	/**
+	 * @return void
+	 */
 	public function testReset() {
 		Router::connect('/:controller/:action/*');