Skip to content

ddeboer/message-decorator

Repository files navigation

Message Decorator

Latest Version Software License Build Status Code Coverage Quality Score HHVM Status Total Downloads

Decorators for PSR-7 HTTP Messages.

Install

Via Composer

$ composer require php-http/message-decorator

Usage

This package provides an easy way to decorate PSR-7 messages. While the decorator classes themselves are not abstract, they only make sense when they are extended to add custom logic:

use Http\Message\RequestDecorator;

class MyRequestDecorator extends RequestDecorator
{
    public function isThisAPostRequest()
    {
        return $this->getMethod() === 'POST';
    }
}

$request = new MyRequestDecorator($decoratedRequest);

If you override the constructor, make sure that you accept a message argument (either request or response):

use Http\Message\ResponseDecorator;
use Psr\Http\Message\ResponseInterface;

class MyResponseDecorator extends ResponseDecorator
{
    public function __construct(ResponseInterface $message)
    {
        parent::__construct($message);

        // some custom logic
    }
}

The decorated messages are stored under a private $message property. To ease acces to this property, there is a public getMessage method available in both decorators.

use Http\Message\ResponseDecorator;

class MyResponseDecorator extends ResponseDecorator
{
    public function stringifyResponse()
    {
        return (string) $this->getMessage()->getBody();
    }
}

Since the underlying message is immutable as well, there is no risk that you can alter it, so exposing it is safe. However the decorators are completely transparent, so there are rare cases when you want to access the original message.

Note: Hence the immutability of both the decorators and the underlying messages, every writting operation causes two object cloning which definitely mean a bigger performance hit.

Testing

$ phpspec run

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please contact us at security@php-http.org.

License

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

About

Decorators for PSR-7 HTTP Messages

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages