narrowspark/http-message-util

This repository holds utility classes and constants to facilitate common operations of PSR-7.

v0.3.0 2018-05-08 20:04 UTC

README

This repository holds utility classes and constants to facilitate common operations of PSR-7.

68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6172726f77737061726b2f687474702d6d6573736167652d7574696c2e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545372e312e302d3838393242462e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6e6172726f77737061726b2f687474702d6d6573736167652d7574696c2f6d61737465722e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6e6172726f77737061726b2f687474702d6d6573736167652d7574696c2f6d61737465722e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e6172726f77737061726b2f687474702d6d6573736167652d7574696c2e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265

Each header class is named after the specific http header and has the following functions for parsing and signing.

Installation

$ composer require narrowspark/http-message-util

Use

Here's an example using the InteractsWithAcceptLanguage class:

<?php
declare(strict_types=1);

use Narrowspark\Http\Message\Util\InteractsWithAcceptLanguage;

$request = new Request();
$request = $request->withHeader('Accept-Language', 'zh, en-us; q=0.8, en; q=0.6');

return InteractsWithAcceptLanguage::getLanguages($request); // => ['zh', 'en', 'en_US']

Here's an example using the InteractsWithAuthorization class:

<?php
declare(strict_types=1);

use Narrowspark\Http\Message\Util\InteractsWithAuthorization;

$request = new Request();
$request = $request->withHeader('Authorization', 'Basic QWxhZGRpbjpPcGVuU2VzYW1l');

return InteractsWithAuthorization::getAuthorization($request); // => ['Basic', 'QWxhZGRpbjpPcGVuU2VzYW1l']

Here's an example using the InteractsWithContentTypes class:

<?php
declare(strict_types=1);

use Narrowspark\Http\Message\Util\InteractsWithContentTypes;

$request = new Request();
$request = $request->withHeader('Content-Type', 'application/json, */*');

return InteractsWithContentTypes::isJson($request); // => true

$request = $request->withHeader('X-Pjax', 'true');

return InteractsWithContentTypes::isPjax($request); // => true

$request = $request->withHeader('X-Requested-With', 'XMLHttpRequest');

return InteractsWithContentTypes::isAjax($request); // => true

Here's an example using the HeaderUtils class:

<?php
declare(strict_types=1);

use Narrowspark\Http\Message\Util\HeaderUtils;

return HeaderUtils::split("da, en-gb;q=0.8", ",;"); // => array(array('da'), array('en-gb', 'q=0.8'))

or

return HeaderUtils::combine(array(array("foo", "abc"), array("bar"))); // => array("foo" => "abc", "bar" => true)

or

return HeaderUtils::toString(array("foo" => "abc", "bar" => true, "baz" => "a b c"), ",");  // => 'foo=abc, bar, baz="a b c"'

or

return HeaderUtils::quote('foo bar'); // => "foo bar"

or

return HeaderUtils::unquote('"foo bar"'); // => foo bar

Here's an example using the InteractsWithDisposition class:

<?php
declare(strict_types=1);

use Narrowspark\Http\Message\Util\InteractsWithDisposition;

$response = InteractsWithDisposition::appendDispositionHeader(new Response(), InteractsWithDisposition::DISPOSITION_ATTACHMENT, 'foo.html');

return $response->getHeaderLine('Content-Disposition'); // => attachment; filename=foo.html

or

return InteractsWithDisposition::makeDisposition(InteractsWithDisposition::DISPOSITION_ATTACHMENT, 'foo.html'); // => attachment; filename=foo.html

Testing

$ vendor/bin/phpunit

Contributing

If you would like to help take a look at the list of issues and check our Contributing guild.

Note: Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

The Narrowspark http-emitter is open-sourced software licensed under the MIT license