Repository navigation
feat: Introduce JSON utility class with encoding, decoding, and validation methods #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
63c9cc9
feat: Introduce JSON utility class with encoding, decoding, and valid…
marcreichel 48662b5
feat: Introduce JSON utility class with encoding, decoding, and valid…
marcreichel 0cc9d26
Merge remote-tracking branch 'origin/feat/json-helper' into feat/json…
marcreichel 39b57b6
chore: Update PHPStan baseline messages and increment error counts
marcreichel f91c52f
feat: Introduce JSON utility class with encoding, decoding, and valid…
marcreichel 4987fa7
feat: Introduce JSON utility class with encoding, decoding, and valid…
marcreichel 3911f6f
chore: Update PHPStan baseline messages and increment error counts
marcreichel d93c00c
Merge remote-tracking branch 'origin/feat/json-helper' into feat/json…
marcreichel a481bf9
Merge remote-tracking branch 'origin/main' into feat/json-helper
marcreichel 041a7fe
feat: enhance JSON handling with new decoding helpers and expanded tests
marcreichel d9d3aa1
refactor: reorder `decodeSilently` method position in JSON.php file
marcreichel ebe7bfc
style: fix docblock order in `decodeAsArray` method
marcreichel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,52 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Artemeon\Support; | ||
|
|
||
| use JetBrains\PhpStorm\Language; | ||
| use JsonException; | ||
|
|
||
| class JSON | ||
| { | ||
| /** | ||
| * @param positive-int $depth | ||
| * | ||
| * @throws JsonException | ||
| */ | ||
| public static function encode(mixed $value, int $flags = 0, int $depth = 512): string | ||
| { | ||
| return json_encode($value, JSON_THROW_ON_ERROR | $flags, $depth); | ||
| } | ||
|
|
||
| /** | ||
| * @param int<1, 2147483647> $depth | ||
| * | ||
| * @throws JsonException | ||
| */ | ||
| public static function decode(#[Language('JSON')] string $json, ?bool $associative = null, int $depth = 512, int $flags = 0): mixed | ||
| { | ||
| return json_decode($json, $associative, $depth, JSON_THROW_ON_ERROR | $flags); | ||
| } | ||
|
|
||
| /** | ||
| * @param int<1, 2147483647> $depth | ||
| */ | ||
| public static function decodeSilently(#[Language('JSON')] string $json, ?bool $associative = null, int $depth = 512, int $flags = 0): mixed | ||
| { | ||
| try { | ||
| return self::decode($json, $associative, $depth, $flags); | ||
| } catch (JsonException) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param int<1, 2147483647> $depth | ||
| * @param int-mask-of<0|1048576> $flags | ||
| */ | ||
| public static function validate(#[Language('JSON')] string $json, int $depth = 512, int $flags = 0): bool | ||
| { | ||
| return json_validate($json, $depth, $flags); | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Artemeon\Support\JSON; | ||
|
|
||
| it('should encode data', function (): void { | ||
| expect(JSON::encode(['foo' => 'bar'])) | ||
| ->toBe('{"foo":"bar"}'); | ||
| }); | ||
|
|
||
| it('should decode data', function (): void { | ||
| expect(JSON::decode('{"foo": "bar"}', true)) | ||
| ->toBe(['foo' => 'bar']); | ||
| }); | ||
|
|
||
| it('should throw with invalid data', function (): void { | ||
| JSON::decode('lol'); | ||
| })->throws(JsonException::class); | ||
|
|
||
| it('should return null and not throw with invalid data', function (): void { | ||
| expect(JSON::decodeSilently('lol')) | ||
| ->toBeNull(); | ||
| }); | ||
|
|
||
| it('should validate data', function (): void { | ||
| expect(JSON::validate('{"foo": "bar"}')) | ||
| ->toBeTrue() | ||
| ->and(JSON::validate('lol')) | ||
| ->toBeFalse(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.