@@ -42,50 +42,50 @@ |
||
| 42 | 42 | */ |
| 43 | 43 | function server_request_files(?array $files = null): array |
| 44 | 44 | { |
| 45 | - $files ??= $_FILES; |
|
| 45 | + $files ??= $_FILES; |
|
| 46 | 46 | |
| 47 | - $walker = function ($path, $size, $error, $name, $type) use (&$walker) { |
|
| 48 | - if (!is_array($path)) { |
|
| 49 | - // What if the path is an empty string? |
|
| 50 | - $stream = new FileStream($path, 'rb'); |
|
| 47 | + $walker = function ($path, $size, $error, $name, $type) use (&$walker) { |
|
| 48 | + if (!is_array($path)) { |
|
| 49 | + // What if the path is an empty string? |
|
| 50 | + $stream = new FileStream($path, 'rb'); |
|
| 51 | 51 | |
| 52 | - return new UploadedFile( |
|
| 53 | - $stream, |
|
| 54 | - $size, |
|
| 55 | - $error, |
|
| 56 | - $name, |
|
| 57 | - $type |
|
| 58 | - ); |
|
| 59 | - } |
|
| 52 | + return new UploadedFile( |
|
| 53 | + $stream, |
|
| 54 | + $size, |
|
| 55 | + $error, |
|
| 56 | + $name, |
|
| 57 | + $type |
|
| 58 | + ); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - $result = []; |
|
| 62 | - foreach ($path as $key => $_) { |
|
| 63 | - if (UPLOAD_ERR_NO_FILE <> $error[$key]) { |
|
| 64 | - $result[$key] = $walker( |
|
| 65 | - $path[$key], |
|
| 66 | - $size[$key], |
|
| 67 | - $error[$key], |
|
| 68 | - $name[$key], |
|
| 69 | - $type[$key] |
|
| 70 | - ); |
|
| 71 | - } |
|
| 72 | - } |
|
| 61 | + $result = []; |
|
| 62 | + foreach ($path as $key => $_) { |
|
| 63 | + if (UPLOAD_ERR_NO_FILE <> $error[$key]) { |
|
| 64 | + $result[$key] = $walker( |
|
| 65 | + $path[$key], |
|
| 66 | + $size[$key], |
|
| 67 | + $error[$key], |
|
| 68 | + $name[$key], |
|
| 69 | + $type[$key] |
|
| 70 | + ); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - return $result; |
|
| 75 | - }; |
|
| 74 | + return $result; |
|
| 75 | + }; |
|
| 76 | 76 | |
| 77 | - $result = []; |
|
| 78 | - foreach ($files as $key => $file) { |
|
| 79 | - if (UPLOAD_ERR_NO_FILE <> $file['error']) { |
|
| 80 | - $result[$key] = $walker( |
|
| 81 | - $file['tmp_name'], |
|
| 82 | - $file['size'], |
|
| 83 | - $file['error'], |
|
| 84 | - $file['name'], |
|
| 85 | - $file['type'] |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - } |
|
| 77 | + $result = []; |
|
| 78 | + foreach ($files as $key => $file) { |
|
| 79 | + if (UPLOAD_ERR_NO_FILE <> $file['error']) { |
|
| 80 | + $result[$key] = $walker( |
|
| 81 | + $file['tmp_name'], |
|
| 82 | + $file['size'], |
|
| 83 | + $file['error'], |
|
| 84 | + $file['name'], |
|
| 85 | + $file['type'] |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - return $result; |
|
| 90 | + return $result; |
|
| 91 | 91 | } |
@@ -25,105 +25,105 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | function header_accept_parse(string $header): array |
| 27 | 27 | { |
| 28 | - static $cache = []; |
|
| 29 | - |
|
| 30 | - if (isset($cache[$header])) { |
|
| 31 | - return $cache[$header]; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - $cursor = -1; |
|
| 35 | - $cursorInValue = true; |
|
| 36 | - $cursorInParameter = false; |
|
| 37 | - $cursorInParameterName = false; |
|
| 38 | - $cursorInParameterValue = false; |
|
| 39 | - $cursorInQuotedParameterValue = false; |
|
| 40 | - |
|
| 41 | - $data = []; |
|
| 42 | - $valueIndex = 0; |
|
| 43 | - $parameterIndex = 0; |
|
| 44 | - |
|
| 45 | - while (true) { |
|
| 46 | - $char = $header[++$cursor] ?? null; |
|
| 47 | - $prev = $header[$cursor-1] ?? null; |
|
| 48 | - $next = $header[$cursor+1] ?? null; |
|
| 49 | - |
|
| 50 | - if ($char === null) { |
|
| 51 | - break; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - if ($char === ' ' && !$cursorInQuotedParameterValue) { |
|
| 55 | - continue; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - if ($char === ',' && !$cursorInQuotedParameterValue) { |
|
| 59 | - $cursorInValue = true; |
|
| 60 | - $cursorInParameter = false; |
|
| 61 | - $cursorInParameterName = false; |
|
| 62 | - $cursorInParameterValue = false; |
|
| 63 | - $cursorInQuotedParameterValue = false; |
|
| 64 | - $parameterIndex = 0; |
|
| 65 | - $valueIndex++; |
|
| 66 | - continue; |
|
| 67 | - } |
|
| 68 | - if ($char === ';' && !$cursorInQuotedParameterValue) { |
|
| 69 | - $cursorInValue = false; |
|
| 70 | - $cursorInParameter = true; |
|
| 71 | - $cursorInParameterName = true; |
|
| 72 | - $cursorInParameterValue = false; |
|
| 73 | - $cursorInQuotedParameterValue = false; |
|
| 74 | - $parameterIndex++; |
|
| 75 | - continue; |
|
| 76 | - } |
|
| 77 | - if ($char === '=' && !$cursorInQuotedParameterValue && $cursorInParameterName) { |
|
| 78 | - $cursorInValue = false; |
|
| 79 | - $cursorInParameter = true; |
|
| 80 | - $cursorInParameterName = false; |
|
| 81 | - $cursorInParameterValue = true; |
|
| 82 | - $cursorInQuotedParameterValue = false; |
|
| 83 | - continue; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if ($char === '"' && !$cursorInQuotedParameterValue && $cursorInParameterValue) { |
|
| 87 | - $cursorInQuotedParameterValue = true; |
|
| 88 | - continue; |
|
| 89 | - } |
|
| 90 | - if ($char === '\\' && $next === '"' && $cursorInQuotedParameterValue) { |
|
| 91 | - continue; |
|
| 92 | - } |
|
| 93 | - if ($char === '"' && $prev !== '\\' && $cursorInQuotedParameterValue) { |
|
| 94 | - $cursorInParameterValue = false; |
|
| 95 | - $cursorInQuotedParameterValue = false; |
|
| 96 | - continue; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - if ($cursorInValue) { |
|
| 100 | - $data[$valueIndex][0] ??= ''; |
|
| 101 | - $data[$valueIndex][0] .= $char; |
|
| 102 | - continue; |
|
| 103 | - } |
|
| 104 | - if ($cursorInParameterName && isset($data[$valueIndex][0])) { |
|
| 105 | - $data[$valueIndex][1][$parameterIndex][0] ??= ''; |
|
| 106 | - $data[$valueIndex][1][$parameterIndex][0] .= $char; |
|
| 107 | - continue; |
|
| 108 | - } |
|
| 109 | - if ($cursorInParameterValue && isset($data[$valueIndex][1][$parameterIndex][0])) { |
|
| 110 | - $data[$valueIndex][1][$parameterIndex][1] ??= ''; |
|
| 111 | - $data[$valueIndex][1][$parameterIndex][1] .= $char; |
|
| 112 | - continue; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $result = []; |
|
| 117 | - foreach ($data as $item) { |
|
| 118 | - $result[$item[0]] = []; |
|
| 119 | - if (isset($item[1])) { |
|
| 120 | - foreach ($item[1] as $param) { |
|
| 121 | - $result[$item[0]][$param[0]] = $param[1] ?? ''; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - uasort($result, fn(array $a, array $b): int => ($b['q'] ?? 1) <=> ($a['q'] ?? 1)); |
|
| 127 | - |
|
| 128 | - return $cache[$header] = $result; |
|
| 28 | + static $cache = []; |
|
| 29 | + |
|
| 30 | + if (isset($cache[$header])) { |
|
| 31 | + return $cache[$header]; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + $cursor = -1; |
|
| 35 | + $cursorInValue = true; |
|
| 36 | + $cursorInParameter = false; |
|
| 37 | + $cursorInParameterName = false; |
|
| 38 | + $cursorInParameterValue = false; |
|
| 39 | + $cursorInQuotedParameterValue = false; |
|
| 40 | + |
|
| 41 | + $data = []; |
|
| 42 | + $valueIndex = 0; |
|
| 43 | + $parameterIndex = 0; |
|
| 44 | + |
|
| 45 | + while (true) { |
|
| 46 | + $char = $header[++$cursor] ?? null; |
|
| 47 | + $prev = $header[$cursor-1] ?? null; |
|
| 48 | + $next = $header[$cursor+1] ?? null; |
|
| 49 | + |
|
| 50 | + if ($char === null) { |
|
| 51 | + break; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + if ($char === ' ' && !$cursorInQuotedParameterValue) { |
|
| 55 | + continue; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + if ($char === ',' && !$cursorInQuotedParameterValue) { |
|
| 59 | + $cursorInValue = true; |
|
| 60 | + $cursorInParameter = false; |
|
| 61 | + $cursorInParameterName = false; |
|
| 62 | + $cursorInParameterValue = false; |
|
| 63 | + $cursorInQuotedParameterValue = false; |
|
| 64 | + $parameterIndex = 0; |
|
| 65 | + $valueIndex++; |
|
| 66 | + continue; |
|
| 67 | + } |
|
| 68 | + if ($char === ';' && !$cursorInQuotedParameterValue) { |
|
| 69 | + $cursorInValue = false; |
|
| 70 | + $cursorInParameter = true; |
|
| 71 | + $cursorInParameterName = true; |
|
| 72 | + $cursorInParameterValue = false; |
|
| 73 | + $cursorInQuotedParameterValue = false; |
|
| 74 | + $parameterIndex++; |
|
| 75 | + continue; |
|
| 76 | + } |
|
| 77 | + if ($char === '=' && !$cursorInQuotedParameterValue && $cursorInParameterName) { |
|
| 78 | + $cursorInValue = false; |
|
| 79 | + $cursorInParameter = true; |
|
| 80 | + $cursorInParameterName = false; |
|
| 81 | + $cursorInParameterValue = true; |
|
| 82 | + $cursorInQuotedParameterValue = false; |
|
| 83 | + continue; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if ($char === '"' && !$cursorInQuotedParameterValue && $cursorInParameterValue) { |
|
| 87 | + $cursorInQuotedParameterValue = true; |
|
| 88 | + continue; |
|
| 89 | + } |
|
| 90 | + if ($char === '\\' && $next === '"' && $cursorInQuotedParameterValue) { |
|
| 91 | + continue; |
|
| 92 | + } |
|
| 93 | + if ($char === '"' && $prev !== '\\' && $cursorInQuotedParameterValue) { |
|
| 94 | + $cursorInParameterValue = false; |
|
| 95 | + $cursorInQuotedParameterValue = false; |
|
| 96 | + continue; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + if ($cursorInValue) { |
|
| 100 | + $data[$valueIndex][0] ??= ''; |
|
| 101 | + $data[$valueIndex][0] .= $char; |
|
| 102 | + continue; |
|
| 103 | + } |
|
| 104 | + if ($cursorInParameterName && isset($data[$valueIndex][0])) { |
|
| 105 | + $data[$valueIndex][1][$parameterIndex][0] ??= ''; |
|
| 106 | + $data[$valueIndex][1][$parameterIndex][0] .= $char; |
|
| 107 | + continue; |
|
| 108 | + } |
|
| 109 | + if ($cursorInParameterValue && isset($data[$valueIndex][1][$parameterIndex][0])) { |
|
| 110 | + $data[$valueIndex][1][$parameterIndex][1] ??= ''; |
|
| 111 | + $data[$valueIndex][1][$parameterIndex][1] .= $char; |
|
| 112 | + continue; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $result = []; |
|
| 117 | + foreach ($data as $item) { |
|
| 118 | + $result[$item[0]] = []; |
|
| 119 | + if (isset($item[1])) { |
|
| 120 | + foreach ($item[1] as $param) { |
|
| 121 | + $result[$item[0]][$param[0]] = $param[1] ?? ''; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + uasort($result, fn(array $a, array $b): int => ($b['q'] ?? 1) <=> ($a['q'] ?? 1)); |
|
| 127 | + |
|
| 128 | + return $cache[$header] = $result; |
|
| 129 | 129 | } |
@@ -36,222 +36,222 @@ |
||
| 36 | 36 | abstract class Header implements HeaderInterface |
| 37 | 37 | { |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Regular Expression for a token validation |
|
| 41 | - * |
|
| 42 | - * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 43 | - * |
|
| 44 | - * @var string |
|
| 45 | - */ |
|
| 46 | - public const RFC7230_VALID_TOKEN = '/^[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]+$/'; |
|
| 39 | + /** |
|
| 40 | + * Regular Expression for a token validation |
|
| 41 | + * |
|
| 42 | + * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 43 | + * |
|
| 44 | + * @var string |
|
| 45 | + */ |
|
| 46 | + public const RFC7230_VALID_TOKEN = '/^[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]+$/'; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Regular Expression for a field value validation |
|
| 50 | - * |
|
| 51 | - * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 52 | - * |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - public const RFC7230_VALID_FIELD_VALUE = '/^[\x09\x20-\x7E\x80-\xFF]*$/'; |
|
| 48 | + /** |
|
| 49 | + * Regular Expression for a field value validation |
|
| 50 | + * |
|
| 51 | + * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 52 | + * |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + public const RFC7230_VALID_FIELD_VALUE = '/^[\x09\x20-\x7E\x80-\xFF]*$/'; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Regular Expression for a quoted string validation |
|
| 59 | - * |
|
| 60 | - * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 61 | - * |
|
| 62 | - * @var string |
|
| 63 | - */ |
|
| 64 | - public const RFC7230_VALID_QUOTED_STRING = '/^[\x09\x20\x21\x23-\x5B\x5D-\x7E\x80-\xFF]*$/'; |
|
| 57 | + /** |
|
| 58 | + * Regular Expression for a quoted string validation |
|
| 59 | + * |
|
| 60 | + * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 61 | + * |
|
| 62 | + * @var string |
|
| 63 | + */ |
|
| 64 | + public const RFC7230_VALID_QUOTED_STRING = '/^[\x09\x20\x21\x23-\x5B\x5D-\x7E\x80-\xFF]*$/'; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Date and time format |
|
| 68 | - * |
|
| 69 | - * @link https://www.rfc-editor.org/rfc/rfc822#section-5 |
|
| 70 | - * |
|
| 71 | - * @var string |
|
| 72 | - */ |
|
| 73 | - public const RFC822_DATE_TIME_FORMAT = 'D, d M y H:i:s O'; |
|
| 66 | + /** |
|
| 67 | + * Date and time format |
|
| 68 | + * |
|
| 69 | + * @link https://www.rfc-editor.org/rfc/rfc822#section-5 |
|
| 70 | + * |
|
| 71 | + * @var string |
|
| 72 | + */ |
|
| 73 | + public const RFC822_DATE_TIME_FORMAT = 'D, d M y H:i:s O'; |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * {@inheritdoc} |
|
| 77 | - */ |
|
| 78 | - final public function getIterator(): Traversable |
|
| 79 | - { |
|
| 80 | - yield $this->getFieldName(); |
|
| 81 | - yield $this->getFieldValue(); |
|
| 82 | - } |
|
| 75 | + /** |
|
| 76 | + * {@inheritdoc} |
|
| 77 | + */ |
|
| 78 | + final public function getIterator(): Traversable |
|
| 79 | + { |
|
| 80 | + yield $this->getFieldName(); |
|
| 81 | + yield $this->getFieldValue(); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * {@inheritdoc} |
|
| 86 | - */ |
|
| 87 | - final public function __toString(): string |
|
| 88 | - { |
|
| 89 | - return sprintf('%s: %s', $this->getFieldName(), $this->getFieldValue()); |
|
| 90 | - } |
|
| 84 | + /** |
|
| 85 | + * {@inheritdoc} |
|
| 86 | + */ |
|
| 87 | + final public function __toString(): string |
|
| 88 | + { |
|
| 89 | + return sprintf('%s: %s', $this->getFieldName(), $this->getFieldValue()); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Checks if the given string is a token |
|
| 94 | - * |
|
| 95 | - * @param string $token |
|
| 96 | - * |
|
| 97 | - * @return bool |
|
| 98 | - */ |
|
| 99 | - final protected function isToken(string $token): bool |
|
| 100 | - { |
|
| 101 | - return preg_match(self::RFC7230_VALID_TOKEN, $token) === 1; |
|
| 102 | - } |
|
| 92 | + /** |
|
| 93 | + * Checks if the given string is a token |
|
| 94 | + * |
|
| 95 | + * @param string $token |
|
| 96 | + * |
|
| 97 | + * @return bool |
|
| 98 | + */ |
|
| 99 | + final protected function isToken(string $token): bool |
|
| 100 | + { |
|
| 101 | + return preg_match(self::RFC7230_VALID_TOKEN, $token) === 1; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Validates the given token(s) |
|
| 106 | - * |
|
| 107 | - * @param string ...$tokens |
|
| 108 | - * |
|
| 109 | - * @return void |
|
| 110 | - * |
|
| 111 | - * @throws InvalidHeaderException |
|
| 112 | - * If one of the tokens isn't valid. |
|
| 113 | - */ |
|
| 114 | - final protected function validateToken(string ...$tokens): void |
|
| 115 | - { |
|
| 116 | - $this->validateValueByRegex(self::RFC7230_VALID_TOKEN, ...$tokens); |
|
| 117 | - } |
|
| 104 | + /** |
|
| 105 | + * Validates the given token(s) |
|
| 106 | + * |
|
| 107 | + * @param string ...$tokens |
|
| 108 | + * |
|
| 109 | + * @return void |
|
| 110 | + * |
|
| 111 | + * @throws InvalidHeaderException |
|
| 112 | + * If one of the tokens isn't valid. |
|
| 113 | + */ |
|
| 114 | + final protected function validateToken(string ...$tokens): void |
|
| 115 | + { |
|
| 116 | + $this->validateValueByRegex(self::RFC7230_VALID_TOKEN, ...$tokens); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Validates the given field value(s) |
|
| 121 | - * |
|
| 122 | - * @param string ...$fieldValues |
|
| 123 | - * |
|
| 124 | - * @return void |
|
| 125 | - * |
|
| 126 | - * @throws InvalidHeaderException |
|
| 127 | - * If one of the field values isn't valid. |
|
| 128 | - */ |
|
| 129 | - final protected function validateFieldValue(string ...$fieldValues): void |
|
| 130 | - { |
|
| 131 | - $this->validateValueByRegex(self::RFC7230_VALID_FIELD_VALUE, ...$fieldValues); |
|
| 132 | - } |
|
| 119 | + /** |
|
| 120 | + * Validates the given field value(s) |
|
| 121 | + * |
|
| 122 | + * @param string ...$fieldValues |
|
| 123 | + * |
|
| 124 | + * @return void |
|
| 125 | + * |
|
| 126 | + * @throws InvalidHeaderException |
|
| 127 | + * If one of the field values isn't valid. |
|
| 128 | + */ |
|
| 129 | + final protected function validateFieldValue(string ...$fieldValues): void |
|
| 130 | + { |
|
| 131 | + $this->validateValueByRegex(self::RFC7230_VALID_FIELD_VALUE, ...$fieldValues); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Validates the given quoted string(s) |
|
| 136 | - * |
|
| 137 | - * @param string ...$quotedStrings |
|
| 138 | - * |
|
| 139 | - * @return void |
|
| 140 | - * |
|
| 141 | - * @throws InvalidHeaderException |
|
| 142 | - * If one of the quoted strings isn't valid. |
|
| 143 | - */ |
|
| 144 | - final protected function validateQuotedString(string ...$quotedStrings): void |
|
| 145 | - { |
|
| 146 | - $this->validateValueByRegex(self::RFC7230_VALID_QUOTED_STRING, ...$quotedStrings); |
|
| 147 | - } |
|
| 134 | + /** |
|
| 135 | + * Validates the given quoted string(s) |
|
| 136 | + * |
|
| 137 | + * @param string ...$quotedStrings |
|
| 138 | + * |
|
| 139 | + * @return void |
|
| 140 | + * |
|
| 141 | + * @throws InvalidHeaderException |
|
| 142 | + * If one of the quoted strings isn't valid. |
|
| 143 | + */ |
|
| 144 | + final protected function validateQuotedString(string ...$quotedStrings): void |
|
| 145 | + { |
|
| 146 | + $this->validateValueByRegex(self::RFC7230_VALID_QUOTED_STRING, ...$quotedStrings); |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Validates and normalizes the given parameters |
|
| 151 | - * |
|
| 152 | - * @param array<array-key, mixed> $parameters |
|
| 153 | - * |
|
| 154 | - * @return array<string, string> |
|
| 155 | - * The normalized parameters. |
|
| 156 | - * |
|
| 157 | - * @throws InvalidHeaderException |
|
| 158 | - * If one of the parameters isn't valid. |
|
| 159 | - */ |
|
| 160 | - final protected function validateParameters(array $parameters): array |
|
| 161 | - { |
|
| 162 | - return $this->validateParametersByRegex( |
|
| 163 | - $parameters, |
|
| 164 | - self::RFC7230_VALID_TOKEN, |
|
| 165 | - self::RFC7230_VALID_QUOTED_STRING |
|
| 166 | - ); |
|
| 167 | - } |
|
| 149 | + /** |
|
| 150 | + * Validates and normalizes the given parameters |
|
| 151 | + * |
|
| 152 | + * @param array<array-key, mixed> $parameters |
|
| 153 | + * |
|
| 154 | + * @return array<string, string> |
|
| 155 | + * The normalized parameters. |
|
| 156 | + * |
|
| 157 | + * @throws InvalidHeaderException |
|
| 158 | + * If one of the parameters isn't valid. |
|
| 159 | + */ |
|
| 160 | + final protected function validateParameters(array $parameters): array |
|
| 161 | + { |
|
| 162 | + return $this->validateParametersByRegex( |
|
| 163 | + $parameters, |
|
| 164 | + self::RFC7230_VALID_TOKEN, |
|
| 165 | + self::RFC7230_VALID_QUOTED_STRING |
|
| 166 | + ); |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Validates the given value(s) by the given regular expression |
|
| 171 | - * |
|
| 172 | - * @param string $regex |
|
| 173 | - * @param string ...$values |
|
| 174 | - * |
|
| 175 | - * @return void |
|
| 176 | - * |
|
| 177 | - * @throws InvalidHeaderException |
|
| 178 | - * If one of the values isn't valid. |
|
| 179 | - */ |
|
| 180 | - final protected function validateValueByRegex(string $regex, string ...$values): void |
|
| 181 | - { |
|
| 182 | - foreach ($values as $value) { |
|
| 183 | - if (!preg_match($regex, $value)) { |
|
| 184 | - throw new InvalidHeaderException(sprintf( |
|
| 185 | - 'The value "%2$s" for the header "%1$s" is not valid', |
|
| 186 | - $this->getFieldName(), |
|
| 187 | - $value |
|
| 188 | - )); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - } |
|
| 169 | + /** |
|
| 170 | + * Validates the given value(s) by the given regular expression |
|
| 171 | + * |
|
| 172 | + * @param string $regex |
|
| 173 | + * @param string ...$values |
|
| 174 | + * |
|
| 175 | + * @return void |
|
| 176 | + * |
|
| 177 | + * @throws InvalidHeaderException |
|
| 178 | + * If one of the values isn't valid. |
|
| 179 | + */ |
|
| 180 | + final protected function validateValueByRegex(string $regex, string ...$values): void |
|
| 181 | + { |
|
| 182 | + foreach ($values as $value) { |
|
| 183 | + if (!preg_match($regex, $value)) { |
|
| 184 | + throw new InvalidHeaderException(sprintf( |
|
| 185 | + 'The value "%2$s" for the header "%1$s" is not valid', |
|
| 186 | + $this->getFieldName(), |
|
| 187 | + $value |
|
| 188 | + )); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - /** |
|
| 194 | - * Validates and normalizes the given parameters by the given regular expressions |
|
| 195 | - * |
|
| 196 | - * @param array<array-key, mixed> $parameters |
|
| 197 | - * @param string $nameRegex |
|
| 198 | - * @param string $valueRegex |
|
| 199 | - * |
|
| 200 | - * @return array<string, string> |
|
| 201 | - * The normalized parameters. |
|
| 202 | - * |
|
| 203 | - * @throws InvalidHeaderException |
|
| 204 | - * If one of the parameters isn't valid. |
|
| 205 | - */ |
|
| 206 | - final protected function validateParametersByRegex(array $parameters, string $nameRegex, string $valueRegex): array |
|
| 207 | - { |
|
| 208 | - foreach ($parameters as $name => &$value) { |
|
| 209 | - if (!is_string($name) || !preg_match($nameRegex, $name)) { |
|
| 210 | - throw new InvalidHeaderException(sprintf( |
|
| 211 | - 'The parameter name "%2$s" for the header "%1$s" is not valid', |
|
| 212 | - $this->getFieldName(), |
|
| 213 | - (is_string($name) ? $name : ('<' . gettype($name) . '>')) |
|
| 214 | - )); |
|
| 215 | - } |
|
| 193 | + /** |
|
| 194 | + * Validates and normalizes the given parameters by the given regular expressions |
|
| 195 | + * |
|
| 196 | + * @param array<array-key, mixed> $parameters |
|
| 197 | + * @param string $nameRegex |
|
| 198 | + * @param string $valueRegex |
|
| 199 | + * |
|
| 200 | + * @return array<string, string> |
|
| 201 | + * The normalized parameters. |
|
| 202 | + * |
|
| 203 | + * @throws InvalidHeaderException |
|
| 204 | + * If one of the parameters isn't valid. |
|
| 205 | + */ |
|
| 206 | + final protected function validateParametersByRegex(array $parameters, string $nameRegex, string $valueRegex): array |
|
| 207 | + { |
|
| 208 | + foreach ($parameters as $name => &$value) { |
|
| 209 | + if (!is_string($name) || !preg_match($nameRegex, $name)) { |
|
| 210 | + throw new InvalidHeaderException(sprintf( |
|
| 211 | + 'The parameter name "%2$s" for the header "%1$s" is not valid', |
|
| 212 | + $this->getFieldName(), |
|
| 213 | + (is_string($name) ? $name : ('<' . gettype($name) . '>')) |
|
| 214 | + )); |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - // e.g. Cache-Control: max-age=31536000 |
|
| 218 | - if (is_int($value)) { |
|
| 219 | - $value = (string) $value; |
|
| 220 | - } |
|
| 217 | + // e.g. Cache-Control: max-age=31536000 |
|
| 218 | + if (is_int($value)) { |
|
| 219 | + $value = (string) $value; |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - if (!is_string($value) || !preg_match($valueRegex, $value)) { |
|
| 223 | - throw new InvalidHeaderException(sprintf( |
|
| 224 | - 'The parameter value "%2$s" for the header "%1$s" is not valid', |
|
| 225 | - $this->getFieldName(), |
|
| 226 | - (is_string($value) ? $value : ('<' . gettype($value) . '>')) |
|
| 227 | - )); |
|
| 228 | - } |
|
| 229 | - } |
|
| 222 | + if (!is_string($value) || !preg_match($valueRegex, $value)) { |
|
| 223 | + throw new InvalidHeaderException(sprintf( |
|
| 224 | + 'The parameter value "%2$s" for the header "%1$s" is not valid', |
|
| 225 | + $this->getFieldName(), |
|
| 226 | + (is_string($value) ? $value : ('<' . gettype($value) . '>')) |
|
| 227 | + )); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - /** @var array<string, string> $parameters */ |
|
| 231 | + /** @var array<string, string> $parameters */ |
|
| 232 | 232 | |
| 233 | - return $parameters; |
|
| 234 | - } |
|
| 233 | + return $parameters; |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - /** |
|
| 237 | - * Formats the given date-time object |
|
| 238 | - * |
|
| 239 | - * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 240 | - * |
|
| 241 | - * @param DateTimeInterface $dateTime |
|
| 242 | - * |
|
| 243 | - * @return string |
|
| 244 | - */ |
|
| 245 | - final protected function formatDateTime(DateTimeInterface $dateTime): string |
|
| 246 | - { |
|
| 247 | - if ($dateTime instanceof DateTime) { |
|
| 248 | - return (clone $dateTime) |
|
| 249 | - ->setTimezone(new DateTimeZone('GMT')) |
|
| 250 | - ->format(self::RFC822_DATE_TIME_FORMAT); |
|
| 251 | - } |
|
| 236 | + /** |
|
| 237 | + * Formats the given date-time object |
|
| 238 | + * |
|
| 239 | + * @link https://tools.ietf.org/html/rfc7230#section-3.2 |
|
| 240 | + * |
|
| 241 | + * @param DateTimeInterface $dateTime |
|
| 242 | + * |
|
| 243 | + * @return string |
|
| 244 | + */ |
|
| 245 | + final protected function formatDateTime(DateTimeInterface $dateTime): string |
|
| 246 | + { |
|
| 247 | + if ($dateTime instanceof DateTime) { |
|
| 248 | + return (clone $dateTime) |
|
| 249 | + ->setTimezone(new DateTimeZone('GMT')) |
|
| 250 | + ->format(self::RFC822_DATE_TIME_FORMAT); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - return $dateTime |
|
| 254 | - ->setTimezone(new DateTimeZone('GMT')) |
|
| 255 | - ->format(self::RFC822_DATE_TIME_FORMAT); |
|
| 256 | - } |
|
| 253 | + return $dateTime |
|
| 254 | + ->setTimezone(new DateTimeZone('GMT')) |
|
| 255 | + ->format(self::RFC822_DATE_TIME_FORMAT); |
|
| 256 | + } |
|
| 257 | 257 | } |
@@ -38,582 +38,582 @@ |
||
| 38 | 38 | final class ServerRequestProxy implements ServerRequestInterface, RequestMethodInterface |
| 39 | 39 | { |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @var ServerRequestInterface |
|
| 43 | - */ |
|
| 44 | - private ServerRequestInterface $request; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Constructor of the class |
|
| 48 | - * |
|
| 49 | - * @param ServerRequestInterface $request |
|
| 50 | - */ |
|
| 51 | - public function __construct(ServerRequestInterface $request) |
|
| 52 | - { |
|
| 53 | - $this->request = $request; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Creates the proxy from the given object |
|
| 58 | - * |
|
| 59 | - * @param ServerRequestInterface $request |
|
| 60 | - * |
|
| 61 | - * @return self |
|
| 62 | - */ |
|
| 63 | - public static function create(ServerRequestInterface $request): self |
|
| 64 | - { |
|
| 65 | - if ($request instanceof self) { |
|
| 66 | - return $request; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return new self($request); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Checks if the request is JSON |
|
| 74 | - * |
|
| 75 | - * @link https://tools.ietf.org/html/rfc4627 |
|
| 76 | - * |
|
| 77 | - * @return bool |
|
| 78 | - */ |
|
| 79 | - public function isJson(): bool |
|
| 80 | - { |
|
| 81 | - return $this->clientProducesMediaType([ |
|
| 82 | - 'application/json', |
|
| 83 | - ]); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Checks if the request is XML |
|
| 88 | - * |
|
| 89 | - * @link https://tools.ietf.org/html/rfc2376 |
|
| 90 | - * |
|
| 91 | - * @return bool |
|
| 92 | - */ |
|
| 93 | - public function isXml(): bool |
|
| 94 | - { |
|
| 95 | - return $this->clientProducesMediaType([ |
|
| 96 | - 'application/xml', |
|
| 97 | - 'text/xml', |
|
| 98 | - ]); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Gets the client's IP address |
|
| 103 | - * |
|
| 104 | - * @param array<string, string> $proxyChain |
|
| 105 | - * |
|
| 106 | - * @return IpAddress |
|
| 107 | - */ |
|
| 108 | - public function getClientIpAddress(array $proxyChain = []): IpAddress |
|
| 109 | - { |
|
| 110 | - $env = $this->request->getServerParams(); |
|
| 111 | - |
|
| 112 | - /** @var string */ |
|
| 113 | - $client = $env['REMOTE_ADDR'] ?? '::1'; |
|
| 114 | - |
|
| 115 | - /** @var list<string> */ |
|
| 116 | - $proxies = []; |
|
| 117 | - |
|
| 118 | - while (isset($proxyChain[$client])) { |
|
| 119 | - $proxyHeader = $proxyChain[$client]; |
|
| 120 | - unset($proxyChain[$client]); |
|
| 121 | - |
|
| 122 | - $header = $this->request->getHeaderLine($proxyHeader); |
|
| 123 | - if ($header === '') { |
|
| 124 | - break; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $addresses = explode(',', $header); |
|
| 128 | - foreach ($addresses as $i => $address) { |
|
| 129 | - $addresses[$i] = trim($address); |
|
| 130 | - if ($addresses[$i] === '') { |
|
| 131 | - unset($addresses[$i]); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - if ($addresses === []) { |
|
| 136 | - break; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - $client = reset($addresses); |
|
| 140 | - unset($addresses[key($addresses)]); |
|
| 141 | - |
|
| 142 | - foreach ($addresses as $address) { |
|
| 143 | - $proxies[] = $address; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return new IpAddress($client, $proxies); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Gets the client's produced media type |
|
| 152 | - * |
|
| 153 | - * @link https://tools.ietf.org/html/rfc7231#section-3.1.1.1 |
|
| 154 | - * @link https://tools.ietf.org/html/rfc7231#section-3.1.1.5 |
|
| 155 | - * |
|
| 156 | - * @return string |
|
| 157 | - */ |
|
| 158 | - public function getClientProducedMediaType(): string |
|
| 159 | - { |
|
| 160 | - $header = $this->request->getHeaderLine('Content-Type'); |
|
| 161 | - if ($header === '') { |
|
| 162 | - return ''; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $result = strstr($header, ';', true); |
|
| 166 | - if ($result === false) { |
|
| 167 | - $result = $header; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $result = trim($result); |
|
| 171 | - if ($result === '') { |
|
| 172 | - return ''; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - return strtolower($result); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Gets the client's consumed media types |
|
| 180 | - * |
|
| 181 | - * @link https://tools.ietf.org/html/rfc7231#section-1.2 |
|
| 182 | - * @link https://tools.ietf.org/html/rfc7231#section-3.1.1.1 |
|
| 183 | - * @link https://tools.ietf.org/html/rfc7231#section-5.3.2 |
|
| 184 | - * |
|
| 185 | - * @return array<string, array<string, string>> |
|
| 186 | - */ |
|
| 187 | - public function getClientConsumedMediaTypes(): array |
|
| 188 | - { |
|
| 189 | - $header = $this->request->getHeaderLine('Accept'); |
|
| 190 | - if ($header === '') { |
|
| 191 | - return []; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - $accept = header_accept_parse($header); |
|
| 195 | - if ($accept === []) { |
|
| 196 | - return []; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - $result = []; |
|
| 200 | - foreach ($accept as $type => $params) { |
|
| 201 | - $result[strtolower($type)] = $params; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - return $result; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Gets the client's consumed encodings |
|
| 209 | - * |
|
| 210 | - * @return array<string, array<string, string>> |
|
| 211 | - */ |
|
| 212 | - public function getClientConsumedEncodings(): array |
|
| 213 | - { |
|
| 214 | - $header = $this->request->getHeaderLine('Accept-Encoding'); |
|
| 215 | - if ($header === '') { |
|
| 216 | - return []; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $accept = header_accept_parse($header); |
|
| 220 | - if ($accept === []) { |
|
| 221 | - return []; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - return $accept; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Gets the client's consumed languages |
|
| 229 | - * |
|
| 230 | - * @return array<string, array<string, string>> |
|
| 231 | - */ |
|
| 232 | - public function getClientConsumedLanguages(): array |
|
| 233 | - { |
|
| 234 | - $header = $this->request->getHeaderLine('Accept-Language'); |
|
| 235 | - if ($header === '') { |
|
| 236 | - return []; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - $accept = header_accept_parse($header); |
|
| 240 | - if ($accept === []) { |
|
| 241 | - return []; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - return $accept; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Checks if the client produces one of the given media types |
|
| 249 | - * |
|
| 250 | - * @param list<string> $consumes |
|
| 251 | - * |
|
| 252 | - * @return bool |
|
| 253 | - */ |
|
| 254 | - public function clientProducesMediaType(array $consumes): bool |
|
| 255 | - { |
|
| 256 | - if ($consumes === []) { |
|
| 257 | - return true; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - $produced = $this->getClientProducedMediaType(); |
|
| 261 | - if ($produced === '') { |
|
| 262 | - return false; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - foreach ($consumes as $consumed) { |
|
| 266 | - if ($this->equalsMediaTypes($consumed, $produced)) { |
|
| 267 | - return true; |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - return false; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Checks if the client consumes one of the given media types |
|
| 276 | - * |
|
| 277 | - * @param list<string> $produces |
|
| 278 | - * |
|
| 279 | - * @return bool |
|
| 280 | - */ |
|
| 281 | - public function clientConsumesMediaType(array $produces): bool |
|
| 282 | - { |
|
| 283 | - if ($produces === []) { |
|
| 284 | - return true; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - $consumes = $this->getClientConsumedMediaTypes(); |
|
| 288 | - if ($consumes === []) { |
|
| 289 | - return true; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - if (isset($consumes['*/*'])) { |
|
| 293 | - return true; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - foreach ($produces as $a) { |
|
| 297 | - foreach ($consumes as $b => $_) { |
|
| 298 | - if ($this->equalsMediaTypes($a, $b)) { |
|
| 299 | - return true; |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - return false; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Checks if the given media types are equal |
|
| 309 | - * |
|
| 310 | - * @param string $a |
|
| 311 | - * @param string $b |
|
| 312 | - * |
|
| 313 | - * @return bool |
|
| 314 | - */ |
|
| 315 | - public function equalsMediaTypes(string $a, string $b): bool |
|
| 316 | - { |
|
| 317 | - if ($a === $b) { |
|
| 318 | - return true; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - $slash = strpos($a, '/'); |
|
| 322 | - if ($slash === false || !isset($b[$slash]) || $b[$slash] !== '/') { |
|
| 323 | - return false; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - $star = $slash + 1; |
|
| 327 | - if (!isset($a[$star], $b[$star])) { |
|
| 328 | - return false; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - if (!($a[$star] === '*' || $b[$star] === '*')) { |
|
| 332 | - return false; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - return strncmp($a, $b, $star) === 0; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * {@inheritdoc} |
|
| 340 | - */ |
|
| 341 | - public function getProtocolVersion(): string |
|
| 342 | - { |
|
| 343 | - return $this->request->getProtocolVersion(); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * {@inheritdoc} |
|
| 348 | - */ |
|
| 349 | - public function withProtocolVersion($version) |
|
| 350 | - { |
|
| 351 | - $clone = clone $this; |
|
| 352 | - $clone->request = $clone->request->withProtocolVersion($version); |
|
| 353 | - |
|
| 354 | - return $clone; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * {@inheritdoc} |
|
| 359 | - */ |
|
| 360 | - public function getHeaders(): array |
|
| 361 | - { |
|
| 362 | - return $this->request->getHeaders(); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * {@inheritdoc} |
|
| 367 | - */ |
|
| 368 | - public function hasHeader($name): bool |
|
| 369 | - { |
|
| 370 | - return $this->request->hasHeader($name); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * {@inheritdoc} |
|
| 375 | - */ |
|
| 376 | - public function getHeader($name): array |
|
| 377 | - { |
|
| 378 | - return $this->request->getHeader($name); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * {@inheritdoc} |
|
| 383 | - */ |
|
| 384 | - public function getHeaderLine($name): string |
|
| 385 | - { |
|
| 386 | - return $this->request->getHeaderLine($name); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * {@inheritdoc} |
|
| 391 | - */ |
|
| 392 | - public function withHeader($name, $value) |
|
| 393 | - { |
|
| 394 | - $clone = clone $this; |
|
| 395 | - $clone->request = $clone->request->withHeader($name, $value); |
|
| 396 | - |
|
| 397 | - return $clone; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * {@inheritdoc} |
|
| 402 | - */ |
|
| 403 | - public function withAddedHeader($name, $value) |
|
| 404 | - { |
|
| 405 | - $clone = clone $this; |
|
| 406 | - $clone->request = $clone->request->withAddedHeader($name, $value); |
|
| 407 | - |
|
| 408 | - return $clone; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * {@inheritdoc} |
|
| 413 | - */ |
|
| 414 | - public function withoutHeader($name) |
|
| 415 | - { |
|
| 416 | - $clone = clone $this; |
|
| 417 | - $clone->request = $clone->request->withoutHeader($name); |
|
| 418 | - |
|
| 419 | - return $clone; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * {@inheritdoc} |
|
| 424 | - */ |
|
| 425 | - public function getBody(): StreamInterface |
|
| 426 | - { |
|
| 427 | - return $this->request->getBody(); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * {@inheritdoc} |
|
| 432 | - */ |
|
| 433 | - public function withBody(StreamInterface $body) |
|
| 434 | - { |
|
| 435 | - $clone = clone $this; |
|
| 436 | - $clone->request = $clone->request->withBody($body); |
|
| 437 | - |
|
| 438 | - return $clone; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * {@inheritdoc} |
|
| 443 | - */ |
|
| 444 | - public function getMethod(): string |
|
| 445 | - { |
|
| 446 | - return $this->request->getMethod(); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * {@inheritdoc} |
|
| 451 | - */ |
|
| 452 | - public function withMethod($method) |
|
| 453 | - { |
|
| 454 | - $clone = clone $this; |
|
| 455 | - $clone->request = $clone->request->withMethod($method); |
|
| 456 | - |
|
| 457 | - return $clone; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * {@inheritdoc} |
|
| 462 | - */ |
|
| 463 | - public function getUri(): UriInterface |
|
| 464 | - { |
|
| 465 | - return $this->request->getUri(); |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * {@inheritdoc} |
|
| 470 | - */ |
|
| 471 | - public function withUri(UriInterface $uri, $preserveHost = false) |
|
| 472 | - { |
|
| 473 | - $clone = clone $this; |
|
| 474 | - $clone->request = $clone->request->withUri($uri, $preserveHost); |
|
| 475 | - |
|
| 476 | - return $clone; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * {@inheritdoc} |
|
| 481 | - */ |
|
| 482 | - public function getRequestTarget(): string |
|
| 483 | - { |
|
| 484 | - return $this->request->getRequestTarget(); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * {@inheritdoc} |
|
| 489 | - */ |
|
| 490 | - public function withRequestTarget($requestTarget) |
|
| 491 | - { |
|
| 492 | - $clone = clone $this; |
|
| 493 | - $clone->request = $clone->request->withRequestTarget($requestTarget); |
|
| 494 | - |
|
| 495 | - return $clone; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * {@inheritdoc} |
|
| 500 | - */ |
|
| 501 | - public function getServerParams(): array |
|
| 502 | - { |
|
| 503 | - return $this->request->getServerParams(); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - /** |
|
| 507 | - * {@inheritdoc} |
|
| 508 | - */ |
|
| 509 | - public function getQueryParams(): array |
|
| 510 | - { |
|
| 511 | - return $this->request->getQueryParams(); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * {@inheritdoc} |
|
| 516 | - */ |
|
| 517 | - public function withQueryParams(array $query) |
|
| 518 | - { |
|
| 519 | - $clone = clone $this; |
|
| 520 | - $clone->request = $clone->request->withQueryParams($query); |
|
| 521 | - |
|
| 522 | - return $clone; |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - /** |
|
| 526 | - * {@inheritdoc} |
|
| 527 | - */ |
|
| 528 | - public function getCookieParams(): array |
|
| 529 | - { |
|
| 530 | - return $this->request->getCookieParams(); |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * {@inheritdoc} |
|
| 535 | - */ |
|
| 536 | - public function withCookieParams(array $cookies) |
|
| 537 | - { |
|
| 538 | - $clone = clone $this; |
|
| 539 | - $clone->request = $clone->request->withCookieParams($cookies); |
|
| 540 | - |
|
| 541 | - return $clone; |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - /** |
|
| 545 | - * {@inheritdoc} |
|
| 546 | - */ |
|
| 547 | - public function getUploadedFiles(): array |
|
| 548 | - { |
|
| 549 | - return $this->request->getUploadedFiles(); |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * {@inheritdoc} |
|
| 554 | - */ |
|
| 555 | - public function withUploadedFiles(array $uploadedFiles) |
|
| 556 | - { |
|
| 557 | - $clone = clone $this; |
|
| 558 | - $clone->request = $clone->request->withUploadedFiles($uploadedFiles); |
|
| 559 | - |
|
| 560 | - return $clone; |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * {@inheritdoc} |
|
| 565 | - */ |
|
| 566 | - public function getParsedBody() |
|
| 567 | - { |
|
| 568 | - return $this->request->getParsedBody(); |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * {@inheritdoc} |
|
| 573 | - */ |
|
| 574 | - public function withParsedBody($data) |
|
| 575 | - { |
|
| 576 | - $clone = clone $this; |
|
| 577 | - $clone->request = $clone->request->withParsedBody($data); |
|
| 578 | - |
|
| 579 | - return $clone; |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * {@inheritdoc} |
|
| 584 | - */ |
|
| 585 | - public function getAttributes(): array |
|
| 586 | - { |
|
| 587 | - return $this->request->getAttributes(); |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * {@inheritdoc} |
|
| 592 | - */ |
|
| 593 | - public function getAttribute($name, $default = null) |
|
| 594 | - { |
|
| 595 | - return $this->request->getAttribute($name, $default); |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * {@inheritdoc} |
|
| 600 | - */ |
|
| 601 | - public function withAttribute($name, $value) |
|
| 602 | - { |
|
| 603 | - $clone = clone $this; |
|
| 604 | - $clone->request = $clone->request->withAttribute($name, $value); |
|
| 605 | - |
|
| 606 | - return $clone; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * {@inheritdoc} |
|
| 611 | - */ |
|
| 612 | - public function withoutAttribute($name) |
|
| 613 | - { |
|
| 614 | - $clone = clone $this; |
|
| 615 | - $clone->request = $clone->request->withoutAttribute($name); |
|
| 616 | - |
|
| 617 | - return $clone; |
|
| 618 | - } |
|
| 41 | + /** |
|
| 42 | + * @var ServerRequestInterface |
|
| 43 | + */ |
|
| 44 | + private ServerRequestInterface $request; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Constructor of the class |
|
| 48 | + * |
|
| 49 | + * @param ServerRequestInterface $request |
|
| 50 | + */ |
|
| 51 | + public function __construct(ServerRequestInterface $request) |
|
| 52 | + { |
|
| 53 | + $this->request = $request; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Creates the proxy from the given object |
|
| 58 | + * |
|
| 59 | + * @param ServerRequestInterface $request |
|
| 60 | + * |
|
| 61 | + * @return self |
|
| 62 | + */ |
|
| 63 | + public static function create(ServerRequestInterface $request): self |
|
| 64 | + { |
|
| 65 | + if ($request instanceof self) { |
|
| 66 | + return $request; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return new self($request); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Checks if the request is JSON |
|
| 74 | + * |
|
| 75 | + * @link https://tools.ietf.org/html/rfc4627 |
|
| 76 | + * |
|
| 77 | + * @return bool |
|
| 78 | + */ |
|
| 79 | + public function isJson(): bool |
|
| 80 | + { |
|
| 81 | + return $this->clientProducesMediaType([ |
|
| 82 | + 'application/json', |
|
| 83 | + ]); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Checks if the request is XML |
|
| 88 | + * |
|
| 89 | + * @link https://tools.ietf.org/html/rfc2376 |
|
| 90 | + * |
|
| 91 | + * @return bool |
|
| 92 | + */ |
|
| 93 | + public function isXml(): bool |
|
| 94 | + { |
|
| 95 | + return $this->clientProducesMediaType([ |
|
| 96 | + 'application/xml', |
|
| 97 | + 'text/xml', |
|
| 98 | + ]); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Gets the client's IP address |
|
| 103 | + * |
|
| 104 | + * @param array<string, string> $proxyChain |
|
| 105 | + * |
|
| 106 | + * @return IpAddress |
|
| 107 | + */ |
|
| 108 | + public function getClientIpAddress(array $proxyChain = []): IpAddress |
|
| 109 | + { |
|
| 110 | + $env = $this->request->getServerParams(); |
|
| 111 | + |
|
| 112 | + /** @var string */ |
|
| 113 | + $client = $env['REMOTE_ADDR'] ?? '::1'; |
|
| 114 | + |
|
| 115 | + /** @var list<string> */ |
|
| 116 | + $proxies = []; |
|
| 117 | + |
|
| 118 | + while (isset($proxyChain[$client])) { |
|
| 119 | + $proxyHeader = $proxyChain[$client]; |
|
| 120 | + unset($proxyChain[$client]); |
|
| 121 | + |
|
| 122 | + $header = $this->request->getHeaderLine($proxyHeader); |
|
| 123 | + if ($header === '') { |
|
| 124 | + break; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $addresses = explode(',', $header); |
|
| 128 | + foreach ($addresses as $i => $address) { |
|
| 129 | + $addresses[$i] = trim($address); |
|
| 130 | + if ($addresses[$i] === '') { |
|
| 131 | + unset($addresses[$i]); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + if ($addresses === []) { |
|
| 136 | + break; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + $client = reset($addresses); |
|
| 140 | + unset($addresses[key($addresses)]); |
|
| 141 | + |
|
| 142 | + foreach ($addresses as $address) { |
|
| 143 | + $proxies[] = $address; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return new IpAddress($client, $proxies); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Gets the client's produced media type |
|
| 152 | + * |
|
| 153 | + * @link https://tools.ietf.org/html/rfc7231#section-3.1.1.1 |
|
| 154 | + * @link https://tools.ietf.org/html/rfc7231#section-3.1.1.5 |
|
| 155 | + * |
|
| 156 | + * @return string |
|
| 157 | + */ |
|
| 158 | + public function getClientProducedMediaType(): string |
|
| 159 | + { |
|
| 160 | + $header = $this->request->getHeaderLine('Content-Type'); |
|
| 161 | + if ($header === '') { |
|
| 162 | + return ''; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $result = strstr($header, ';', true); |
|
| 166 | + if ($result === false) { |
|
| 167 | + $result = $header; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $result = trim($result); |
|
| 171 | + if ($result === '') { |
|
| 172 | + return ''; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + return strtolower($result); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Gets the client's consumed media types |
|
| 180 | + * |
|
| 181 | + * @link https://tools.ietf.org/html/rfc7231#section-1.2 |
|
| 182 | + * @link https://tools.ietf.org/html/rfc7231#section-3.1.1.1 |
|
| 183 | + * @link https://tools.ietf.org/html/rfc7231#section-5.3.2 |
|
| 184 | + * |
|
| 185 | + * @return array<string, array<string, string>> |
|
| 186 | + */ |
|
| 187 | + public function getClientConsumedMediaTypes(): array |
|
| 188 | + { |
|
| 189 | + $header = $this->request->getHeaderLine('Accept'); |
|
| 190 | + if ($header === '') { |
|
| 191 | + return []; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + $accept = header_accept_parse($header); |
|
| 195 | + if ($accept === []) { |
|
| 196 | + return []; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + $result = []; |
|
| 200 | + foreach ($accept as $type => $params) { |
|
| 201 | + $result[strtolower($type)] = $params; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + return $result; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Gets the client's consumed encodings |
|
| 209 | + * |
|
| 210 | + * @return array<string, array<string, string>> |
|
| 211 | + */ |
|
| 212 | + public function getClientConsumedEncodings(): array |
|
| 213 | + { |
|
| 214 | + $header = $this->request->getHeaderLine('Accept-Encoding'); |
|
| 215 | + if ($header === '') { |
|
| 216 | + return []; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $accept = header_accept_parse($header); |
|
| 220 | + if ($accept === []) { |
|
| 221 | + return []; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + return $accept; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Gets the client's consumed languages |
|
| 229 | + * |
|
| 230 | + * @return array<string, array<string, string>> |
|
| 231 | + */ |
|
| 232 | + public function getClientConsumedLanguages(): array |
|
| 233 | + { |
|
| 234 | + $header = $this->request->getHeaderLine('Accept-Language'); |
|
| 235 | + if ($header === '') { |
|
| 236 | + return []; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + $accept = header_accept_parse($header); |
|
| 240 | + if ($accept === []) { |
|
| 241 | + return []; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + return $accept; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Checks if the client produces one of the given media types |
|
| 249 | + * |
|
| 250 | + * @param list<string> $consumes |
|
| 251 | + * |
|
| 252 | + * @return bool |
|
| 253 | + */ |
|
| 254 | + public function clientProducesMediaType(array $consumes): bool |
|
| 255 | + { |
|
| 256 | + if ($consumes === []) { |
|
| 257 | + return true; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + $produced = $this->getClientProducedMediaType(); |
|
| 261 | + if ($produced === '') { |
|
| 262 | + return false; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + foreach ($consumes as $consumed) { |
|
| 266 | + if ($this->equalsMediaTypes($consumed, $produced)) { |
|
| 267 | + return true; |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + return false; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Checks if the client consumes one of the given media types |
|
| 276 | + * |
|
| 277 | + * @param list<string> $produces |
|
| 278 | + * |
|
| 279 | + * @return bool |
|
| 280 | + */ |
|
| 281 | + public function clientConsumesMediaType(array $produces): bool |
|
| 282 | + { |
|
| 283 | + if ($produces === []) { |
|
| 284 | + return true; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + $consumes = $this->getClientConsumedMediaTypes(); |
|
| 288 | + if ($consumes === []) { |
|
| 289 | + return true; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if (isset($consumes['*/*'])) { |
|
| 293 | + return true; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + foreach ($produces as $a) { |
|
| 297 | + foreach ($consumes as $b => $_) { |
|
| 298 | + if ($this->equalsMediaTypes($a, $b)) { |
|
| 299 | + return true; |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + return false; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Checks if the given media types are equal |
|
| 309 | + * |
|
| 310 | + * @param string $a |
|
| 311 | + * @param string $b |
|
| 312 | + * |
|
| 313 | + * @return bool |
|
| 314 | + */ |
|
| 315 | + public function equalsMediaTypes(string $a, string $b): bool |
|
| 316 | + { |
|
| 317 | + if ($a === $b) { |
|
| 318 | + return true; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + $slash = strpos($a, '/'); |
|
| 322 | + if ($slash === false || !isset($b[$slash]) || $b[$slash] !== '/') { |
|
| 323 | + return false; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + $star = $slash + 1; |
|
| 327 | + if (!isset($a[$star], $b[$star])) { |
|
| 328 | + return false; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + if (!($a[$star] === '*' || $b[$star] === '*')) { |
|
| 332 | + return false; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + return strncmp($a, $b, $star) === 0; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * {@inheritdoc} |
|
| 340 | + */ |
|
| 341 | + public function getProtocolVersion(): string |
|
| 342 | + { |
|
| 343 | + return $this->request->getProtocolVersion(); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * {@inheritdoc} |
|
| 348 | + */ |
|
| 349 | + public function withProtocolVersion($version) |
|
| 350 | + { |
|
| 351 | + $clone = clone $this; |
|
| 352 | + $clone->request = $clone->request->withProtocolVersion($version); |
|
| 353 | + |
|
| 354 | + return $clone; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * {@inheritdoc} |
|
| 359 | + */ |
|
| 360 | + public function getHeaders(): array |
|
| 361 | + { |
|
| 362 | + return $this->request->getHeaders(); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * {@inheritdoc} |
|
| 367 | + */ |
|
| 368 | + public function hasHeader($name): bool |
|
| 369 | + { |
|
| 370 | + return $this->request->hasHeader($name); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * {@inheritdoc} |
|
| 375 | + */ |
|
| 376 | + public function getHeader($name): array |
|
| 377 | + { |
|
| 378 | + return $this->request->getHeader($name); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * {@inheritdoc} |
|
| 383 | + */ |
|
| 384 | + public function getHeaderLine($name): string |
|
| 385 | + { |
|
| 386 | + return $this->request->getHeaderLine($name); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * {@inheritdoc} |
|
| 391 | + */ |
|
| 392 | + public function withHeader($name, $value) |
|
| 393 | + { |
|
| 394 | + $clone = clone $this; |
|
| 395 | + $clone->request = $clone->request->withHeader($name, $value); |
|
| 396 | + |
|
| 397 | + return $clone; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * {@inheritdoc} |
|
| 402 | + */ |
|
| 403 | + public function withAddedHeader($name, $value) |
|
| 404 | + { |
|
| 405 | + $clone = clone $this; |
|
| 406 | + $clone->request = $clone->request->withAddedHeader($name, $value); |
|
| 407 | + |
|
| 408 | + return $clone; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * {@inheritdoc} |
|
| 413 | + */ |
|
| 414 | + public function withoutHeader($name) |
|
| 415 | + { |
|
| 416 | + $clone = clone $this; |
|
| 417 | + $clone->request = $clone->request->withoutHeader($name); |
|
| 418 | + |
|
| 419 | + return $clone; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * {@inheritdoc} |
|
| 424 | + */ |
|
| 425 | + public function getBody(): StreamInterface |
|
| 426 | + { |
|
| 427 | + return $this->request->getBody(); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * {@inheritdoc} |
|
| 432 | + */ |
|
| 433 | + public function withBody(StreamInterface $body) |
|
| 434 | + { |
|
| 435 | + $clone = clone $this; |
|
| 436 | + $clone->request = $clone->request->withBody($body); |
|
| 437 | + |
|
| 438 | + return $clone; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * {@inheritdoc} |
|
| 443 | + */ |
|
| 444 | + public function getMethod(): string |
|
| 445 | + { |
|
| 446 | + return $this->request->getMethod(); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * {@inheritdoc} |
|
| 451 | + */ |
|
| 452 | + public function withMethod($method) |
|
| 453 | + { |
|
| 454 | + $clone = clone $this; |
|
| 455 | + $clone->request = $clone->request->withMethod($method); |
|
| 456 | + |
|
| 457 | + return $clone; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * {@inheritdoc} |
|
| 462 | + */ |
|
| 463 | + public function getUri(): UriInterface |
|
| 464 | + { |
|
| 465 | + return $this->request->getUri(); |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * {@inheritdoc} |
|
| 470 | + */ |
|
| 471 | + public function withUri(UriInterface $uri, $preserveHost = false) |
|
| 472 | + { |
|
| 473 | + $clone = clone $this; |
|
| 474 | + $clone->request = $clone->request->withUri($uri, $preserveHost); |
|
| 475 | + |
|
| 476 | + return $clone; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * {@inheritdoc} |
|
| 481 | + */ |
|
| 482 | + public function getRequestTarget(): string |
|
| 483 | + { |
|
| 484 | + return $this->request->getRequestTarget(); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * {@inheritdoc} |
|
| 489 | + */ |
|
| 490 | + public function withRequestTarget($requestTarget) |
|
| 491 | + { |
|
| 492 | + $clone = clone $this; |
|
| 493 | + $clone->request = $clone->request->withRequestTarget($requestTarget); |
|
| 494 | + |
|
| 495 | + return $clone; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * {@inheritdoc} |
|
| 500 | + */ |
|
| 501 | + public function getServerParams(): array |
|
| 502 | + { |
|
| 503 | + return $this->request->getServerParams(); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * {@inheritdoc} |
|
| 508 | + */ |
|
| 509 | + public function getQueryParams(): array |
|
| 510 | + { |
|
| 511 | + return $this->request->getQueryParams(); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * {@inheritdoc} |
|
| 516 | + */ |
|
| 517 | + public function withQueryParams(array $query) |
|
| 518 | + { |
|
| 519 | + $clone = clone $this; |
|
| 520 | + $clone->request = $clone->request->withQueryParams($query); |
|
| 521 | + |
|
| 522 | + return $clone; |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + /** |
|
| 526 | + * {@inheritdoc} |
|
| 527 | + */ |
|
| 528 | + public function getCookieParams(): array |
|
| 529 | + { |
|
| 530 | + return $this->request->getCookieParams(); |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * {@inheritdoc} |
|
| 535 | + */ |
|
| 536 | + public function withCookieParams(array $cookies) |
|
| 537 | + { |
|
| 538 | + $clone = clone $this; |
|
| 539 | + $clone->request = $clone->request->withCookieParams($cookies); |
|
| 540 | + |
|
| 541 | + return $clone; |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + /** |
|
| 545 | + * {@inheritdoc} |
|
| 546 | + */ |
|
| 547 | + public function getUploadedFiles(): array |
|
| 548 | + { |
|
| 549 | + return $this->request->getUploadedFiles(); |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * {@inheritdoc} |
|
| 554 | + */ |
|
| 555 | + public function withUploadedFiles(array $uploadedFiles) |
|
| 556 | + { |
|
| 557 | + $clone = clone $this; |
|
| 558 | + $clone->request = $clone->request->withUploadedFiles($uploadedFiles); |
|
| 559 | + |
|
| 560 | + return $clone; |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * {@inheritdoc} |
|
| 565 | + */ |
|
| 566 | + public function getParsedBody() |
|
| 567 | + { |
|
| 568 | + return $this->request->getParsedBody(); |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * {@inheritdoc} |
|
| 573 | + */ |
|
| 574 | + public function withParsedBody($data) |
|
| 575 | + { |
|
| 576 | + $clone = clone $this; |
|
| 577 | + $clone->request = $clone->request->withParsedBody($data); |
|
| 578 | + |
|
| 579 | + return $clone; |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * {@inheritdoc} |
|
| 584 | + */ |
|
| 585 | + public function getAttributes(): array |
|
| 586 | + { |
|
| 587 | + return $this->request->getAttributes(); |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * {@inheritdoc} |
|
| 592 | + */ |
|
| 593 | + public function getAttribute($name, $default = null) |
|
| 594 | + { |
|
| 595 | + return $this->request->getAttribute($name, $default); |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * {@inheritdoc} |
|
| 600 | + */ |
|
| 601 | + public function withAttribute($name, $value) |
|
| 602 | + { |
|
| 603 | + $clone = clone $this; |
|
| 604 | + $clone->request = $clone->request->withAttribute($name, $value); |
|
| 605 | + |
|
| 606 | + return $clone; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * {@inheritdoc} |
|
| 611 | + */ |
|
| 612 | + public function withoutAttribute($name) |
|
| 613 | + { |
|
| 614 | + $clone = clone $this; |
|
| 615 | + $clone->request = $clone->request->withoutAttribute($name); |
|
| 616 | + |
|
| 617 | + return $clone; |
|
| 618 | + } |
|
| 619 | 619 | } |
@@ -36,57 +36,57 @@ |
||
| 36 | 36 | final class JsonResponse extends Response |
| 37 | 37 | { |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Constructor of the class |
|
| 41 | - * |
|
| 42 | - * @param int $statusCode |
|
| 43 | - * @param mixed $data |
|
| 44 | - * @param int $flags |
|
| 45 | - * @param int $depth |
|
| 46 | - * |
|
| 47 | - * @throws InvalidArgumentException |
|
| 48 | - */ |
|
| 49 | - public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512) |
|
| 50 | - { |
|
| 51 | - parent::__construct($statusCode); |
|
| 39 | + /** |
|
| 40 | + * Constructor of the class |
|
| 41 | + * |
|
| 42 | + * @param int $statusCode |
|
| 43 | + * @param mixed $data |
|
| 44 | + * @param int $flags |
|
| 45 | + * @param int $depth |
|
| 46 | + * |
|
| 47 | + * @throws InvalidArgumentException |
|
| 48 | + */ |
|
| 49 | + public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512) |
|
| 50 | + { |
|
| 51 | + parent::__construct($statusCode); |
|
| 52 | 52 | |
| 53 | - $this->setBody($this->createBody($data, $flags, $depth)); |
|
| 53 | + $this->setBody($this->createBody($data, $flags, $depth)); |
|
| 54 | 54 | |
| 55 | - $this->setHeader('Content-Type', 'application/json; charset=utf-8'); |
|
| 56 | - } |
|
| 55 | + $this->setHeader('Content-Type', 'application/json; charset=utf-8'); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Creates the response body from the given JSON data |
|
| 60 | - * |
|
| 61 | - * @param mixed $data |
|
| 62 | - * @param int $flags |
|
| 63 | - * @param int $depth |
|
| 64 | - * |
|
| 65 | - * @return StreamInterface |
|
| 66 | - * |
|
| 67 | - * @throws InvalidArgumentException |
|
| 68 | - */ |
|
| 69 | - private function createBody($data, int $flags, int $depth): StreamInterface |
|
| 70 | - { |
|
| 71 | - if ($data instanceof StreamInterface) { |
|
| 72 | - return $data; |
|
| 73 | - } |
|
| 58 | + /** |
|
| 59 | + * Creates the response body from the given JSON data |
|
| 60 | + * |
|
| 61 | + * @param mixed $data |
|
| 62 | + * @param int $flags |
|
| 63 | + * @param int $depth |
|
| 64 | + * |
|
| 65 | + * @return StreamInterface |
|
| 66 | + * |
|
| 67 | + * @throws InvalidArgumentException |
|
| 68 | + */ |
|
| 69 | + private function createBody($data, int $flags, int $depth): StreamInterface |
|
| 70 | + { |
|
| 71 | + if ($data instanceof StreamInterface) { |
|
| 72 | + return $data; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - $flags |= JSON_THROW_ON_ERROR; |
|
| 75 | + $flags |= JSON_THROW_ON_ERROR; |
|
| 76 | 76 | |
| 77 | - try { |
|
| 78 | - $payload = json_encode($data, $flags, $depth); |
|
| 79 | - } catch (JsonException $e) { |
|
| 80 | - throw new InvalidArgumentException(sprintf( |
|
| 81 | - 'Unable to create JSON response due to invalid JSON data: %s', |
|
| 82 | - $e->getMessage() |
|
| 83 | - ), 0, $e); |
|
| 84 | - } |
|
| 77 | + try { |
|
| 78 | + $payload = json_encode($data, $flags, $depth); |
|
| 79 | + } catch (JsonException $e) { |
|
| 80 | + throw new InvalidArgumentException(sprintf( |
|
| 81 | + 'Unable to create JSON response due to invalid JSON data: %s', |
|
| 82 | + $e->getMessage() |
|
| 83 | + ), 0, $e); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - $stream = new PhpTempStream('r+b'); |
|
| 87 | - $stream->write($payload); |
|
| 88 | - $stream->rewind(); |
|
| 86 | + $stream = new PhpTempStream('r+b'); |
|
| 87 | + $stream->write($payload); |
|
| 88 | + $stream->rewind(); |
|
| 89 | 89 | |
| 90 | - return $stream; |
|
| 91 | - } |
|
| 90 | + return $stream; |
|
| 91 | + } |
|
| 92 | 92 | } |
@@ -32,51 +32,51 @@ |
||
| 32 | 32 | final class HtmlResponse extends Response |
| 33 | 33 | { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Constructor of the class |
|
| 37 | - * |
|
| 38 | - * @param int $statusCode |
|
| 39 | - * @param mixed $html |
|
| 40 | - * |
|
| 41 | - * @throws InvalidArgumentException |
|
| 42 | - */ |
|
| 43 | - public function __construct(int $statusCode, $html) |
|
| 44 | - { |
|
| 45 | - parent::__construct($statusCode); |
|
| 35 | + /** |
|
| 36 | + * Constructor of the class |
|
| 37 | + * |
|
| 38 | + * @param int $statusCode |
|
| 39 | + * @param mixed $html |
|
| 40 | + * |
|
| 41 | + * @throws InvalidArgumentException |
|
| 42 | + */ |
|
| 43 | + public function __construct(int $statusCode, $html) |
|
| 44 | + { |
|
| 45 | + parent::__construct($statusCode); |
|
| 46 | 46 | |
| 47 | - $this->setBody($this->createBody($html)); |
|
| 47 | + $this->setBody($this->createBody($html)); |
|
| 48 | 48 | |
| 49 | - $this->setHeader('Content-Type', 'text/html; charset=utf-8'); |
|
| 50 | - } |
|
| 49 | + $this->setHeader('Content-Type', 'text/html; charset=utf-8'); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Creates the response body from the given HTML |
|
| 54 | - * |
|
| 55 | - * @param mixed $html |
|
| 56 | - * |
|
| 57 | - * @return StreamInterface |
|
| 58 | - * |
|
| 59 | - * @throws InvalidArgumentException |
|
| 60 | - */ |
|
| 61 | - private function createBody($html): StreamInterface |
|
| 62 | - { |
|
| 63 | - if ($html instanceof StreamInterface) { |
|
| 64 | - return $html; |
|
| 65 | - } |
|
| 52 | + /** |
|
| 53 | + * Creates the response body from the given HTML |
|
| 54 | + * |
|
| 55 | + * @param mixed $html |
|
| 56 | + * |
|
| 57 | + * @return StreamInterface |
|
| 58 | + * |
|
| 59 | + * @throws InvalidArgumentException |
|
| 60 | + */ |
|
| 61 | + private function createBody($html): StreamInterface |
|
| 62 | + { |
|
| 63 | + if ($html instanceof StreamInterface) { |
|
| 64 | + return $html; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - if (is_object($html) && method_exists($html, '__toString')) { |
|
| 68 | - /** @var string */ |
|
| 69 | - $html = $html->__toString(); |
|
| 70 | - } |
|
| 67 | + if (is_object($html) && method_exists($html, '__toString')) { |
|
| 68 | + /** @var string */ |
|
| 69 | + $html = $html->__toString(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - if (!is_string($html)) { |
|
| 73 | - throw new InvalidArgumentException('Unable to create HTML response due to unexpected HTML data'); |
|
| 74 | - } |
|
| 72 | + if (!is_string($html)) { |
|
| 73 | + throw new InvalidArgumentException('Unable to create HTML response due to unexpected HTML data'); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - $stream = new PhpTempStream('r+b'); |
|
| 77 | - $stream->write($html); |
|
| 78 | - $stream->rewind(); |
|
| 76 | + $stream = new PhpTempStream('r+b'); |
|
| 77 | + $stream->write($html); |
|
| 78 | + $stream->rewind(); |
|
| 79 | 79 | |
| 80 | - return $stream; |
|
| 81 | - } |
|
| 80 | + return $stream; |
|
| 81 | + } |
|
| 82 | 82 | } |
@@ -35,258 +35,258 @@ |
||
| 35 | 35 | class Response extends Message implements ResponseInterface, StatusCodeInterface |
| 36 | 36 | { |
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * List of Reason Phrases |
|
| 40 | - * |
|
| 41 | - * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
| 42 | - * |
|
| 43 | - * @var array<int, string> |
|
| 44 | - */ |
|
| 45 | - public const REASON_PHRASES = [ |
|
| 38 | + /** |
|
| 39 | + * List of Reason Phrases |
|
| 40 | + * |
|
| 41 | + * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
| 42 | + * |
|
| 43 | + * @var array<int, string> |
|
| 44 | + */ |
|
| 45 | + public const REASON_PHRASES = [ |
|
| 46 | 46 | |
| 47 | - // 1xx |
|
| 48 | - 100 => 'Continue', |
|
| 49 | - 101 => 'Switching Protocols', |
|
| 50 | - 102 => 'Processing', |
|
| 51 | - 103 => 'Early Hints', |
|
| 47 | + // 1xx |
|
| 48 | + 100 => 'Continue', |
|
| 49 | + 101 => 'Switching Protocols', |
|
| 50 | + 102 => 'Processing', |
|
| 51 | + 103 => 'Early Hints', |
|
| 52 | 52 | |
| 53 | - // 2xx |
|
| 54 | - 200 => 'OK', |
|
| 55 | - 201 => 'Created', |
|
| 56 | - 202 => 'Accepted', |
|
| 57 | - 203 => 'Non-Authoritative Information', |
|
| 58 | - 204 => 'No Content', |
|
| 59 | - 205 => 'Reset Content', |
|
| 60 | - 206 => 'Partial Content', |
|
| 61 | - 207 => 'Multi-Status', |
|
| 62 | - 208 => 'Already Reported', |
|
| 63 | - 226 => 'IM Used', |
|
| 53 | + // 2xx |
|
| 54 | + 200 => 'OK', |
|
| 55 | + 201 => 'Created', |
|
| 56 | + 202 => 'Accepted', |
|
| 57 | + 203 => 'Non-Authoritative Information', |
|
| 58 | + 204 => 'No Content', |
|
| 59 | + 205 => 'Reset Content', |
|
| 60 | + 206 => 'Partial Content', |
|
| 61 | + 207 => 'Multi-Status', |
|
| 62 | + 208 => 'Already Reported', |
|
| 63 | + 226 => 'IM Used', |
|
| 64 | 64 | |
| 65 | - // 3xx |
|
| 66 | - 300 => 'Multiple Choices', |
|
| 67 | - 301 => 'Moved Permanently', |
|
| 68 | - 302 => 'Found', |
|
| 69 | - 303 => 'See Other', |
|
| 70 | - 304 => 'Not Modified', |
|
| 71 | - 305 => 'Use Proxy', |
|
| 72 | - 307 => 'Temporary Redirect', |
|
| 73 | - 308 => 'Permanent Redirect', |
|
| 65 | + // 3xx |
|
| 66 | + 300 => 'Multiple Choices', |
|
| 67 | + 301 => 'Moved Permanently', |
|
| 68 | + 302 => 'Found', |
|
| 69 | + 303 => 'See Other', |
|
| 70 | + 304 => 'Not Modified', |
|
| 71 | + 305 => 'Use Proxy', |
|
| 72 | + 307 => 'Temporary Redirect', |
|
| 73 | + 308 => 'Permanent Redirect', |
|
| 74 | 74 | |
| 75 | - // 4xx |
|
| 76 | - 400 => 'Bad Request', |
|
| 77 | - 401 => 'Unauthorized', |
|
| 78 | - 402 => 'Payment Required', |
|
| 79 | - 403 => 'Forbidden', |
|
| 80 | - 404 => 'Not Found', |
|
| 81 | - 405 => 'Method Not Allowed', |
|
| 82 | - 406 => 'Not Acceptable', |
|
| 83 | - 407 => 'Proxy Authentication Required', |
|
| 84 | - 408 => 'Request Timeout', |
|
| 85 | - 409 => 'Conflict', |
|
| 86 | - 410 => 'Gone', |
|
| 87 | - 411 => 'Length Required', |
|
| 88 | - 412 => 'Precondition Failed', |
|
| 89 | - 413 => 'Payload Too Large', |
|
| 90 | - 414 => 'URI Too Long', |
|
| 91 | - 415 => 'Unsupported Media Type', |
|
| 92 | - 416 => 'Range Not Satisfiable', |
|
| 93 | - 417 => 'Expectation Failed', |
|
| 94 | - 421 => 'Misdirected Request', |
|
| 95 | - 422 => 'Unprocessable Entity', |
|
| 96 | - 423 => 'Locked', |
|
| 97 | - 424 => 'Failed Dependency', |
|
| 98 | - 425 => 'Too Early', |
|
| 99 | - 426 => 'Upgrade Required', |
|
| 100 | - 428 => 'Precondition Required', |
|
| 101 | - 429 => 'Too Many Requests', |
|
| 102 | - 431 => 'Request Header Fields Too Large', |
|
| 103 | - 451 => 'Unavailable For Legal Reasons', |
|
| 75 | + // 4xx |
|
| 76 | + 400 => 'Bad Request', |
|
| 77 | + 401 => 'Unauthorized', |
|
| 78 | + 402 => 'Payment Required', |
|
| 79 | + 403 => 'Forbidden', |
|
| 80 | + 404 => 'Not Found', |
|
| 81 | + 405 => 'Method Not Allowed', |
|
| 82 | + 406 => 'Not Acceptable', |
|
| 83 | + 407 => 'Proxy Authentication Required', |
|
| 84 | + 408 => 'Request Timeout', |
|
| 85 | + 409 => 'Conflict', |
|
| 86 | + 410 => 'Gone', |
|
| 87 | + 411 => 'Length Required', |
|
| 88 | + 412 => 'Precondition Failed', |
|
| 89 | + 413 => 'Payload Too Large', |
|
| 90 | + 414 => 'URI Too Long', |
|
| 91 | + 415 => 'Unsupported Media Type', |
|
| 92 | + 416 => 'Range Not Satisfiable', |
|
| 93 | + 417 => 'Expectation Failed', |
|
| 94 | + 421 => 'Misdirected Request', |
|
| 95 | + 422 => 'Unprocessable Entity', |
|
| 96 | + 423 => 'Locked', |
|
| 97 | + 424 => 'Failed Dependency', |
|
| 98 | + 425 => 'Too Early', |
|
| 99 | + 426 => 'Upgrade Required', |
|
| 100 | + 428 => 'Precondition Required', |
|
| 101 | + 429 => 'Too Many Requests', |
|
| 102 | + 431 => 'Request Header Fields Too Large', |
|
| 103 | + 451 => 'Unavailable For Legal Reasons', |
|
| 104 | 104 | |
| 105 | - // 5xx |
|
| 106 | - 500 => 'Internal Server Error', |
|
| 107 | - 501 => 'Not Implemented', |
|
| 108 | - 502 => 'Bad Gateway', |
|
| 109 | - 503 => 'Service Unavailable', |
|
| 110 | - 504 => 'Gateway Timeout', |
|
| 111 | - 505 => 'HTTP Version Not Supported', |
|
| 112 | - 506 => 'Variant Also Negotiates', |
|
| 113 | - 507 => 'Insufficient Storage', |
|
| 114 | - 508 => 'Loop Detected', |
|
| 115 | - 510 => 'Not Extended', |
|
| 116 | - 511 => 'Network Authentication Required', |
|
| 117 | - ]; |
|
| 105 | + // 5xx |
|
| 106 | + 500 => 'Internal Server Error', |
|
| 107 | + 501 => 'Not Implemented', |
|
| 108 | + 502 => 'Bad Gateway', |
|
| 109 | + 503 => 'Service Unavailable', |
|
| 110 | + 504 => 'Gateway Timeout', |
|
| 111 | + 505 => 'HTTP Version Not Supported', |
|
| 112 | + 506 => 'Variant Also Negotiates', |
|
| 113 | + 507 => 'Insufficient Storage', |
|
| 114 | + 508 => 'Loop Detected', |
|
| 115 | + 510 => 'Not Extended', |
|
| 116 | + 511 => 'Network Authentication Required', |
|
| 117 | + ]; |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Default response status code |
|
| 121 | - * |
|
| 122 | - * @var int |
|
| 123 | - */ |
|
| 124 | - public const DEFAULT_STATUS_CODE = self::STATUS_OK; |
|
| 119 | + /** |
|
| 120 | + * Default response status code |
|
| 121 | + * |
|
| 122 | + * @var int |
|
| 123 | + */ |
|
| 124 | + public const DEFAULT_STATUS_CODE = self::STATUS_OK; |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Default response reason phrase |
|
| 128 | - * |
|
| 129 | - * @var string |
|
| 130 | - */ |
|
| 131 | - public const DEFAULT_REASON_PHRASE = self::REASON_PHRASES[self::DEFAULT_STATUS_CODE]; |
|
| 126 | + /** |
|
| 127 | + * Default response reason phrase |
|
| 128 | + * |
|
| 129 | + * @var string |
|
| 130 | + */ |
|
| 131 | + public const DEFAULT_REASON_PHRASE = self::REASON_PHRASES[self::DEFAULT_STATUS_CODE]; |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Reason phrase for unknown status code |
|
| 135 | - * |
|
| 136 | - * @var string |
|
| 137 | - */ |
|
| 138 | - public const UNKNOWN_STATUS_CODE_REASON_PHRASE = 'Unknown Status Code'; |
|
| 133 | + /** |
|
| 134 | + * Reason phrase for unknown status code |
|
| 135 | + * |
|
| 136 | + * @var string |
|
| 137 | + */ |
|
| 138 | + public const UNKNOWN_STATUS_CODE_REASON_PHRASE = 'Unknown Status Code'; |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * The response's status code |
|
| 142 | - * |
|
| 143 | - * @var int |
|
| 144 | - */ |
|
| 145 | - private int $statusCode = self::DEFAULT_STATUS_CODE; |
|
| 140 | + /** |
|
| 141 | + * The response's status code |
|
| 142 | + * |
|
| 143 | + * @var int |
|
| 144 | + */ |
|
| 145 | + private int $statusCode = self::DEFAULT_STATUS_CODE; |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * The response's reason phrase |
|
| 149 | - * |
|
| 150 | - * @var string |
|
| 151 | - */ |
|
| 152 | - private string $reasonPhrase = self::DEFAULT_REASON_PHRASE; |
|
| 147 | + /** |
|
| 148 | + * The response's reason phrase |
|
| 149 | + * |
|
| 150 | + * @var string |
|
| 151 | + */ |
|
| 152 | + private string $reasonPhrase = self::DEFAULT_REASON_PHRASE; |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * Constrictor of the class |
|
| 156 | - * |
|
| 157 | - * @param int|null $statusCode |
|
| 158 | - * @param string|null $reasonPhrase |
|
| 159 | - * @param array<string, string|string[]>|null $headers |
|
| 160 | - * @param StreamInterface|null $body |
|
| 161 | - * |
|
| 162 | - * @throws InvalidArgumentException |
|
| 163 | - * If one of the arguments isn't valid. |
|
| 164 | - */ |
|
| 165 | - public function __construct( |
|
| 166 | - ?int $statusCode = null, |
|
| 167 | - ?string $reasonPhrase = null, |
|
| 168 | - ?array $headers = null, |
|
| 169 | - ?StreamInterface $body = null |
|
| 170 | - ) { |
|
| 171 | - if (isset($statusCode)) { |
|
| 172 | - $this->setStatus($statusCode, $reasonPhrase ?? ''); |
|
| 173 | - } |
|
| 154 | + /** |
|
| 155 | + * Constrictor of the class |
|
| 156 | + * |
|
| 157 | + * @param int|null $statusCode |
|
| 158 | + * @param string|null $reasonPhrase |
|
| 159 | + * @param array<string, string|string[]>|null $headers |
|
| 160 | + * @param StreamInterface|null $body |
|
| 161 | + * |
|
| 162 | + * @throws InvalidArgumentException |
|
| 163 | + * If one of the arguments isn't valid. |
|
| 164 | + */ |
|
| 165 | + public function __construct( |
|
| 166 | + ?int $statusCode = null, |
|
| 167 | + ?string $reasonPhrase = null, |
|
| 168 | + ?array $headers = null, |
|
| 169 | + ?StreamInterface $body = null |
|
| 170 | + ) { |
|
| 171 | + if (isset($statusCode)) { |
|
| 172 | + $this->setStatus($statusCode, $reasonPhrase ?? ''); |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - if (isset($headers)) { |
|
| 176 | - $this->setHeaders($headers); |
|
| 177 | - } |
|
| 175 | + if (isset($headers)) { |
|
| 176 | + $this->setHeaders($headers); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - if (isset($body)) { |
|
| 180 | - $this->setBody($body); |
|
| 181 | - } |
|
| 182 | - } |
|
| 179 | + if (isset($body)) { |
|
| 180 | + $this->setBody($body); |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Gets the response's status code |
|
| 186 | - * |
|
| 187 | - * @return int |
|
| 188 | - */ |
|
| 189 | - public function getStatusCode(): int |
|
| 190 | - { |
|
| 191 | - return $this->statusCode; |
|
| 192 | - } |
|
| 184 | + /** |
|
| 185 | + * Gets the response's status code |
|
| 186 | + * |
|
| 187 | + * @return int |
|
| 188 | + */ |
|
| 189 | + public function getStatusCode(): int |
|
| 190 | + { |
|
| 191 | + return $this->statusCode; |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Gets the response's reason phrase |
|
| 196 | - * |
|
| 197 | - * @return string |
|
| 198 | - */ |
|
| 199 | - public function getReasonPhrase(): string |
|
| 200 | - { |
|
| 201 | - return $this->reasonPhrase; |
|
| 202 | - } |
|
| 194 | + /** |
|
| 195 | + * Gets the response's reason phrase |
|
| 196 | + * |
|
| 197 | + * @return string |
|
| 198 | + */ |
|
| 199 | + public function getReasonPhrase(): string |
|
| 200 | + { |
|
| 201 | + return $this->reasonPhrase; |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - /** |
|
| 205 | - * Creates a new instance of the response with the given status code |
|
| 206 | - * |
|
| 207 | - * @param int $code |
|
| 208 | - * @param string $reasonPhrase |
|
| 209 | - * |
|
| 210 | - * @return static |
|
| 211 | - * |
|
| 212 | - * @throws InvalidArgumentException |
|
| 213 | - * If the status isn't valid. |
|
| 214 | - */ |
|
| 215 | - public function withStatus($code, $reasonPhrase = ''): ResponseInterface |
|
| 216 | - { |
|
| 217 | - $clone = clone $this; |
|
| 218 | - $clone->setStatus($code, $reasonPhrase); |
|
| 204 | + /** |
|
| 205 | + * Creates a new instance of the response with the given status code |
|
| 206 | + * |
|
| 207 | + * @param int $code |
|
| 208 | + * @param string $reasonPhrase |
|
| 209 | + * |
|
| 210 | + * @return static |
|
| 211 | + * |
|
| 212 | + * @throws InvalidArgumentException |
|
| 213 | + * If the status isn't valid. |
|
| 214 | + */ |
|
| 215 | + public function withStatus($code, $reasonPhrase = ''): ResponseInterface |
|
| 216 | + { |
|
| 217 | + $clone = clone $this; |
|
| 218 | + $clone->setStatus($code, $reasonPhrase); |
|
| 219 | 219 | |
| 220 | - return $clone; |
|
| 221 | - } |
|
| 220 | + return $clone; |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * Sets the given status code to the response |
|
| 225 | - * |
|
| 226 | - * @param int $statusCode |
|
| 227 | - * @param string $reasonPhrase |
|
| 228 | - * |
|
| 229 | - * @return void |
|
| 230 | - * |
|
| 231 | - * @throws InvalidArgumentException |
|
| 232 | - * If the status isn't valid. |
|
| 233 | - */ |
|
| 234 | - final protected function setStatus($statusCode, $reasonPhrase): void |
|
| 235 | - { |
|
| 236 | - $this->validateStatusCode($statusCode); |
|
| 237 | - $this->validateReasonPhrase($reasonPhrase); |
|
| 223 | + /** |
|
| 224 | + * Sets the given status code to the response |
|
| 225 | + * |
|
| 226 | + * @param int $statusCode |
|
| 227 | + * @param string $reasonPhrase |
|
| 228 | + * |
|
| 229 | + * @return void |
|
| 230 | + * |
|
| 231 | + * @throws InvalidArgumentException |
|
| 232 | + * If the status isn't valid. |
|
| 233 | + */ |
|
| 234 | + final protected function setStatus($statusCode, $reasonPhrase): void |
|
| 235 | + { |
|
| 236 | + $this->validateStatusCode($statusCode); |
|
| 237 | + $this->validateReasonPhrase($reasonPhrase); |
|
| 238 | 238 | |
| 239 | - if ('' === $reasonPhrase) { |
|
| 240 | - $reasonPhrase = self::REASON_PHRASES[$statusCode] ?? self::UNKNOWN_STATUS_CODE_REASON_PHRASE; |
|
| 241 | - } |
|
| 239 | + if ('' === $reasonPhrase) { |
|
| 240 | + $reasonPhrase = self::REASON_PHRASES[$statusCode] ?? self::UNKNOWN_STATUS_CODE_REASON_PHRASE; |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - $this->statusCode = $statusCode; |
|
| 244 | - $this->reasonPhrase = $reasonPhrase; |
|
| 245 | - } |
|
| 243 | + $this->statusCode = $statusCode; |
|
| 244 | + $this->reasonPhrase = $reasonPhrase; |
|
| 245 | + } |
|
| 246 | 246 | |
| 247 | - /** |
|
| 248 | - * Validates the given status code |
|
| 249 | - * |
|
| 250 | - * @link https://tools.ietf.org/html/rfc7230#section-3.1.2 |
|
| 251 | - * |
|
| 252 | - * @param mixed $statusCode |
|
| 253 | - * |
|
| 254 | - * @return void |
|
| 255 | - * |
|
| 256 | - * @throws InvalidArgumentException |
|
| 257 | - * If the status code isn't valid. |
|
| 258 | - */ |
|
| 259 | - private function validateStatusCode($statusCode): void |
|
| 260 | - { |
|
| 261 | - if (!is_int($statusCode)) { |
|
| 262 | - throw new InvalidArgumentException('HTTP status code must be an integer'); |
|
| 263 | - } |
|
| 247 | + /** |
|
| 248 | + * Validates the given status code |
|
| 249 | + * |
|
| 250 | + * @link https://tools.ietf.org/html/rfc7230#section-3.1.2 |
|
| 251 | + * |
|
| 252 | + * @param mixed $statusCode |
|
| 253 | + * |
|
| 254 | + * @return void |
|
| 255 | + * |
|
| 256 | + * @throws InvalidArgumentException |
|
| 257 | + * If the status code isn't valid. |
|
| 258 | + */ |
|
| 259 | + private function validateStatusCode($statusCode): void |
|
| 260 | + { |
|
| 261 | + if (!is_int($statusCode)) { |
|
| 262 | + throw new InvalidArgumentException('HTTP status code must be an integer'); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - if (! ($statusCode >= 100 && $statusCode <= 599)) { |
|
| 266 | - throw new InvalidArgumentException('Invalid HTTP status code'); |
|
| 267 | - } |
|
| 268 | - } |
|
| 265 | + if (! ($statusCode >= 100 && $statusCode <= 599)) { |
|
| 266 | + throw new InvalidArgumentException('Invalid HTTP status code'); |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * Validates the given reason phrase |
|
| 272 | - * |
|
| 273 | - * @link https://tools.ietf.org/html/rfc7230#section-3.1.2 |
|
| 274 | - * |
|
| 275 | - * @param mixed $reasonPhrase |
|
| 276 | - * |
|
| 277 | - * @return void |
|
| 278 | - * |
|
| 279 | - * @throws InvalidArgumentException |
|
| 280 | - * If the reason phrase isn't valid. |
|
| 281 | - */ |
|
| 282 | - private function validateReasonPhrase($reasonPhrase): void |
|
| 283 | - { |
|
| 284 | - if (!is_string($reasonPhrase)) { |
|
| 285 | - throw new InvalidArgumentException('HTTP reason phrase must be a string'); |
|
| 286 | - } |
|
| 270 | + /** |
|
| 271 | + * Validates the given reason phrase |
|
| 272 | + * |
|
| 273 | + * @link https://tools.ietf.org/html/rfc7230#section-3.1.2 |
|
| 274 | + * |
|
| 275 | + * @param mixed $reasonPhrase |
|
| 276 | + * |
|
| 277 | + * @return void |
|
| 278 | + * |
|
| 279 | + * @throws InvalidArgumentException |
|
| 280 | + * If the reason phrase isn't valid. |
|
| 281 | + */ |
|
| 282 | + private function validateReasonPhrase($reasonPhrase): void |
|
| 283 | + { |
|
| 284 | + if (!is_string($reasonPhrase)) { |
|
| 285 | + throw new InvalidArgumentException('HTTP reason phrase must be a string'); |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - if (!preg_match(Header::RFC7230_VALID_FIELD_VALUE, $reasonPhrase)) { |
|
| 289 | - throw new InvalidArgumentException('Invalid HTTP reason phrase'); |
|
| 290 | - } |
|
| 291 | - } |
|
| 288 | + if (!preg_match(Header::RFC7230_VALID_FIELD_VALUE, $reasonPhrase)) { |
|
| 289 | + throw new InvalidArgumentException('Invalid HTTP reason phrase'); |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | 292 | } |
@@ -27,13 +27,13 @@ |
||
| 27 | 27 | final class PhpMemoryStream extends Stream |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor of the class |
|
| 32 | - * |
|
| 33 | - * @param string $mode |
|
| 34 | - */ |
|
| 35 | - public function __construct(string $mode = 'r+b') |
|
| 36 | - { |
|
| 37 | - parent::__construct(fopen('php://memory', $mode)); |
|
| 38 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor of the class |
|
| 32 | + * |
|
| 33 | + * @param string $mode |
|
| 34 | + */ |
|
| 35 | + public function __construct(string $mode = 'r+b') |
|
| 36 | + { |
|
| 37 | + parent::__construct(fopen('php://memory', $mode)); |
|
| 38 | + } |
|
| 39 | 39 | } |
@@ -28,18 +28,18 @@ |
||
| 28 | 28 | final class PhpInputStream extends Stream |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Constructor of the class |
|
| 33 | - */ |
|
| 34 | - public function __construct() |
|
| 35 | - { |
|
| 36 | - $input = fopen('php://input', 'rb'); |
|
| 37 | - $resource = fopen('php://temp', 'r+b'); |
|
| 31 | + /** |
|
| 32 | + * Constructor of the class |
|
| 33 | + */ |
|
| 34 | + public function __construct() |
|
| 35 | + { |
|
| 36 | + $input = fopen('php://input', 'rb'); |
|
| 37 | + $resource = fopen('php://temp', 'r+b'); |
|
| 38 | 38 | |
| 39 | - stream_copy_to_stream($input, $resource); |
|
| 39 | + stream_copy_to_stream($input, $resource); |
|
| 40 | 40 | |
| 41 | - parent::__construct($resource); |
|
| 41 | + parent::__construct($resource); |
|
| 42 | 42 | |
| 43 | - $this->rewind(); |
|
| 44 | - } |
|
| 43 | + $this->rewind(); |
|
| 44 | + } |
|
| 45 | 45 | } |