lukasoppermann/http-status

A minimal package for working with HTTP statuses.

4.0.0 2023-06-23 18:39 UTC

README

Latest Version on Packagist Software License Build Status Build Status Total Downloads

The Httpstatus package provides an easy and convinent way to retrieve the standard status text (english) for any given HTTP status code. You can also get the HTTP status code for any valid status text. Additionally this package provides all status codes as constants, to use for a better readability of your code (HTTP_OK is just much easier to understand than 200).

Install

Via Composer

$ composer require lukasoppermann/http-status

Usage

$Httpstatus = new Lukasoppermann\Httpstatus\Httpstatus();

// (optional) specify language, default: en
$Httpstatus->setLanguage('en'); // Currently supported: en, fr

// get status text from code
echo $Httpstatus->getReasonPhrase(301); // Moved Permanently

// get the status code by text
echo $Httpstatus->getStatusCode('Method Not Allowed'); // 405

// check if status code exists
echo $Httpstatus->hasStatusCode(404); // true
echo $Httpstatus->hasStatusCode(601); // false

// check if reason phrase exists
echo $Httpstatus->hasReasonPhrase('Method Not Allowed'); // true
echo $Httpstatus->hasReasonPhrase('Does not exist'); // false

// determine the type (or "class") of the code
echo $Httpstatus->getResponseClass(503); // Httpstatus::CLASS_SERVER_ERROR

This package provides an interface with all status codes as constanst for your convenience. When developing a class that deals with HTTP status codes, simply implement the interface and start using constants instead of magic numbers for more readable and understandable code.

use Lukasoppermann\Httpstatus\Httpstatuscodes;

class Response implements Httpstatuscodes{

  public function someMethod(){
      // ... some logic
      return respond(self::HTTP_CREATED, $json);
  }

}

It is also possible to directly use a constant from the Interface if you so desire.

use Lukasoppermann\Httpstatus\Httpstatuscodes as Status;

class UserTest{

  public function test_create_new_user(){
      $this->assertEquals(Status::HTTP_CREATED, $response->status());
  }

}

Configure

If you want to localize status texts, you can supply an array when initiating the class. You may overwrite all or just some codes. A reason phrase has to be unique and may only be used for one status code.

// add custom texts
$Httpstatus = new Lukasoppermann\Httpstatus\Httpstatus([
    200 => 'Kein Inhalt',
    404 => 'Nicht gefunden',
]);

HTTP status code classes (from RFC7231)

The first digit of the status-code defines the class of response. The last two digits do not have any categorization role. There are five values for the first digit:

Available HTTP status codes

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email oppermann.lukas@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.