royalcms/wrappers-php-json

The Most Useful Wrappers For PHP Functions

v2.0.0 2021-05-06 02:33 UTC

This package is auto-updated.

Last update: 2024-03-06 08:31:19 UTC


README

logo

The Most Useful Wrappers For PHP Functions

Version Version Build Status Coverage Downloads count License

This package contains most useful wrappers for native PHP functions.

Install

Require this package with composer using the following command:

$ composer require royalcms/wrappers-php-json "^2.0"

Installed composer is required (how to install composer).

You need to fix the major version of package.

Usage

\Tarampampam\Wrappers\Json

This wrapper was wrote for throwing exceptions (JsonEncodeDecodeException) on any tries to work with wrong json string or converting wrong object into json string.

Also method ::decode(...) by default returns associated array instead object ($assoc = true).

Examples:

<?php

use Tarampampam\Wrappers\Json;

Json::encode(['foo' => 'bar']); // {"foo":"bar"}
Json::encode(tmpfile());        // Throws an exception - value cannot be resource

Json::decode('{"foo":"bar"}');        // ['foo' => 'bar']
Json::decode('{"foo":"bar"}', false); // $object->foo === bar
Json::decode('{"foo":"ba');           // Throws an exception - wrong json string

Json::encode([], JSON_EMPTY_ARRAYS_TO_OBJECTS); // {}
Json::encode(['foo' => []], JSON_EMPTY_ARRAYS_TO_OBJECTS); // {"foo":{}}

Json::encode(['foo' => 'bar'], JSON_PRETTY_PRINT_2_SPACES);
// {
//   "foo": "bar"
// }

Testing

For package testing we use phpunit framework and docker-ce + docker-compose as develop environment. So, just write into your terminal after repository cloning:

$ make build
$ make latest # or 'make lowest'
$ make test

Changes log

Release date Commits since latest release

Changes log can be found here.

Support

Issues Issues

If you will find any package errors, please, make an issue in current repository.

License

This is open-sourced software licensed under the MIT License.