Browse Source

Add support for is() with multiple types.

Add ability to check if a request is one of a set number of types
by providing an array. If any type matches, then the method returns
true.

Refs #3714
mark_story 13 years ago
parent
commit
d4a3594e4f
2 changed files with 28 additions and 3 deletions
  1. 10 3
      lib/Cake/Network/CakeRequest.php
  2. 18 0
      lib/Cake/Test/Case/Network/CakeRequestTest.php

+ 10 - 3
lib/Cake/Network/CakeRequest.php

@@ -466,14 +466,21 @@ class CakeRequest implements ArrayAccess {
 	}
 
 /**
- * Check whether or not a Request is a certain type. Uses the built in detection rules
- * as well as additional rules defined with CakeRequest::addDetector(). Any detector can be called
+ * Check whether or not a Request is a certain type.
+ *
+ * Uses the built in detection rules as well as additional rules
+ * defined with CakeRequest::addDetector(). Any detector can be called
  * as `is($type)` or `is$Type()`.
  *
- * @param string $type The type of request you want to check.
+ * @param string|array $type The type of request you want to check. If an array
+ *   this method will return true if the request matches any type.
  * @return boolean Whether or not the request is the type you are checking.
  */
 	public function is($type) {
+		if (is_array($type)) {
+			$result = array_map(array($this, 'is'), $type);
+			return count(array_filter($result)) > 0;
+		}
 		$type = strtolower($type);
 		if (!isset($this->_detectors[$type])) {
 			return false;

+ 18 - 0
lib/Cake/Test/Case/Network/CakeRequestTest.php

@@ -724,6 +724,24 @@ class CakeRequestTest extends CakeTestCase {
 	}
 
 /**
+ * Test is() with multiple types.
+ *
+ * @return void
+ */
+	public function testIsMultiple() {
+		$request = new CakeRequest('some/path');
+
+		$_SERVER['REQUEST_METHOD'] = 'GET';
+		$this->assertTrue($request->is(array('get', 'post')));
+
+		$_SERVER['REQUEST_METHOD'] = 'POST';
+		$this->assertTrue($request->is(array('get', 'post')));
+
+		$_SERVER['REQUEST_METHOD'] = 'PUT';
+		$this->assertFalse($request->is(array('get', 'post')));
+	}
+
+/**
  * test the method() method.
  *
  * @return void