Giter Site home page Giter Site logo

teapot's Introduction

Teapot

This is a very simple library that aims to aid verbosity in any Web-based application by defining clearly the HTTP 1.1 response codes as constants. It includes two main components: an interface, which contains the constants, and an exception specifically for HTTP.

Build Status Scrutinizer Quality Score Dependency Status Latest Stable Version Total Downloads Latest Unstable Version License

Using the StatusCodes interface

Assuming for a moment a PHPUnit test on a cURL client response:

<?php

/**
 * @dataProvider someUrlProvider
 */
public function testResponseIsOK($url)
{
    $client = new Client($url);
    $response = $client->get();

    $this->assertSame(200, $response->getStatusCode());
}

This becomes:

<?php

use Teapot\StatusCode;
...
$this->assertSame(StatusCode::OK, $response->getStatusCode());

While this is a trivial example, the additional verbosity of the code is clearer with other HTTP status codes:

<?php

use Teapot\StatusCode;

$code = $response->getStatusCode();

$this->assertNotEquals(StatusCode::NOT_FOUND, $code);
$this->assertNotEquals(StatusCode::FORBIDDEN, $code);
$this->assertNotEquals(StatusCode::MOVED_PERMANENTLY, $code);
$this->assertSame(StatusCode::CREATED, $code);

As StatusCode is an interface without any methods, you can directly implement it if you prefer:

<?php

use Teapot\StatusCode;

class FooController implements StatusCode
{
    public function badAction()
    {
        if ($this->request->getMethod() == 'POST') {
            throw new \Exception('Bad!', self::METHOD_NOT_ALLOWED);
        }
    }
}

This may be beneficial in an abstract class, so that child classes don't need to explicitly use the interface.

There are various "helper" interfaces within the library, such as WebDAV and Http. Additionally, the various status codes are split into the RFCs that defined them: the Http helper interface extends RFCs 2616, 2324, and 2774, for example. This allows you very granular control of what status codes you want to allow within your application.

All constants have doc blocks that use the official W3C and IETF draft specification descriptions of the status code, to aid IDEs and for reference.

Using the HttpException

The HttpException is very straightforward. It simply is a named exception to aid verbosity:

<?php

use Teapot\HttpException;
use Teapot\StatusCode;

throw new HttpException(
    'Sorry this page does not exist!',
    StatusCode::NOT_FOUND
);

The exception itself uses the StatusCode interface, allowing you to avoid manually and explicitly importing it if you prefer:

<?php

use Teapot\HttpException;

throw new HttpException(
    'Sorry this page does not exist!',
    HttpException::NOT_FOUND
);

Installation

Run the following command.

composer require shrikeh/teapot

Coding Standards

The entire library is intended to be PSR-1, PSR-2 and PSR-4 compliant.

Get in touch

If you have any suggestions, feel free to email me at [email protected] or ping me on Twitter with @barney_hanlon.

teapot's People

Contributors

alecsammon avatar judgej avatar localheinz avatar nousefreak avatar shrikeh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.