Skip to content

josantonius/php-json

Repository files navigation

PHP JSON library

Latest Stable Version License Total Downloads CI PSR2 PSR4 CodeCov

Translations: Español

PHP simple library for managing JSON files.



Requirements

This library is compatible from PHP 8.0 version to PHP 8.1 version.

To continue using the version with static methods without the new features:

  • For older versions of PHP (from 5.6 to 7.4), version 1.1.9 of this library can be used.

  • For PHP versions 8.0 and 8.1, version 1.2.0 of this library can be used.

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

@Return array - File contents

Set the content of the JSON file

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

@throws CreateFileException | JsonErrorException | UnavailableMethodException

@Return void

Merge into JSON file

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

@throws CreateFileException | GetFileException | JsonErrorException | UnavailableMethodException

@Return array - Resulting array

Push on the JSON file

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

@throws CreateFileException | GetFileException | JsonErrorException | UnavailableMethodException

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

☑ 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 and you want to thank me, you could sponsor me! 😊

License

This repository is licensed under the MIT License.

Copyright © 2016-present, Josantonius

About

PHP simple library for managing JSON files

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •  

Languages