示例#1
0
文件: Response.php 项目: kaiwa/http
 /**
  * Class init.
  *
  * @param string $body
  * @param int    $status
  * @param array  $headers
  */
 public function __construct($body = 'php://memory', $status = 200, array $headers = array())
 {
     if (!$body instanceof StreamInterface) {
         $body = new Stream($body, Stream::MODE_READ_WRITE_RESET);
     }
     foreach ($headers as $name => $value) {
         $value = HeaderHelper::allToArray($value);
         if (!HeaderHelper::arrayOnlyContainsString($value)) {
             throw new \InvalidArgumentException('Header values should ony have string.');
         }
         if (!HeaderHelper::isValidName($name)) {
             throw new \InvalidArgumentException('Invalid header name');
         }
         $normalized = strtolower($name);
         $this->headerNames[$normalized] = $name;
         $this->headers[$name] = $value;
     }
     $this->stream = $body;
     $this->statusCode = $status;
 }
示例#2
0
 /**
  * Class init.
  *
  * @param string|UriInterface    $uri
  * @param string                 $method
  * @param string|StreamInterface $body
  * @param array                  $headers
  */
 public function __construct($uri = null, $method = null, $body = 'php://memory', $headers = array())
 {
     if (!$body instanceof StreamInterface) {
         $body = new Stream($body, Stream::MODE_READ_WRITE_RESET);
     }
     if (!$uri instanceof UriInterface) {
         $uri = new PsrUri((string) $uri);
     }
     foreach ($headers as $name => $value) {
         $value = HeaderHelper::allToArray($value);
         if (!HeaderHelper::arrayOnlyContainsString($value)) {
             throw new \InvalidArgumentException('Header values should ony have string.');
         }
         if (!HeaderHelper::isValidName($name)) {
             throw new \InvalidArgumentException('Invalid header name');
         }
         $normalized = strtolower($name);
         $this->headerNames[$normalized] = $name;
         $this->headers[$name] = $value;
     }
     $this->stream = $body;
     $this->method = $this->validateMethod($method);
     $this->uri = $uri;
 }
示例#3
0
 /**
  * Return an instance with the specified header appended with the given value.
  *
  * Existing values for the specified header will be maintained. The new
  * value(s) will be appended to the existing list. If the header did not
  * exist previously, it will be added.
  *
  * This method MUST be implemented in such a way as to retain the
  * immutability of the message, and MUST return an instance that has the
  * new header and/or value.
  *
  * @param string  $name  Case-insensitive header field name to add.
  * @param mixed   $value Header value(s).
  *
  * @return static
  * @throws \InvalidArgumentException for invalid header names or values.
  */
 public function withAddedHeader($name, $value)
 {
     $value = HeaderHelper::allToArray($value);
     if (!HeaderHelper::arrayOnlyContainsString($value)) {
         throw new \InvalidArgumentException('Header values should ony have string.');
     }
     if (!HeaderHelper::isValidName($name)) {
         throw new \InvalidArgumentException('Invalid header name');
     }
     $new = clone $this;
     if (!$this->hasHeader($name)) {
         $new = $new->createHeader($name);
     }
     $name = $new->getHeaderName($name);
     $new->headers[$name] = array_merge($new->headers[$name], $value);
     return $new;
 }