AspireCloud/app/Utils/JSON.php
Chuck Adams 35b967b124
Repo Admin API (#123)
* fix: switch redis driver to predis because laravel is hopeless

* chore: add spatie/laravel-permission

* build: composer update and bump

* style: death to captain obvious (zap laravel's banner comments)

* feat: initial roles and perms

* chore: assign User role on User created

* refactor: rename 'aspire_press' config key to 'aspirecloud'

* feat: require json content-type on admin api routes

* chore: implement BulkImport endpoint

* chore: give RepoAdmin users BulkImport permissions

* docs: update README
2024-12-28 15:57:51 -07:00

53 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Utils;
use Safe\Exceptions\JsonException;
use stdClass;
class JSON
{
public const DEFAULT_JSON_OPTIONS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
/**
* @param array<string, mixed> $value
* @throws JsonException
*/
public static function fromAssoc(array $value, int $flags = self::DEFAULT_JSON_OPTIONS, int $depth = 512): string
{
return static::encode($value, $flags, $depth);
}
/**
* @return array<string, mixed>
* @throws JsonException
*/
public static function toAssoc(string $json, int $depth = 512, int $flags = self::DEFAULT_JSON_OPTIONS): array
{
return static::decode($json, true, $depth, $flags);
}
/** @throws JsonException */
public static function toObject(string $json, int $depth = 512, int $flags = self::DEFAULT_JSON_OPTIONS): stdClass
{
return static::decode($json, false, $depth, $flags);
}
/** @throws JsonException */
public static function encode(mixed $value, int $flags = self::DEFAULT_JSON_OPTIONS, int $depth = 512): string
{
return \Safe\json_encode($value, $flags, $depth);
}
/** @throws JsonException */
public static function decode(
string $json,
bool $assoc = false,
int $depth = 512,
int $flags = self::DEFAULT_JSON_OPTIONS,
): mixed {
return \Safe\json_decode($json, $assoc, $depth, $flags);
}
}