Permalink
Browse files

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
  • Loading branch information...
1 parent d809b14 commit d4a3594e4f53eca1090eb8ab4f30e03304bb305e @markstory markstory committed Apr 2, 2013
Showing with 28 additions and 3 deletions.
  1. +10 −3 lib/Cake/Network/CakeRequest.php
  2. +18 −0 lib/Cake/Test/Case/Network/CakeRequestTest.php
View
13 lib/Cake/Network/CakeRequest.php
@@ -466,14 +466,21 @@ public function __isset($name) {
}
/**
- * 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;
View
18 lib/Cake/Test/Case/Network/CakeRequestTest.php
@@ -724,6 +724,24 @@ public function testIsHttpMethods() {
}
/**
+ * 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

0 comments on commit d4a3594

Please sign in to comment.