1<?php 2 3namespace iplx\Http; 4 5use Psr\Http\Message\ResponseInterface; 6 7/** 8 * A HTTP response 9 */ 10class Response implements ResponseInterface 11{ 12 use MessageTrait; 13 14 /** 15 * Status code of the response 16 * 17 * @var int 18 */ 19 protected $statusCode; 20 21 /** 22 * Response status reason phrase 23 * 24 * @var string 25 */ 26 protected $reasonPhrase = ''; 27 28 /** 29 * Create a new HTTP response 30 * 31 * @param int $statusCode Response status code 32 * @param array $headers Response headers 33 * @param string $body Response body 34 * @param string $protocolVersion Protocol version 35 * @param string $reasonPhrase Response status reason phrase 36 */ 37 public function __construct($statusCode = 200, array $headers = [], $body = null, $protocolVersion = '1.1', $reasonPhrase = '') 38 { 39 $this->statusCode = $statusCode; 40 $this->setHeaders($headers); 41 $this->body = Stream::create($body); 42 $this->protocolVersion = $protocolVersion; 43 $this->reasonPhrase = $reasonPhrase; 44 } 45 46 public function getStatusCode() 47 { 48 return $this->statusCode; 49 } 50 51 public function withStatus($code, $reasonPhrase = '') 52 { 53 $response = clone $this; 54 $response->statusCode = $code; 55 $response->reasonPhrase = $reasonPhrase; 56 57 return $response; 58 } 59 60 public function getReasonPhrase() 61 { 62 return $this->reasonPhrase; 63 } 64} 65