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
This commit is contained in:
mark_story 2013-04-01 21:41:39 -04:00
parent d809b1480e
commit d4a3594e4f
2 changed files with 28 additions and 3 deletions

View file

@ -466,14 +466,21 @@ class CakeRequest implements ArrayAccess {
} }
/** /**
* Check whether or not a Request is a certain type. Uses the built in detection rules * Check whether or not a Request is a certain type.
* as well as additional rules defined with CakeRequest::addDetector(). Any detector can be called *
* 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()`. * 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. * @return boolean Whether or not the request is the type you are checking.
*/ */
public function is($type) { public function is($type) {
if (is_array($type)) {
$result = array_map(array($this, 'is'), $type);
return count(array_filter($result)) > 0;
}
$type = strtolower($type); $type = strtolower($type);
if (!isset($this->_detectors[$type])) { if (!isset($this->_detectors[$type])) {
return false; return false;

View file

@ -723,6 +723,24 @@ class CakeRequestTest extends CakeTestCase {
$this->assertFalse($request->is('delete')); $this->assertFalse($request->is('delete'));
} }
/**
* 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. * test the method() method.
* *