-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 24d357e
Showing
18 changed files
with
517 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# EditorConfig helps developers define and maintain consistent | ||
# coding styles between different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_size = 4 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[**.{neon,neon.dist}] | ||
indent_style = tab | ||
|
||
[**.{yml,yaml}] | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/tests/ export-ignore | ||
/.editorconfig export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/phpstan.neon.dist export-ignore | ||
/psalm.xml.dist export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
name: Tests | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- 'main' | ||
|
||
jobs: | ||
tests: | ||
name: PHP ${{ matrix.php }} | ||
runs-on: ubuntu-20.04 | ||
|
||
strategy: | ||
matrix: | ||
php: | ||
- "7.4" | ||
- "8.0" | ||
- "8.1" | ||
dependencies: | ||
- "lowest" | ||
- "highest" | ||
|
||
env: | ||
extensions: ctype, dom, intl, json, mbstring, openssl, xml, zip, zlib | ||
key: "cache-${{ matrix.php }}-${{ matrix.dependencies }}-v1" # can be any string, change to clear the extension cache. | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup cache environment | ||
id: extcache | ||
uses: shivammathur/cache-extensions@v1 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
extensions: ${{ env.extensions }} | ||
key: ${{ env.key }} | ||
|
||
- name: Cache extensions | ||
uses: actions/cache@v2 | ||
with: | ||
path: ${{ steps.extcache.outputs.dir }} | ||
key: ${{ steps.extcache.outputs.key }} | ||
restore-keys: ${{ steps.extcache.outputs.key }} | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
extensions: ${{ env.extensions }} | ||
tools: composer, pecl | ||
coverage: xdebug | ||
env: | ||
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- uses: "ramsey/composer-install@v2" | ||
with: | ||
dependency-versions: "${{ matrix.dependencies }}" | ||
composer-options: "${{ matrix.composer-options }}" | ||
|
||
- name: Install tools | ||
run: composer install-tools | ||
|
||
- name: Setup problem matchers for PHP | ||
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json" | ||
|
||
- name: Run PHPStan | ||
run: composer phpstan | ||
|
||
- name: Run Psalm | ||
run: composer psalm -- --output-format=github | ||
|
||
- name: Setup Problem Matchers for PHPUnit | ||
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" | ||
|
||
- name: Run PHPUnit | ||
run: vendor/bin/phpunit --testdox --coverage-text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/.build/ | ||
/vendor/ | ||
|
||
/composer.lock | ||
/phpstan.neon | ||
/phpunit.xml | ||
/psalm.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Jérôme Gamez, https://github.com/beste <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# JSON | ||
|
||
[![Current version](https://img.shields.io/packagist/v/beste/json.svg?logo=composer)](https://packagist.org/packages/beste/json) | ||
[![Supported PHP version](https://img.shields.io/static/v1?logo=php&label=PHP&message=~7.4.0%20||%20~8.0.0%20||%20~8.1.0&color=777bb4)](https://packagist.org/packages/beste/json) | ||
[![Tests](https://github.com/beste/json-php/workflows/Tests/badge.svg)](https://github.com/beste/json-php/actions) | ||
[![Discord](https://img.shields.io/discord/807679292573220925.svg?color=7289da&logo=discord)](https://discord.gg/Yacm7unBsr) | ||
[![Sponsor](https://img.shields.io/static/v1?logo=GitHub&label=Sponsor&message=%E2%9D%A4&color=ff69b4)](https://github.com/sponsors/jeromegamez) | ||
|
||
A simple helper to decode and encode JSON, including from files. | ||
|
||
## Usage | ||
|
||
```php | ||
use Beste\Json; | ||
use InvalidArgumentException; | ||
|
||
$object = Json::decode('{"key": "value"}'); | ||
|
||
$array = Json::decode('{"key": "value"}', $forceArray = true); | ||
|
||
$object = Json::decodeFile('/path/to/file.json'); | ||
|
||
$json = Json::encode($object); | ||
|
||
$prettyJson = Json::pretty($object); | ||
|
||
// When something goes wring while decoding/encoding, | ||
// an `InvalidArgumentException` is thrown | ||
try { | ||
Json::decode('{]'); | ||
} catch (InvalidArgumentException $e) { | ||
// Handle error | ||
} | ||
``` | ||
|
||
## Installation | ||
|
||
```shell | ||
composer require beste/json | ||
``` | ||
|
||
## Running tests | ||
|
||
```shell | ||
composer run tests | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"name": "beste/json", | ||
"type": "library", | ||
"description": "A simple JSON helper to decode and encode JSON", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jérôme Gamez", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "~7.4.0 || ~8.0.0 || ~8.1.0", | ||
"ext-json": "*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5.13" | ||
}, | ||
"autoload": { | ||
"files": [ | ||
"src/Json.php" | ||
] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Beste\\Json\\Tests\\": "tests" | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"scripts": { | ||
"install-tools": [ | ||
"composer update --working-dir=tools/phpstan", | ||
"composer update --working-dir=tools/psalm" | ||
], | ||
"clean": "rm -rf .build && mkdir .build", | ||
"phpstan": "tools/phpstan/vendor/bin/phpstan analyse", | ||
"phpunit": "vendor/bin/phpunit", | ||
"psalm": "tools/psalm/vendor/bin/psalm", | ||
"tests": [ | ||
"@phpstan", | ||
"@psalm", | ||
"@phpunit" | ||
] | ||
}, | ||
"scripts-descriptions": { | ||
"clean": "Recreates ", | ||
"phpstan": "Runs static analysis with PHPStan", | ||
"phpunit": "Runs tests with PHPUnit", | ||
"psalm": "Runs static analysis with Psalm", | ||
"tests": "Runs static analysis and test suites" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
parameters: | ||
level: max | ||
|
||
paths: | ||
- src/ | ||
- tests/ | ||
|
||
tmpDir: .build/phpstan/ | ||
|
||
ignoreErrors: | ||
- | ||
message: '#Dynamic call to static method PHPUnit#' | ||
path: tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
cacheResultFile=".build/phpunit/phpunit.cache" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Tests"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
</coverage> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0"?> | ||
<psalm | ||
xmlns="https://getpsalm.org/schema/config" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" | ||
cacheDirectory=".build/psalm" | ||
errorLevel="1" | ||
resolveFromConfigFile="true" | ||
> | ||
<projectFiles> | ||
<directory name="src" /> | ||
<directory name="tests" /> | ||
<ignoreFiles> | ||
<directory name="vendor" /> | ||
</ignoreFiles> | ||
</projectFiles> | ||
|
||
<plugins> | ||
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/> | ||
</plugins> | ||
</psalm> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Beste; | ||
|
||
use InvalidArgumentException; | ||
use JsonException; | ||
|
||
final class Json | ||
{ | ||
private const ENCODE_DEFAULT = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; | ||
private const ENCODE_PRETTY = self::ENCODE_DEFAULT | JSON_PRETTY_PRINT; | ||
private const DECODE_DEFAULT = JSON_BIGINT_AS_STRING; | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
* | ||
* @return mixed | ||
*/ | ||
public static function decode(string $json, ?bool $forceArray = null) | ||
{ | ||
$forceArray = $forceArray ?? false; | ||
$flags = $forceArray ? JSON_OBJECT_AS_ARRAY : 0; | ||
|
||
try { | ||
return json_decode($json, $forceArray, 512, $flags | self::DECODE_DEFAULT | JSON_THROW_ON_ERROR); | ||
} catch (JsonException $e) { | ||
throw new InvalidArgumentException($e->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
* | ||
* @return mixed | ||
*/ | ||
public static function decodeFile(string $path, bool $forceArray = null) | ||
{ | ||
if (!is_readable($path)) { | ||
throw new InvalidArgumentException("The file at '$path' does not exist"); | ||
} | ||
|
||
return self::decode((string) file_get_contents($path), $forceArray); | ||
} | ||
|
||
/** | ||
* @param mixed $data | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function encode($data, ?int $options = null): string | ||
{ | ||
$options = $options ?? 0; | ||
|
||
try { | ||
return json_encode($data, $options | self::ENCODE_DEFAULT | JSON_THROW_ON_ERROR); | ||
} catch (JsonException $e) { | ||
throw new InvalidArgumentException($e->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function pretty($value, ?int $options = null): string | ||
{ | ||
$options = $options ?? 0; | ||
|
||
return self::encode($value, $options | self::ENCODE_PRETTY); | ||
} | ||
} |
Oops, something went wrong.