From 427e859b521a70790542fb5036c9f80b0e90eeb4 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 1 Jul 2010 00:46:28 -0400 Subject: [PATCH] Refactoring how prefers() works. Tests updated. Removing support for array args in setContent(). --- .../controller/components/request_handler.php | 48 ++++++------------- .../components/request_handler.test.php | 7 +-- 2 files changed, 16 insertions(+), 39 deletions(-) diff --git a/cake/libs/controller/components/request_handler.php b/cake/libs/controller/components/request_handler.php index 1829cd3a6..1956afa2a 100644 --- a/cake/libs/controller/components/request_handler.php +++ b/cake/libs/controller/components/request_handler.php @@ -384,10 +384,6 @@ class RequestHandlerComponent extends Object { * @return void */ public function setContent($name, $type = null) { - if (is_array($name)) { - $this->__requestContent = array_merge($this->__requestContent, $name); - return; - } $this->__requestContent[$name] = $type; } @@ -461,13 +457,15 @@ class RequestHandlerComponent extends Object { * Determines the content type of the data the client has sent (i.e. in a POST request) * * @param mixed $type Can be null (or no parameter), a string type name, or an array of types - * @return mixed + * @return mixed If a single type is supplied a boolean will be returned. If no type is provided + * The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type + * in the request content type will be returned. */ public function requestedWith($type = null) { if (!$this->request->is('post') && !$this->request->is('put')) { return null; } - + list($contentType) = explode(';', env('CONTENT_TYPE')); if ($type == null) { return $this->mapType($contentType); @@ -500,51 +498,33 @@ class RequestHandlerComponent extends Object { */ function prefers($type = null) { $this->__initializeTypes(); - $accept = $this->accepts(); + $accepts = $this->accepts(); if ($type == null) { if (empty($this->ext)) { - if (is_array($accept)) { - return $accept[0]; + if (is_array($accepts)) { + return $accepts[0]; } - return $accept; + return $accepts; } return $this->ext; } - $types = $type; - if (is_string($type)) { - $types = array($type); - } + $types = (array)$type; if (count($types) === 1) { if (!empty($this->ext)) { return ($types[0] == $this->ext); } - return ($types[0] == $accept[0]); - } - $accepts = array(); - - foreach ($types as $type) { - if (in_array($type, $accept)) { - $accepts[] = $type; - } + return ($types[0] == $accepts[0]); } - if (count($accepts) === 0) { + + $intersect = array_values(array_intersect($accepts, $types)); + if (empty($intersect)) { return false; - } elseif (count($types) === 1) { - return ($types[0] === $accepts[0]); - } elseif (count($accepts) === 1) { - return $accepts[0]; } - - $acceptedTypes = array(); - foreach ($this->__acceptTypes as $type) { - $acceptedTypes[] = $this->mapType($type); - } - $accepts = array_intersect($acceptedTypes, $accepts); - return $accepts[0]; + return $intersect[0]; } /** diff --git a/cake/tests/cases/libs/controller/components/request_handler.test.php b/cake/tests/cases/libs/controller/components/request_handler.test.php index acfcd737b..37a084ef0 100644 --- a/cake/tests/cases/libs/controller/components/request_handler.test.php +++ b/cake/tests/cases/libs/controller/components/request_handler.test.php @@ -562,6 +562,8 @@ class RequestHandlerComponentTest extends CakeTestCase { $this->assertEqual($this->RequestHandler->prefers(), 'rss'); $this->assertFalse($this->RequestHandler->prefers('xml')); $this->assertEqual($this->RequestHandler->prefers(array('js', 'xml', 'xhtml')), 'xml'); + $this->assertFalse($this->RequestHandler->prefers(array('red', 'blue'))); + $this->assertEqual($this->RequestHandler->prefers(array('js', 'json', 'xhtml')), 'xhtml'); $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; $this->_init(); @@ -585,11 +587,6 @@ class RequestHandlerComponentTest extends CakeTestCase { $this->RequestHandler->setContent('mobile', 'text/x-mobile'); $this->RequestHandler->startup($this->Controller); $this->assertEqual($this->RequestHandler->prefers(), 'mobile'); - - $this->_init(); - $this->RequestHandler->setContent(array('mobile' => 'text/x-mobile')); - $this->RequestHandler->startup($this->Controller); - $this->assertEqual($this->RequestHandler->prefers(), 'mobile'); } /**