/** * Build a request instance from request parameters. * * @param string|null $method Case-sensitive method, or parsed from REQUEST_METHOD * when null is supplied for the method parameter. * @param string|null $uri, The URI value to parse, when the URI * is null the URI will be parsed from the $_SERVER superglobal. * @param array|null $headers, An associative array of the message's headers. Each * key MUST be a header name, and each value MUST be an array of strings * for that header. When null $_SERVER superglobal will be used. * @param string|null $version HTTP protocol version or parsed from SERVER_PROTOCOL * when null is provided. * @param string|resource|null $body, The PHP resource for the stream, representing * the request body content or a string containing the request body. * @return object */ public static function buildRequest($method = null, $uri = null, array $headers = null, $version = null, $body = null) { $headers = Headers::buildHeaders($headers); $uri = Uri::buildUri($uri); $body = $body === null ? Stream::buildStream('php://input') : Stream::buildStream($body); if ($method === null and isset($_SERVER['REQUEST_METHOD'])) { $method = $_SERVER['REQUEST_METHOD']; } if ($version === null and isset($_SERVER['SERVER_PROTOCOL'])) { $version = str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']); } return new Request($method, $uri, $headers, $version, $body); }
/** * Build a response instance from response parameters. * * @param int $code The 3-digit integer result code to set, 200 will be used * when null is provided for the code. * @param array|null $headers, An associative array of the message's headers. Each * key MUST be a header name, and each value MUST be an array of strings * for that header. When null an empty array will be used. * @param string|null $version HTTP protocol version or parsed from SERVER_PROTOCOL * when null is provided. * @param string|resource|null $body, The PHP resource for the stream, representing * the request body content or a string containing the request body. * @return object */ public static function buildResponse($code = null, array $headers = null, $version = null, $body = null) { if ($code === null) { $code = 200; // OK } if ($headers === null) { $headers = []; } if ($version === null and isset($_SERVER['SERVER_PROTOCOL'])) { $version = str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']); } $headers = Headers::buildHeaders($headers); $body = Stream::buildStream($body, 'w+'); return new Response($code, $headers, $version, $body); }
/** * Build a server request instance from server request parameters. * * @see Sephedo\Http\Request::buildRequest * @param array|null $cookies Array of key/value pairs representing cookies, * parsed from $_COOKIES when null is provided * @param array|null $environment Array of values representing the server * environment, when null environment is parsed from $_SERVER. * @param array|null $uploadedFiles Array containing uploaded files typically * from $_FILES. * @return object */ public static function buildServerRequest($method = null, $uri = null, array $headers = null, $version = null, array $cookies = null, array $environment = null, $body = null, array $uploadedFiles = null) { if ($cookies === null) { $cookies = isset($_COOKIES) ? $_COOKIES : []; } if ($environment === null) { $environment = $_SERVER; } $uploadedFiles = UploadedFile::buildUploadedFiles($uploadedFiles); $headers = Headers::buildHeaders($headers); $uri = Uri::buildUri($uri); $body = $body === null ? Stream::buildStream('php://input') : Stream::buildStream('php://input'); if ($method === null and isset($_SERVER['REQUEST_METHOD'])) { $method = $_SERVER['REQUEST_METHOD']; } if ($version === null and isset($_SERVER['SERVER_PROTOCOL'])) { $version = str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']); } return new ServerRequest($method, $uri, $headers, $version, $cookies, $environment, $body, $uploadedFiles); }