Skip to content

Commit

Permalink
Merge pull request #304 from bowphp/fix/str
Browse files Browse the repository at this point in the history
Implement feature for improve http and str classes
  • Loading branch information
papac authored Dec 20, 2024
2 parents 414ab4b + 5673a7f commit be3b14d
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 67 deletions.
149 changes: 149 additions & 0 deletions src/Http/HttpStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Bow\Http;

use InvalidArgumentException;

final class HttpStatus
{
private const STATUS = [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
208 => 'Already Reported',
226 => 'IM Used',
300 => 'Multipe Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication',
408 => 'Request Time Out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Payload Too Large',
414 => 'URI Too Long',
415 => 'Unsupport Media',
416 => 'Range Not Statisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
421 => 'Misdirected Request',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
444 => 'Connection Closed Without Response',
451 => 'Unavailable For Legal Reasons',
499 => 'Client Closed Request',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
];

public const CONTINUE = 100;
public const SWITCHING_PROTOCOLS = 101;
public const PROCESSING = 102;
public const OK = 200;
public const CREATED = 201;
public const ACCEPTED = 202;
public const NO_CONTENT = 204;
public const RESET_CONTENT = 205;
public const PARTIAL_CONTENT = 206;
public const MULTI_STATUS = 207;
public const ALREADY_REPORTED = 208;
public const IM_USED = 226;
public const MULTIPE_CHOICES = 300;
public const MOVED_PERMANENTLY = 301;
public const FOUND_ = 302;
public const SEE_OTHER = 303;
public const NOT_MODIFIED = 304;
public const USE_PROXY = 305;
public const TEMPORARY_REDIRECT = 307;
public const PERMANENT_REDIRECT = 308;
public const BAD_REQUEST = 400;
public const UNAUTHORIZED_ = 401;
public const PAYMENT_REQUIRED = 402;
public const FORBIDDEN_ = 403;
public const NOT_FOUND = 404;
public const METHOD_NOT_ALLOWED = 405;
public const NOT_ACCEPTABLE = 406;
public const PROXY_AUTHENTICATION = 407;
public const REQUEST_TIME_OUT = 408;
public const CONFLICT_ = 409;
public const GONE_ = 410;
public const LENGTH_REQUIRED = 411;
public const PRECONDITION_FAILED = 412;
public const PAYLOAD_TOO_LARGE = 413;
public const URI_TOO_LONG = 414;
public const UNSUPPORT_MEDIA = 415;
public const RANGE_NOT_STATISFIABLE = 416;
public const EXPECTATION_FAILED = 417;
public const I_M_A_TEAPOT = 418;
public const MISDIRECTED_REQUEST = 421;
public const UNPROCESSABLE_ENTITY = 422;
public const LOCKED_ = 423;
public const FAILED_DEPENDENCY = 424;
public const UPGRADE_REQUIRED = 426;
public const PRECONDITION_REQUIRED = 428;
public const TOO_MANY_REQUESTS = 429;
public const REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
public const CONNECTION_CLOSED_WITHOUT_RESPONSE = 444;
public const UNAVAILABLE_FOR_LEGAL_REASONS = 451;
public const CLIENT_CLOSED_REQUEST = 499;
public const INTERNAL_SERVER_ERROR = 500;
public const NOT_IMPLEMENTED = 501;
public const BAD_GATEWAY = 502;
public const SERVICE_UNAVAILABLE = 503;
public const GATEWAY_TIMEOUT = 504;
public const HTTP_VERSION_NOT_SUPPORTED = 505;

/**
* Get the message
*
* @param int $code
* @return string
*/
public static function getMessage(int $code): string
{
if (!isset(static::STATUS[$code])) {
throw new InvalidArgumentException("The code {$code} is not exists");
}

return static::STATUS[$code];
}

/**
* Get the codes
*
* @return array
*/
public static function getCodes(): array
{
return array_keys(static::STATUS);
}
}
68 changes: 2 additions & 66 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,6 @@

class Response implements ResponseInterface
{
/**
* Valid http code list for the app Except that
* the user can himself redefine these codes
* if it uses the `header` function of php
*/
private static array $status_codes = [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
208 => 'Already Reported',
226 => 'IM Used',
300 => 'Multipe Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication',
408 => 'Request Time Out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Payload Too Large',
414 => 'URI Too Long',
415 => 'Unsupport Media',
416 => 'Range Not Statisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
421 => 'Misdirected Request',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
444 => 'Connection Closed Without Response',
451 => 'Unavailable For Legal Reasons',
499 => 'Client Closed Request',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
];

/**
* The Response instamce
*
Expand Down Expand Up @@ -283,8 +219,8 @@ public function status(int $code): Response
{
$this->code = $code;

if (in_array($code, array_keys(static::$status_codes), true)) {
@header('HTTP/1.1 ' . $code . ' ' . static::$status_codes[$code], $this->override, $code);
if (in_array($code, HttpStatus::getCodes(), true)) {
@header('HTTP/1.1 ' . $code . ' ' . HttpStatus::getMessage($code), $this->override, $code);
}

return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public static function words(string $words, int $len): string
if (!isset($wordParts[$i])) {
break;
}

$sentence .= ' ' . $wordParts[$i];
}

Expand Down
5 changes: 5 additions & 0 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Bow\Http\Response;
use Bow\Security\Hash;
use Bow\Session\Cookie;
use Bow\Http\HttpStatus;
use Bow\Security\Crypto;
use Bow\Session\Session;
use Bow\Storage\Storage;
Expand Down Expand Up @@ -1149,6 +1150,10 @@ function app_assets(string $filename): string
*/
function app_abort(int $code = 500, string $message = '')
{
if (strlen($message) == 0) {
$message = HttpStatus::getMessage($code);
}

throw new HttpException($message, $code);
}
}
Expand Down

0 comments on commit be3b14d

Please sign in to comment.