Skip to content

josantonius/php-json

Repository files navigation

PHP JSON library

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 Classes

Json Class

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;

Exceptions Used

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;

Usage

Example of use for this library:

Check whether a local file exists

index.php

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->exists(); // bool

Check whether a remote file exists

index.php

use Josantonius\Json\Json;

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

$json->exists(); // bool

Get the JSON file contents

file.json

{
    "foo": "bar"
}

index.php

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->get(); // ['foo' => 'bar']

Get the JSON file contents from URL

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

Get the path of the JSON file

index.php

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->filepath(); // 'file.json'

Get the URL of the remote JSON file

index.php

use Josantonius\Json\Json;

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

$json->filepath(); // 'https://example.com/file.json'

Set an empty array in the JSON file contents

index.php

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->set();

file.json

[]

Set the JSON file contents

index.php

use Josantonius\Json\Json;

$json = new Json('file.json');

$json->set(['foo' => 'bar']);

file.json

{
    "foo": "bar"
}

Merge data into JSON file

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

Push data on the JSON file

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"
    }
]

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

About

PHP simple library for managing JSON files

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages