Translations: Español
PHP simple library for managing JSON files.
- Requirements
- Installation
- Available Classes
- Exceptions Used
- Usage
- Tests
- TODO
- Changelog
- Contribution
- Sponsor
- License
This library is compatible with the PHP versions: 8.0 | 8.1.
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
Josantonius\Json\Json
Check whether a local or remote file exists.
public function exists(): bool;
Get JSON file contents:
/**
* @throws GetFileException if there is an error when getting a file.
* @throws JsonErrorException if there is an error when parsing a JSON file.
*/
public function get(): array;
Get the path or URL of the JSON file.
public function filepath(): string;
Set the content of the JSON file:
/**
* @throws CreateFileException if there is an error when creating a file.
* @throws CreateDirectoryException if the directory cannot be created.
* @throws UnavailableMethodException if the method is not available.
*/
public function set(array|object $content = []): void;
Merge into JSON 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.
*/
public function merge(array|object $content): array;
Push on the JSON 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.
*/
public function push(array|object $content): array;
use Josantonius\Json\Exceptions\GetFileException;
use Josantonius\Json\Exceptions\CreateFileException;
use Josantonius\Json\Exceptions\JsonErrorException;
use Josantonius\Json\Exceptions\CreateDirectoryException;
use Josantonius\Json\Exceptions\UnavailableMethodException;
Example of use for this library:
index.php
use Josantonius\Json\Json;
$json = new Json('file.json');
$json->exists(); // bool
index.php
use Josantonius\Json\Json;
$json = new Json('https://example.com/file.json');
$json->exists(); // bool
file.json
{
"foo": "bar"
}
index.php
use Josantonius\Json\Json;
$json = new Json('file.json');
$json->get(); // ['foo' => 'bar']
https://example.com/file.json
{
"foo": "bar"
}
index.php
use Josantonius\Json\Json;
$json = new Json('https://example.com/file.json');
$json->get(); // ['foo' => 'bar']
index.php
use Josantonius\Json\Json;
$json = new Json('file.json');
$json->filepath(); // 'file.json'
index.php
use Josantonius\Json\Json;
$json = new Json('https://example.com/file.json');
$json->filepath(); // 'https://example.com/file.json'
index.php
use Josantonius\Json\Json;
$json = new Json('file.json');
$json->set();
file.json
[]
index.php
use Josantonius\Json\Json;
$json = new Json('file.json');
$json->set(['foo' => 'bar']);
file.json
{
"foo": "bar"
}
file.json
{
"foo": "bar"
}
index.php
use Josantonius\Json\Json;
$json = new Json('file.json');
$json->merge(['bar' => 'foo']);
file.json
{
"foo": "bar",
"bar": "foo"
}
file.json
[
{
"name": "foo"
}
]
index.php
use Josantonius\Json\Json;
$json = new Json('file.json');
$json->push(['name' => 'bar']);
file.json
[
{
"name": "foo"
},
{
"name": "bar"
}
]
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
- 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)
Detailed changes for each release are documented in the release notes.
Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.
Thanks to all contributors! ❤️
If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊
This repository is licensed under the MIT License.
Copyright © 2016-present, Josantonius