PHP simple library for managing Json files.

1.1.7 2018-01-06 19:03 UTC

This package is auto-updated.

Last update: 2021-04-22 14:51:08 UTC


README

Latest Stable Version Latest Unstable Version License Codacy Badge Total Downloads Travis PSR2 PSR4 CodeCov

Versión en español

PHP simple library for managing Json files.

Requirements

This library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher.

Installation

The preferred way to install this extension is through Composer.

To install PHP Json library, simply:

$ composer require Josantonius/Json

The previous command will only install the necessary files, if you prefer to download the entire source code you can use:

$ composer require Josantonius/Json --prefer-source

You can also clone the complete repository with Git:

$ git clone https://github.com/Josantonius/PHP-Json.git

Or install it manually:

Download Json.php, JsonLastError.php and JsonException.php:

$ wget https://raw.githubusercontent.com/Josantonius/PHP-Json/master/src/Json.php
$ wget https://raw.githubusercontent.com/Josantonius/PHP-Json/master/src/JsonLastError.php
$ wget https://raw.githubusercontent.com/Josantonius/PHP-Json/master/src/Exception/JsonException.php

Available Methods

Available methods in this library:

- Creating JSON file from array:

Json::arrayToFile($array, $file);
Attribute Description Type Required Default
$array Array to be converted to JSON. array Yes
$file Path to the file. string Yes

# Return (boolean)

- Save to array the JSON file content:

Json::fileToArray($file);
Attribute Description Type Required Default
$file Path or external url to JSON file. string Yes

# Return (array|false)

- Check for errors:

JsonLastError::check();

# Return (array|null) → Null if there are no errors or array with state code and error message.

- Get collection of JSON errors:

JsonLastError::getCollection();

# Return (array) → Collection of JSON errors.

Quick Start

To use this library with Composer:

require __DIR__ . '/vendor/autoload.php';

use Josantonius\Json\Json;

Or If you installed it manually, use it:

require_once __DIR__ . '/Json.php';
require_once __DIR__ . '/JsonLastError.php';
require_once __DIR__ . '/JsonException.php';

use Josantonius\Json\Json;

Usage

Example of use for this library:

- Creating JSON file from array:

$array = [
	'name'  => 'Josantonius',
    'email' => 'info@josantonius.com',
    'url'   => 'https://github.com/josantonius/PHP-Json'
];

$pathfile = __DIR__ . '/filename.json';

Json::arrayToFile($array, $pathfile);

- Save to array the JSON file content:

$pathfile = __DIR__ . '/filename.json';

$array = Json::fileToArray($pathfile);

- Check for errors:

$lastError = JsonLastError::check();

if (!is_null($lastError)) {
    var_dump($lastError);
}

- Get collection of JSON errors:

$jsonLastErrorCollection = JsonLastError::getCollection();

Tests

To run tests you just need composer and to execute the following:

$ git clone https://github.com/Josantonius/PHP-Json.git

$ cd PHP-Json

$ composer install

Run unit tests with PHPUnit:

$ composer phpunit

Run PSR2 code standard tests with PHPCS:

$ composer phpcs

Run PHP Mess Detector tests to detect inconsistencies in code style:

$ composer phpmd

Run all previous tests:

$ composer tests

Exception Handler

This library uses exception handler that you can customize.

☑ TODO

  • Add new feature.
  • Improve tests.
  • Improve documentation.
  • Refactor code for disabled code style rules. See phpmd.xml and .php_cs.dist.

Contribute

If you would like to help, please take a look at the list of issues or the To Do checklist.

Pull requests

  • Fork and clone.
  • Run the command composer install to install the dependencies. This will also install the dev dependencies.
  • Run the command composer fix to excute code standard fixers.
  • Run the tests.
  • Create a branch, commit, push and send me a pull request.

Thank you to all the people who already contributed to this project!

peter279k
peter279k

Repository

The file structure from this repository was created with PHP-Skeleton.

License

This project is licensed under MIT license. See the LICENSE file for more info.

Copyright

2016 - 2018 Josantonius, josantonius.com

If you find it useful, let me know 😉

You can contact me on Twitter or through my email.