Skip to content
Permalink
Browse files

Refactoring how prefers() works. Tests updated.

Removing support for array args in setContent().
  • Loading branch information...
markstory committed Jul 1, 2010
1 parent ba287ec commit 427e859b521a70790542fb5036c9f80b0e90eeb4
@@ -384,10 +384,6 @@ public function getAjaxVersion() {
* @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 @@ function accepts($type = null) {
* 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 @@ public function requestedWith($type = null) {
*/
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]);
return ($types[0] == $accepts[0]);
}
$accepts = array();
foreach ($types as $type) {
if (in_array($type, $accept)) {
$accepts[] = $type;
}
}
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];
}
/**
@@ -562,6 +562,8 @@ function testPrefers() {
$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 @@ function testCustomContent() {
$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');
}
/**

0 comments on commit 427e859

Please sign in to comment.
You can’t perform that action at this time.