Composer package providing HTTP Methods, Status Codes and Reason Phrases for PHP

Overview

HTTP Enums For PHP 8.1 and above

PHP Version Require Latest Stable Version Latest Unstable Version License

This package provides HTTP Methods, Status Codes and Reason Phrases as PHP 8.1+ enums

All IANA registered HTTP Status codes and corresponding Reason Phrases as of the latest update on 2018-09-21 are supported.

This includes the HTTP Methods defined in RFC 5789 and RFC 7231, as well as all Status Codes and Reason Phrases as defined in HTTP/1.1 (RFC 7231, RFC 7232, RFC 7233, RFC 7235) and HTTP/2 (RFC 7540) as well as other RFC's defining HTTP status codes such as WebDAV (RFC 2518, RFC 4918, RFC 5842, RFC 8144) and more (RFC 8297, RFC 3229, RFC 7538, RFC 7694, RFC 6585, RFC 7725, RFC 2295, RFC 2774)

Requirements

  • PHP 8.1 or above

Installation

Composer:

composer require alexanderpas/http-enum

Manually (Without Composer):

include the src/Method.php file in order to use the HTTP methods enum.

include both the src/ReasonPhrase.php file as well as the src/StatusCode.php file in order to use the HTTP Status Code enum or the HTTP Reason Phrase enum.

Available Enums and Enum methods

All available Enums live in the \Alexanderpas\Common\HTTP namespace.

  • HTTP Methods are represented by the \Alexanderpas\Common\HTTP\Method enum.
  • HTTP Status Codes are represented by the \Alexanderpas\Common\HTTP\StatusCode enum.
  • HTTP Reason Phrases are represented by the \Alexanderpas\Common\HTTP\ReasonPhrase enum.

In addition to the Enum methods available by default on Backed Enums, the following Enum methods are available.

  • Method::fromName(string $name): Method Gives back a HTTP method enum when provided with a valid uppercase HTTP method. (such as 'GET' or 'POST')
  • StatusCode::fromInteger(int $integer): StatusCode Gives back a HTTP Status Code enum when provided with a valid HTTP status code as integer. (such as 200 or 404)
  • StatusCode::fromName(string $name): StatusCode Gives back a HTTP Status Code enum when provided with a valid HTTP status code as a HTTP_ prefixed string. (such as 'HTTP_200' or 'HTTP_404')
  • ReasonPhrase::fromInteger(int $integer): ReasonPhrase Gives back a HTTP Reason Phrase enum when provided with a valid HTTP status code as integer. (such as 200 or 404)
  • ReasonPhrase::fromName(string $name): ReasonPhrase Gives back a HTTP Reason Phrase enum when provided with a valid HTTP status code as a HTTP_ prefixed string. (such as 'HTTP_200' or 'HTTP_404')

All of the above methods also have a try variant (such as Method::tryFromName(?string $name): ?Method), which returns null if an invalid value of the correct type has been given instead of thowing an exception.

Additionally, you can change between Status Code enums and Reason Phrase enums using the following methods:

  • ReasonPhrase::fromStatusCode(StatusCode $statusCode): ReasonPhrase changes a Status Code enum into the corresponding Reason Phrase enum.
  • StatusCode::fromReasonPhrase(ReasonPhrase $reasonPhrase): StatusCode changes a Reason Phrase enum into the corresponding Status Code enum.

These methods do not have a try variant.

You can get the respective string or integer representation as usual by reading the value attribute on the enum.

License

Copyright Alexander Pas 2021. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

Notes

You might also like...
A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to brute force attack via the http protocol.
A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to brute force attack via the http protocol.

Brutal A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to apply brute force attack via the http protocol. Bru

HTTP header kit for PHP 7.1+ (incl. PHP 8) based on PSR-7

HTTP header kit for PHP 7.1+ (incl. PHP 8) based on PSR-7 Installation composer require sunrise/http-header-kit How to use? HTTP Header Collection Mor

Express.php is a new HTTP - Server especially made for RESTful APIs written in PHP.

express.php Express.php is a new HTTP - Server especially made for RESTful APIs written in PHP. Features Fast The Library is handles requests fast and

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

This is a port of the VCR Ruby library to PHP. Record your test suite's HTTP interactions and replay them during future test runs for fast, determinis

Application for logging HTTP and DNS Requests

