Files
hocusfokus-web/kirby/src/Data/Json.php
Bastian Allgeier 01277f79f2 first version
2019-01-13 23:17:34 +01:00

46 lines
940 B
PHP
Executable File

<?php
namespace Kirby\Data;
use Exception;
/**
* Simple Wrapper around json_encode and json_decode
*
* @package Kirby Data
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
*/
class Json extends Handler
{
/**
* Converts an array to an encoded JSON string
*
* @param array $data
* @return string
*/
public static function encode(array $data): string
{
return json_encode($data);
}
/**
* Parses an encoded JSON string and returns a multi-dimensional array
*
* @param string $string
* @return array
*/
public static function decode($json): array
{
$result = json_decode($json, true);
if (is_array($result) === true) {
return $result;
} else {
throw new Exception('JSON string is invalid');
}
}
}