JsonHelper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 9
c 1
b 0
f 1
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 8 2
A encode() 0 9 2
1
<?php
2
3
namespace Smoren\EncryptionTools\Helpers;
4
5
use Smoren\EncryptionTools\Exceptions\JsonException;
6
7
/**
8
 * Class JsonHelper
9
 * @author Smoren <[email protected]>
10
 */
11
class JsonHelper
12
{
13
    /**
14
     * Converts value to JSON format
15
     * @param mixed $value
16
     * @return string
17
     * @throws JsonException
18
     */
19
    public static function encode($value): string
20
    {
21
        $result = json_encode($value);
22
        if($error = json_last_error()) {
23
            throw new JsonException(json_last_error_msg(), $error);
24
        }
25
26
        /** @var string $result */
27
        return $result;
28
    }
29
30
    /**
31
     * Parses JSON to PHP value
32
     * @param string $json
33
     * @return mixed
34
     * @throws JsonException
35
     */
36
    public static function decode(string $json)
37
    {
38
        $result = json_decode($json, true);
39
        if($error = json_last_error()) {
40
            throw new JsonException(json_last_error_msg(), $error);
41
        }
42
43
        return $result;
44
    }
45
}
46