Request Logger Made by Adam Langley ( https://twitter.com/adamtlangley ) What is it? Request logger is a free and open source utility for logging HTTP

librestful is a virion for PocketMine servers that make easier, readable code and for async http requests.

librestful is a virion for PocketMine servers that make easier, readable code for async rest requests.

Pushpin is a publish-subscribe server, supporting HTTP and WebSocket connections.
Pushpin is a publish-subscribe server, supporting HTTP and WebSocket connections.

Pushpin and 1 million connections Pushpin is a publish-subscribe server, supporting HTTP and WebSocket connections. This repository contains instructi

Event-driven, streaming HTTP client and server implementation for ReactPHP

HTTP Event-driven, streaming HTTP client and server implementation for ReactPHP. This HTTP library provides re-usable implementations for an HTTP clie

A simple yet powerful HTTP metadata and assets provider for NFT collections using Symfony

Safe NFT Metadata Provider A simple yet powerful HTTP metadata and assets provider for NFT collections using Symfony.

Comments
  • use `defined()` check instead of `match` statement duplicating the full list of cases.

    use `defined()` check instead of `match` statement duplicating the full list of cases.

    This is more to open a discussion about Enum usage than anything.

    IMO the match pattern used throughout this lib is kinda repetitive and will easily lead to mistakes if a new case is added and forgotten in the tryFrom* methods. Is there anything that speaks against using the foreach in this PR? Was the match only used because it's fun new syntax or is there any benefit I missed?

    Would there be value in adding a tryFromName to all enums in PHP 8.2?

    Alternative implementation btw, even shorter but not sure if more efficient, could be:

        public static function tryFromName(?string $name): ?StatusCode
        {
            return defined("self::$name") ? constant("self::$name") : null;
        }
    

    /cc @iluuu1994 @crell would be curious to have your opinion here.

    enhancement hacktoberest-accepted hacktoberfest-accepted 
    opened by Seldaek 4
  • Feat: Enable lowercase methods + various minor changes

    Feat: Enable lowercase methods + various minor changes

    Hey hi!

    Thanks for this package, I get it will be useful when 8.1 will be the default PHP version :)

    I made minor changes, but I was unable to test that locally as I don't have php 8.1 available yet on Nixos.

    Among the changes, here are the list:

    1. Insensitive case HTTP methods
    2. Replace is_null() with null ===
    3. Use self
    4. Reduce amount of local variables here and there

    Let me know what you think!

    Regards.

    opened by drupol 3
  • Allow loading Methods by name in a case insenitive manner.

    Allow loading Methods by name in a case insenitive manner.

    In order to load a Method by name, at the moment it is required to provide the Method in UPPERCASE.

    As an enhancement, we should also allow loading of the Methods in a case insensitive manner, so when you load it by name with PATCH, patch, Patch or pAtcH, it all gives back the same result.

    enhancement good first issue 
    opened by alexanderpas 0
  • README: Update to reflect recent improvement to `Method::fromName()`

    README: Update to reflect recent improvement to `Method::fromName()`

    This PR updates the README.md to correctly reflect the internal behaviour of Method::fromName() which was improved to work with case insensitive request methods as per PR#2, discussed in #3.

    opened by sjokkateer 0
Releases(1.0.0)
Just HTTP Status Codes is a great way to empower your project with clean practice 💫

The Simplest & Cleanest HTTP Status Codes for PHP. All PHP HTTP Status Codes are stored into beautiful constant names ?? Ideal when you develop an API that involves various HTTP codes ??

♚ PH⑦ de Soria™♛ 10 Dec 18, 2022
The HttpClient component provides powerful methods to fetch HTTP resources synchronously or asynchronously.

HttpClient component The HttpClient component provides powerful methods to fetch HTTP resources synchronously or asynchronously. Resources Documentati

Symfony 1.7k Jan 6, 2023
Declarative HTTP Clients using Guzzle HTTP Library and PHP 8 Attributes

Waffler How to install? $ composer require waffler/waffler This package requires PHP 8 or above. How to test? $ composer phpunit Quick start For our e

Waffler 3 Aug 26, 2022
🐼 Framework agnostic package using asynchronous HTTP requests and PHP generators to load paginated items of JSON APIs into Laravel lazy collections.

Framework agnostic package using asynchronous HTTP requests and generators to load paginated items of JSON APIs into Laravel lazy collections.

Andrea Marco Sartori 61 Dec 3, 2022
Zenscrape package is a simple PHP HTTP client-provider that makes it easy to parsing site-pages

Zenscrape package is a simple PHP HTTP client-provider that makes it easy to parsing site-pages

Andrei 3 Jan 17, 2022
PHP Curl - This package can send HTTP requests to a given site using Curl.

PHP Curl This package can send HTTP requests to a given site using Curl. It provides functions that can take several types of parameters to configure

Mehmet Can 1 Oct 27, 2022
This package provides the database factory experience to fake Http calls in your testsuite.

This package provides the database factory experience to fake Http calls in your testsuite

DIJ 11 May 2, 2022
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.

Requests for PHP Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python librar

null 3.5k Dec 31, 2022
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.

Requests for PHP Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python librar

null 3.5k Dec 31, 2022
PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs

PHP Curl Class: HTTP requests made easy PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs. Installation Requirements Quic

null 3.1k Jan 5, 2023