Skip to content

Commit

Permalink
[HttpFoundation] added HeaderBag::__toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 15, 2011
1 parent dcb4ef6 commit e81b88c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
21 changes: 21 additions & 0 deletions src/Symfony/Component/HttpFoundation/HeaderBag.php
Expand Up @@ -37,6 +37,27 @@ public function __construct(array $headers = array())
}
}

/**
* Returns the headers as a string.
*
* @return string The headers
*/
public function __toString()
{
$beautifier = function ($name) {
return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", ucfirst($name));
};

$content = '';
foreach ($this->headers as $name => $values) {
foreach ($values as $value) {
$content .= sprintf("%s: %s\r\n", $beautifier($name), $value);
}
}

return $content;
}

/**
* Returns the headers.
*
Expand Down
21 changes: 4 additions & 17 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -288,23 +288,10 @@ public function __clone()
*/
public function __toString()
{
// status
$content = sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n";

$beautifier = function ($name) {
return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", ucfirst($name));
};

// headers
foreach ($this->headers->all() as $name => $values) {
foreach ($values as $value) {
$content .= sprintf("%s: %s\r\n", $beautifier($name), $value);
}
}

$content .= "\r\n".$this->getContent();

return $content;
return
sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n".
$this->headers."\r\n".
$this->getContent();
}

/**
Expand Down
21 changes: 4 additions & 17 deletions src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -98,23 +98,10 @@ public function __toString()
{
$this->fixContentType();

// status
$content = sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n";

$beautifier = function ($name) {
return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", ucfirst($name));
};

// headers
foreach ($this->headers->all() as $name => $values) {
foreach ($values as $value) {
$content .= sprintf("%s: %s\r\n", $beautifier($name), $value);
}
}

$content .= "\r\n".$this->getContent();

return $content;
return
sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n".
$this->headers."\r\n".
$this->getContent();
}

/**
Expand Down

0 comments on commit e81b88c

Please sign in to comment.