germania-kg/jsondecoder

There is no license information available for the latest version (1.0.2) of this package.

JSON decoding solution with JsonException support for older PHP versions

1.0.2 2019-08-22 06:50 UTC

This package is auto-updated.

Last update: 2020-07-22 08:53:08 UTC


README

68747470733a2f2f7374617469632e6765726d616e69612d6b672e636f6d2f6c6f676f732f67612d6c6f676f2d323031362d7765622e7376677a

JsonDecoder

JSON decoding solution with JsonException support for older PHP versions

Packagist PHP version Build Status Scrutinizer Code Quality Code Coverage Build Status

Installation with Composer

$ composer require germania-kg/jsondecoder

Usage

The callable JsonDecoder accepts the same parameters than described in PHP's documentation on json_decode.

Additionally, it accepts some other widely-used variable types as well:

  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\StreamInterface
  • string
use Germania\JsonDecoder\JsonDecoder;

// Let:
// $response instance of ResponseInterface,
// $body instance of StreamInterface
$body = $response->getBody();
$str = $body->__toString();

try {
  $decoder = new JsonDecoder;
  $decoded = $decoder( $response );  
  $decoded = $decoder( $body );  
  $decoded = $decoder( $str );    
}
catch ( \JsonException $e)
{
  echo $e->getMessage();
}

Exceptions

When the decoding fails, a \JsonException (mind the global namespace!) will be thrown. This class is provided by Symfony's Polyfill PHP 7.3 for those lacking PHP 7.3.

Issues

See full issues list.

Development

$ git clone https://github.com/GermaniaKG/JsonDecoder.git
$ cd JsonDecoder
$ composer install

Unit tests

Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:

$ composer test
# or
$ vendor/bin/phpunit