fatcode/http-server

View on GitHub
src/Request.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php declare(strict_types=1);

namespace FatCode\HttpServer;

use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Zend\Diactoros\RequestTrait;

class Request implements RequestInterface
{
    use RequestTrait;

    public function __construct(
        $uri = null,
        HttpMethod $method = null,
        $body = 'php://temp',
        array $headers = []
    ) {
        $this->validateUri($uri);
        $this->setHeaders($headers);
        $this->method = $method ? $method->getValue() : HttpMethod::GET()->getValue();
        if ($uri instanceof UriInterface) {
            $this->uri = $uri;
        } else {
            $this->uri = $uri ? new Uri((string) $uri) : new Uri();
        }
        $this->stream = Stream::create($body, 'wb+');

        $headers['Host'] = $headers['Host'] ?? [$this->getHostFromUri()];
    }

    private function validateUri($uri) : void
    {
        if (!$uri instanceof UriInterface && !is_string($uri) && null !== $uri) {
            throw new InvalidArgumentException(
                'Invalid URI provided; must be null, a string, or a Psr\Http\Message\UriInterface instance'
            );
        }
    }
}