PHP simple library for managing Json files.

Fund package maintenance!
Josantonius

v2.0.1 2022-06-27 23:53 UTC

README

Latest Stable Version License Total Downloads CI CodeCov PSR1 PSR4 PSR12

Translations: Español

PHP simple library for managing JSON files.

Requirements

This library is compatible with the PHP versions: 8.0 | 8.1.

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

Available Methods

Available methods in this library:

Get JSON file contents

$json->get();

@throws CreateDirectoryException If there is an error when creating a directory

@throws CreateFileException If there is an error when creating a file

@throws JsonErrorException If there is an error when parsing a JSON file

@Return array File contents

Set the content of the JSON file

$json->set(array|object $content);

@throws CreateFileException If there is an error when creating a file

@throws JsonErrorException If there is an error when parsing a JSON file

@throws UnavailableMethodException If the method is not available

@Return void

Merge into JSON file

$json->merge(array|object $content);

@throws CreateFileException If there is an error when creating a file

@throws GetFileException If there is an error when getting a file

@throws JsonErrorException If there is an error when parsing a JSON file

@throws UnavailableMethodException If the method is not available

@Return array Resulting array

Push on the JSON file

$json->push(array|object $content);

@throws CreateFileException If there is an error when creating a file

@throws GetFileException If there is an error when getting a file

@throws JsonErrorException If there is an error when parsing a JSON file

@throws UnavailableMethodException If the method is not available

@Return array Resulting array

Quick Start

To use this library with Composer:

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

use josantonius\Json\Json;
$json = new Json('path/to/file.json');

# If the file does not exist it will be created.

OR

$json = new Json('https://site.com/file.json');

# When the JSON file is obtained from a URL, only the "get" method is available.

Usage

Example of use for this library:

Get the JSON file contents

{
    "foo": "bar"
}
$json->get();
['foo' => 'bar']

Set the JSON file contents

$json->set(['foo' => 'bar']);
{
    "foo": "bar"
}

Merge data into JSON file

{
    "foo": "bar"
}
$json->merge(['bar' => 'foo']);
{
    "foo": "bar",
    "bar": "foo"
}

Push data on the JSON file

[
    {
        "name": "foo"
    }
]
$json->push(['name'  => 'bar']);
[
    {
        "name": "foo"
    },
    {
        "name": "bar"
    }
]

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 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

☑ TODO

  • Add new feature.
  • Improve tests.
  • Improve documentation.
  • Improve English translation in the README file.
  • Refactor code for disabled code style rules. See phpmd.xml and phpcs.xml.

Changelog

Detailed changes for each release are documented in the release notes.

Contribution

Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.

Thanks to all contributors! ❤️

Sponsor

If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊

License

This repository is licensed under the MIT License.

Copyright © 2016-present, Josantonius