Phly\Http\Response\Serializer::toString PHP Метод

toString() публичный статический Метод

Create a string representation of a response.
public static toString ( Psr\Http\Message\ResponseInterface $response ) : string
$response Psr\Http\Message\ResponseInterface
Результат string
    public static function toString(ResponseInterface $response)
    {
        $reasonPhrase = $response->getReasonPhrase();
        $headers = self::serializeHeaders($response->getHeaders());
        $body = (string) $response->getBody();
        $format = 'HTTP/%s %d%s%s%s';
        if (!empty($headers)) {
            $headers = "\r\n" . $headers;
        }
        if (!empty($body)) {
            $headers .= "\r\n\r\n";
        }
        return sprintf($format, $response->getProtocolVersion(), $response->getStatusCode(), $reasonPhrase ? ' ' . $reasonPhrase : '', $headers, $body);
    }

Usage Example

Пример #1
0
 public function testOmitsReasonPhraseFromStatusLineIfEmpty()
 {
     $response = (new Response())->withStatus(299)->withAddedHeader('X-Foo-Bar', 'Baz');
     $response->getBody()->write('Content!');
     $message = Serializer::toString($response);
     $this->assertContains("HTTP/1.1 299\r\n", $message);
 }