|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is licensed under MIT License. |
| 4 | + * |
| 5 | + * Copyright (c) 2024 WebFiori Framework |
| 6 | + * |
| 7 | + * For more information on the license, please visit: |
| 8 | + * https://github.com/WebFiori/http/blob/master/LICENSE |
| 9 | + */ |
| 10 | +namespace WebFiori\Http; |
| 11 | + |
| 12 | +/** |
| 13 | + * Base class for HTTP messages (Request and Response). |
| 14 | + * |
| 15 | + * @author Ibrahim |
| 16 | + */ |
| 17 | +class HttpMessage { |
| 18 | + /** |
| 19 | + * @var HeadersPool |
| 20 | + */ |
| 21 | + private $headersPool; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + private $body; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + private $protocolVersion; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + private $requestMethod; |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates new instance of the class. |
| 40 | + */ |
| 41 | + public function __construct() { |
| 42 | + $this->headersPool = new HeadersPool(); |
| 43 | + $this->body = ''; |
| 44 | + $this->protocolVersion = '1.1'; |
| 45 | + $this->requestMethod = 'GET'; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns the headers pool. |
| 50 | + * |
| 51 | + * @return HeadersPool |
| 52 | + */ |
| 53 | + public function getHeadersPool() : HeadersPool { |
| 54 | + return $this->headersPool; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns the value(s) of specific HTTP header. |
| 59 | + * |
| 60 | + * @param string $name The name of the header. |
| 61 | + * |
| 62 | + * @return array |
| 63 | + */ |
| 64 | + public function getHeader(string $name) : array { |
| 65 | + return $this->headersPool->getHeader($name); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns an array that contains all headers. |
| 70 | + * |
| 71 | + * @return array |
| 72 | + */ |
| 73 | + public function getHeaders() : array { |
| 74 | + return $this->headersPool->getHeaders(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Checks if specific header exists. |
| 79 | + * |
| 80 | + * @param string $name The name of the header. |
| 81 | + * @param string|null $val Optional header value to check. |
| 82 | + * |
| 83 | + * @return bool |
| 84 | + */ |
| 85 | + public function hasHeader(string $name, ?string $val = '') : bool { |
| 86 | + return $this->headersPool->hasHeader($name, $val); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Removes specific header. |
| 91 | + * |
| 92 | + * @param string $name The name of the header. |
| 93 | + * @param string|null $val Optional header value to remove. |
| 94 | + * |
| 95 | + * @return bool |
| 96 | + */ |
| 97 | + public function removeHeader(string $name, ?string $val = '') : bool { |
| 98 | + return $this->headersPool->removeHeader($name, $val); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Adds a header to the message. |
| 103 | + * |
| 104 | + * @param string $name The name of the header. |
| 105 | + * @param string $value The value of the header. |
| 106 | + * @param string|null $replaceValue Optional value to replace. |
| 107 | + * |
| 108 | + * @return bool |
| 109 | + */ |
| 110 | + public function addHeader(string $name, string $value, ?string $replaceValue = '') : bool { |
| 111 | + return $this->headersPool->addHeader($name, $value, $replaceValue); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Gets the body of the message. |
| 116 | + * |
| 117 | + * @return string |
| 118 | + */ |
| 119 | + public function getBody() : string { |
| 120 | + return $this->body; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Sets the body of the message. |
| 125 | + * |
| 126 | + * @param string $body |
| 127 | + */ |
| 128 | + public function setBody(string $body) { |
| 129 | + $this->body = $body; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Gets the protocol version. |
| 134 | + * |
| 135 | + * @return string |
| 136 | + */ |
| 137 | + public function getProtocolVersion() : string { |
| 138 | + return $this->protocolVersion; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Sets the protocol version. |
| 143 | + * |
| 144 | + * @param string $version |
| 145 | + */ |
| 146 | + public function setProtocolVersion(string $version) { |
| 147 | + $this->protocolVersion = $version; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Gets the request method. |
| 152 | + * |
| 153 | + * @return string |
| 154 | + */ |
| 155 | + public function getRequestMethod() : string { |
| 156 | + return $this->requestMethod; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Sets the request method. |
| 161 | + * |
| 162 | + * @param string $method |
| 163 | + */ |
| 164 | + public function setRequestMethod(string $method) { |
| 165 | + $this->requestMethod = $method; |
| 166 | + } |
| 167 | +} |
0 commit comments