Skip to content

Instantly share code, notes, and snippets.

@whchi
Created July 5, 2020 04:41
Show Gist options
  • Select an option

  • Save whchi/9d32c2e173b4d8efa64ddbb391d5be33 to your computer and use it in GitHub Desktop.

Select an option

Save whchi/9d32c2e173b4d8efa64ddbb391d5be33 to your computer and use it in GitHub Desktop.
<?php
if (!function_exists('jsonEncode')) {
function jsonEncode($payload, int $options = 0, int $depth = 512): string
{
$defaultOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE;
$options = 0 !== $options ? $options : $defaultOptions;
$json = \json_encode($payload, $options, $depth);
if (JSON_ERROR_NONE !== \json_last_error()) {
throw new \InvalidArgumentException(
'json_encode error: ' . \json_last_error_msg()
);
}
return $json;
}
}
if (!function_exists('jsonDecode')) {
function jsonDecode(string $payload, bool $assoc = false, int $depth = 512, int $options = 0)
{
$data = \json_decode($payload, $assoc, $depth, $options);
if (JSON_ERROR_NONE !== \json_last_error()) {
throw new \InvalidArgumentException(
'json_decode error: ' . \json_last_error_msg()
);
}
return $data;
}
}
if (!function_exists('autoSeq')) {
function autoSeq()
{
for ($i = 0; $i < 9999; $i++) {
yield $i;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment