StrictJsonParser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 13 3
A encode() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MiladRahimi\Jwt\Json;
6
7
use MiladRahimi\Jwt\Exceptions\JsonDecodingException;
8
use MiladRahimi\Jwt\Exceptions\JsonEncodingException;
9
10
class StrictJsonParser implements JsonParser
11
{
12
    /**
13
     * @inheritdoc
14
     */
15
    public function encode(array $data): string
16
    {
17
        $json = json_encode($data);
18
19
        if (json_last_error() !== JSON_ERROR_NONE) {
20
            throw new JsonEncodingException(json_last_error_msg(), json_last_error());
21
        }
22
23
        return $json;
24
    }
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function decode(string $json): array
30
    {
31
        $result = json_decode($json, true);
32
33
        if (json_last_error() !== JSON_ERROR_NONE) {
34
            throw new JsonDecodingException(json_last_error_msg(), json_last_error());
35
        }
36
37
        if (!is_array($result)) {
38
            throw new JsonDecodingException('Claims are not in array format.');
39
        }
40
41
        return $result;
42
    }
43
}
44