Skip to content

Commit

Permalink
Refactoring how prefers() works. Tests updated.
Browse files Browse the repository at this point in the history
Removing support for array args in setContent().
  • Loading branch information
markstory committed Jul 1, 2010
1 parent ba287ec commit 427e859
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
48 changes: 14 additions & 34 deletions cake/libs/controller/components/request_handler.php
Expand Up @@ -384,10 +384,6 @@ public function getAjaxVersion() {
* @return void * @return void
*/ */
public function setContent($name, $type = null) { public function setContent($name, $type = null) {
if (is_array($name)) {
$this->__requestContent = array_merge($this->__requestContent, $name);
return;
}
$this->__requestContent[$name] = $type; $this->__requestContent[$name] = $type;
} }


Expand Down Expand Up @@ -461,13 +457,15 @@ function accepts($type = null) {
* Determines the content type of the data the client has sent (i.e. in a POST request) * 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 * @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) { public function requestedWith($type = null) {
if (!$this->request->is('post') && !$this->request->is('put')) { if (!$this->request->is('post') && !$this->request->is('put')) {
return null; return null;
} }

list($contentType) = explode(';', env('CONTENT_TYPE')); list($contentType) = explode(';', env('CONTENT_TYPE'));
if ($type == null) { if ($type == null) {
return $this->mapType($contentType); return $this->mapType($contentType);
Expand Down Expand Up @@ -500,51 +498,33 @@ public function requestedWith($type = null) {
*/ */
function prefers($type = null) { function prefers($type = null) {
$this->__initializeTypes(); $this->__initializeTypes();
$accept = $this->accepts(); $accepts = $this->accepts();


if ($type == null) { if ($type == null) {
if (empty($this->ext)) { if (empty($this->ext)) {
if (is_array($accept)) { if (is_array($accepts)) {
return $accept[0]; return $accepts[0];
} }
return $accept; return $accepts;
} }
return $this->ext; return $this->ext;
} }


$types = $type; $types = (array)$type;
if (is_string($type)) {
$types = array($type);
}


if (count($types) === 1) { if (count($types) === 1) {
if (!empty($this->ext)) { if (!empty($this->ext)) {
return ($types[0] == $this->ext); return ($types[0] == $this->ext);
} }
return ($types[0] == $accept[0]); return ($types[0] == $accepts[0]);
} }
$accepts = array();


foreach ($types as $type) {
if (in_array($type, $accept)) { $intersect = array_values(array_intersect($accepts, $types));
$accepts[] = $type; if (empty($intersect)) {
}
}

if (count($accepts) === 0) {
return false; 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 $intersect[0];
return $accepts[0];
} }


/** /**
Expand Down
Expand Up @@ -562,6 +562,8 @@ function testPrefers() {
$this->assertEqual($this->RequestHandler->prefers(), 'rss'); $this->assertEqual($this->RequestHandler->prefers(), 'rss');
$this->assertFalse($this->RequestHandler->prefers('xml')); $this->assertFalse($this->RequestHandler->prefers('xml'));
$this->assertEqual($this->RequestHandler->prefers(array('js', 'xml', 'xhtml')), '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'; $_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(); $this->_init();
Expand All @@ -585,11 +587,6 @@ function testCustomContent() {
$this->RequestHandler->setContent('mobile', 'text/x-mobile'); $this->RequestHandler->setContent('mobile', 'text/x-mobile');
$this->RequestHandler->startup($this->Controller); $this->RequestHandler->startup($this->Controller);
$this->assertEqual($this->RequestHandler->prefers(), 'mobile'); $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');
} }


/** /**
Expand Down

0 comments on commit 427e859

Please sign in to comment.