Chris@0: validateStatusCode($statusCode); Chris@0: $this->validateBody($body); Chris@0: $this->validateHeaders($headers); Chris@0: Chris@0: $this->statusCode = (int) $statusCode; Chris@0: $this->body = (string) $body; Chris@0: $this->headers = $this->normalizeHeaders($headers); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritDoc} Chris@0: */ Chris@0: public function getStatusCode() Chris@0: { Chris@0: return $this->statusCode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritDoc} Chris@0: */ Chris@0: public function getBody() Chris@0: { Chris@0: return $this->body; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritDoc} Chris@0: */ Chris@0: public function getHeaderLine($name, $default = null) Chris@0: { Chris@0: $normalizedName = strtolower($name); Chris@0: return isset($this->headers[$normalizedName]) Chris@0: ? $this->headers[$normalizedName] Chris@0: : $default; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate that we have a status code argument that will work for our context. Chris@0: * Chris@17: * @param int $statusCode Chris@0: * @throws Exception\InvalidArgumentException for arguments not castable Chris@0: * to integer HTTP status codes. Chris@0: */ Chris@0: private function validateStatusCode($statusCode) Chris@0: { Chris@0: if (! is_numeric($statusCode)) { Chris@0: throw new Exception\InvalidArgumentException(sprintf( Chris@0: '%s expects a numeric status code; received %s', Chris@0: __CLASS__, Chris@0: (is_object($statusCode) ? get_class($statusCode) : gettype($statusCode)) Chris@0: )); Chris@0: } Chris@0: Chris@0: if (100 > $statusCode || 599 < $statusCode) { Chris@0: throw new Exception\InvalidArgumentException(sprintf( Chris@0: '%s expects an integer status code between 100 and 599 inclusive; received %s', Chris@0: __CLASS__, Chris@0: $statusCode Chris@0: )); Chris@0: } Chris@0: Chris@0: if (intval($statusCode) != $statusCode) { Chris@0: throw new Exception\InvalidArgumentException(sprintf( Chris@0: '%s expects an integer status code; received %s', Chris@0: __CLASS__, Chris@0: $statusCode Chris@0: )); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate that we have a body argument that will work for our context. Chris@0: * Chris@0: * @param mixed $body Chris@0: * @throws Exception\InvalidArgumentException for arguments not castable Chris@0: * to strings. Chris@0: */ Chris@0: private function validateBody($body) Chris@0: { Chris@0: if (is_string($body)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if (is_object($body) && method_exists($body, '__toString')) { Chris@0: return; Chris@0: } Chris@0: Chris@0: throw new Exception\InvalidArgumentException(sprintf( Chris@0: '%s expects a string body, or an object that can cast to string; received %s', Chris@0: __CLASS__, Chris@0: (is_object($body) ? get_class($body) : gettype($body)) Chris@0: )); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate header values. Chris@0: * Chris@0: * @param array $headers Chris@0: * @throws Exception\InvalidArgumentException Chris@0: */ Chris@0: private function validateHeaders(array $headers) Chris@0: { Chris@0: foreach ($headers as $name => $value) { Chris@0: if (! is_string($name) || is_numeric($name) || empty($name)) { Chris@0: throw new Exception\InvalidArgumentException(sprintf( Chris@0: 'Header names provided to %s must be non-empty, non-numeric strings; received %s', Chris@0: __CLASS__, Chris@0: $name Chris@0: )); Chris@0: } Chris@0: Chris@0: if (! is_string($value) && ! is_numeric($value)) { Chris@0: throw new Exception\InvalidArgumentException(sprintf( Chris@0: 'Individual header values provided to %s must be a string or numeric; received %s for header %s', Chris@0: __CLASS__, Chris@0: (is_object($value) ? get_class($value) : gettype($value)), Chris@0: $name Chris@0: )); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Normalize header names to lowercase. Chris@0: * Chris@0: * @param array $headers Chris@0: * @return array Chris@0: */ Chris@0: private function normalizeHeaders(array $headers) Chris@0: { Chris@0: $normalized = []; Chris@0: foreach ($headers as $name => $value) { Chris@0: $normalized[strtolower($name)] = $value; Chris@0: } Chris@0: return $normalized; Chris@0: } Chris@0: }