这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Make sure your request is meaningful and you have tested the app locally before

#### Requirements

- [PHP 7.4+](https://www.apachefriends.org/index.html)
- [PHP 8.1+](https://www.apachefriends.org/index.html)
- [Composer](https://getcomposer.org)

#### Linux
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@
"classmap": [
"src/models/",
"src/views/",
"src/controllers/"
"src/controllers/",
"src/enums/",
"src/exceptions/",
"src/interfaces/"
]
},
"require": {
"php": "^7.4|^8.0",
"php": "^8.1",
"vlucas/phpdotenv": "^5.3"
},
"require-dev": {
"phpunit/phpunit": "^11"
},
"scripts": {
"start": "php7 -S localhost:8000 -t src || php -S localhost:8000 -t src",
"start": "php8 -S localhost:8000 -t src || php -S localhost:8000 -t src",
"test": "./vendor/bin/phpunit --testdox tests",
"format:check": "prettier --check *.md **/**/*.{php,md,js,css} --print-width 120",
"format": "prettier --write *.md **/**/*.{php,md,js,css} --print-width 120"
Expand Down
14 changes: 13 additions & 1 deletion src/controllers/RendererController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ class RendererController
*/
private $params;

/**
* @var ResponseEnum $statusCode Response status code
*/
private ResponseEnum $statusCode = ResponseEnum::HTTP_OK;

/**
* Construct RendererController
*
* @param array<string, string> $params request parameters
*/
public function __construct($params)
public function __construct(array $params)
{
$this->params = $params;

Expand All @@ -40,6 +45,10 @@ public function __construct($params)
$this->model = new ErrorModel(__DIR__ . "/../templates/error.php", $error->getMessage());
// create error rendering view
$this->view = new ErrorView($this->model);

// set status code
$this->statusCode =
$error instanceof IStatusException ? $error->getStatus() : ResponseEnum::HTTP_INTERNAL_SERVER_ERROR;
}
}

Expand Down Expand Up @@ -89,6 +98,9 @@ public function setHeaders(): void

// set cache headers
$this->setCacheRefreshDaily();

// set status code
http_response_code($this->statusCode->value);
}

/**
Expand Down
90 changes: 90 additions & 0 deletions src/enums/ResponseEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* Enumeration of HTTP response status codes.
*
* This enum represents the standard HTTP response status codes
* defined by the Internet Assigned Numbers Authority (IANA) in
* the Hypertext Transfer Protocol (HTTP) status code registry.
* Each status code is associated with an integer value and a
* descriptive name.
*
* Usage example:
* if (ResponseEnum::HTTP_OK->value === 200) {
* echo "Request was successful.";
* }
*/
enum ResponseEnum: int
{
// 1xx: Informational
case HTTP_CONTINUE = 100;
case HTTP_SWITCHING_PROTOCOLS = 101;
case HTTP_PROCESSING = 102;

// 2xx: Success
case HTTP_OK = 200;
case HTTP_CREATED = 201;
case HTTP_ACCEPTED = 202;
case HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
case HTTP_NO_CONTENT = 204;
case HTTP_RESET_CONTENT = 205;
case HTTP_PARTIAL_CONTENT = 206;
case HTTP_MULTI_STATUS = 207;
case HTTP_ALREADY_REPORTED = 208;
case HTTP_IM_USED = 226;

// 3xx: Redirection
case HTTP_MULTIPLE_CHOICES = 300;
case HTTP_MOVED_PERMANENTLY = 301;
case HTTP_FOUND = 302;
case HTTP_SEE_OTHER = 303;
case HTTP_NOT_MODIFIED = 304;
case HTTP_USE_PROXY = 305;
case HTTP_SWITCH_PROXY = 306; // No longer used
case HTTP_TEMPORARY_REDIRECT = 307;
case HTTP_PERMANENT_REDIRECT = 308;

// 4xx: Client Error
case HTTP_BAD_REQUEST = 400;
case HTTP_UNAUTHORIZED = 401;
case HTTP_PAYMENT_REQUIRED = 402;
case HTTP_FORBIDDEN = 403;
case HTTP_NOT_FOUND = 404;
case HTTP_METHOD_NOT_ALLOWED = 405;
case HTTP_NOT_ACCEPTABLE = 406;
case HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
case HTTP_REQUEST_TIMEOUT = 408;
case HTTP_CONFLICT = 409;
case HTTP_GONE = 410;
case HTTP_LENGTH_REQUIRED = 411;
case HTTP_PRECONDITION_FAILED = 412;
case HTTP_PAYLOAD_TOO_LARGE = 413;
case HTTP_URI_TOO_LONG = 414;
case HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
case HTTP_RANGE_NOT_SATISFIABLE = 416;
case HTTP_EXPECTATION_FAILED = 417;
case HTTP_IM_A_TEAPOT = 418;
case HTTP_MISDIRECTED_REQUEST = 421;
case HTTP_UNPROCESSABLE_ENTITY = 422;
case HTTP_LOCKED = 423;
case HTTP_FAILED_DEPENDENCY = 424;
case HTTP_TOO_EARLY = 425;
case HTTP_UPGRADE_REQUIRED = 426;
case HTTP_PRECONDITION_REQUIRED = 428;
case HTTP_TOO_MANY_REQUESTS = 429;
case HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
case HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;

// 5xx: Server Error
case HTTP_INTERNAL_SERVER_ERROR = 500;
case HTTP_NOT_IMPLEMENTED = 501;
case HTTP_BAD_GATEWAY = 502;
case HTTP_SERVICE_UNAVAILABLE = 503;
case HTTP_GATEWAY_TIMEOUT = 504;
case HTTP_HTTP_VERSION_NOT_SUPPORTED = 505;
case HTTP_VARIANT_ALSO_NEGOTIATES = 506;
case HTTP_INSUFFICIENT_STORAGE = 507;
case HTTP_LOOP_DETECTED = 508;
case HTTP_NOT_EXTENDED = 510;
case HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511;
}
9 changes: 9 additions & 0 deletions src/exceptions/UnprocessableEntityException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class UnprocessableEntityException extends InvalidArgumentException implements IStatusException
{
public function getStatus(): ResponseEnum
{
return ResponseEnum::HTTP_UNPROCESSABLE_ENTITY;
}
}
6 changes: 6 additions & 0 deletions src/interfaces/IStatusException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

interface IStatusException
{
public function getStatus(): ResponseEnum;
}
6 changes: 3 additions & 3 deletions src/models/ErrorModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
class ErrorModel
{
/** @var string $message Text to display */
public $message;
public string $message;

/** @var string $template Path to template file */
public $template;
public string $template;

/**
* Construct ErrorModel
*
* @param string $message Text to display
* @param string $template Path to the template file
*/
public function __construct($template, $message)
public function __construct(string $template, string $message)
{
$this->message = $message;
$this->template = $template;
Expand Down
8 changes: 5 additions & 3 deletions src/models/GoogleFontConverter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Class for converting Google Fonts to base 64 for displaying through SVG image
*/
Expand All @@ -10,9 +12,9 @@ class GoogleFontConverter
*
* @param string $font Google Font to fetch
* @param string $text Text to display in font
* @return string|false The CSS for displaying the font
* @return string The CSS for displaying the font
*/
public static function fetchFontCSS($font, $weight, $text)
public static function fetchFontCSS($font, $weight, $text): string
{
$url =
"https://fonts.googleapis.com/css2?" .
Expand Down Expand Up @@ -70,7 +72,7 @@ private static function curlGetContents($url): string
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode != 200) {
if ($httpCode != ResponseEnum::HTTP_OK->value) {
throw new InvalidArgumentException("Failed to fetch Google Font from API.");
}
return $response;
Expand Down
6 changes: 3 additions & 3 deletions src/models/RendererModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function __construct($template, $params)
private function checkLines($lines)
{
if (!$lines) {
throw new InvalidArgumentException("Lines parameter must be set.");
throw new UnprocessableEntityException("Lines parameter must be set.");
}
if (strlen($this->separator) === 1) {
$lines = rtrim($lines, $this->separator);
Expand Down Expand Up @@ -176,7 +176,7 @@ private function checkNumberPositive($num, $field)
{
$digits = intval(preg_replace("/[^0-9\-]/", "", $num));
if ($digits <= 0) {
throw new InvalidArgumentException("$field must be a positive number.");
throw new UnprocessableEntityException("$field must be a positive number.");
}
return $digits;
}
Expand All @@ -192,7 +192,7 @@ private function checkNumberNonNegative($num, $field)
{
$digits = intval(preg_replace("/[^0-9\-]/", "", $num));
if ($digits < 0) {
throw new InvalidArgumentException("$field must be a non-negative number.");
throw new UnprocessableEntityException("$field must be a non-negative number.");
}
return $digits;
}
Expand Down