浏览代码

[HttpFoundation] added HeaderBag::__toString()

Fabien Potencier 14 年之前
父节点
当前提交
e81b88c576

+ 21 - 0
src/Symfony/Component/HttpFoundation/HeaderBag.php

@@ -37,6 +37,27 @@ class HeaderBag
         }
     }
 
+    /**
+     * 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.
      *

+ 4 - 17
src/Symfony/Component/HttpFoundation/Request.php

@@ -288,23 +288,10 @@ class Request
      */
     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();
     }
 
     /**

+ 4 - 17
src/Symfony/Component/HttpFoundation/Response.php

@@ -98,23 +98,10 @@ class Response
     {
         $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();
     }
 
     /**