Browse Source

Merge pull request #5392 from cakephp/3.0-viewvars

Chainable set()
José Lorenzo Rodríguez 11 years ago
parent
commit
43f053fb0c
2 changed files with 20 additions and 7 deletions
  1. 8 7
      src/View/ViewVarsTrait.php
  2. 12 0
      tests/TestCase/View/ViewVarsTraitTest.php

+ 8 - 7
src/View/ViewVarsTrait.php

@@ -93,24 +93,25 @@ trait ViewVarsTrait {
 	}
 
 /**
- * Saves a variable for use inside a template.
+ * Saves a variable or an associative array of variables for use inside a template.
  *
  * @param string|array $name A string or an array of data.
- * @param string|array|null $val Value in case $name is a string (which then works as the key).
+ * @param string|array|null $value Value in case $name is a string (which then works as the key).
  *   Unused if $name is an associative array, otherwise serves as the values to $name's keys.
- * @return void
+ * @return $this
  */
-	public function set($name, $val = null) {
+	public function set($name, $value = null) {
 		if (is_array($name)) {
-			if (is_array($val)) {
-				$data = array_combine($name, $val);
+			if (is_array($value)) {
+				$data = array_combine($name, $value);
 			} else {
 				$data = $name;
 			}
 		} else {
-			$data = [$name => $val];
+			$data = [$name => $value];
 		}
 		$this->viewVars = $data + $this->viewVars;
+		return $this;
 	}
 
 /**

+ 12 - 0
tests/TestCase/View/ViewVarsTraitTest.php

@@ -59,6 +59,18 @@ class ViewVarsTraitTest extends TestCase {
 	}
 
 /**
+ * test chainable set()
+ *
+ * @return void
+ */
+	public function testSetChained() {
+		$result = $this->subject->set('testing', 'value')
+			->set('foo', 'bar');
+		$this->assertSame($this->subject, $result);
+		$this->assertEquals(['testing' => 'value', 'foo' => 'bar'], $this->subject->viewVars);
+	}
+
+/**
  * test set() with 2 params in combine mode
  *
  * @return void