JsonSerializer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 36
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentType() 0 3 1
A unserialize() 0 9 2
A __construct() 0 4 1
A serialize() 0 9 2
1
<?php
2
3
namespace Ipag\Sdk\IO;
4
5
use Ipag\Sdk\Exception\ParseException;
6
7
class JsonSerializer implements SerializerInterface
8
{
9
    protected int $flags;
10
    protected int $depth;
11
12
    public function __construct(int $flags = 0, int $depth = 512)
13
    {
14
        $this->flags = $flags;
15
        $this->depth = $depth;
16
    }
17
18
    public function serialize(array $data): string
19
    {
20
        $data = json_encode($data, $this->flags, $this->depth);
21
22
        if (json_last_error() != JSON_ERROR_NONE) {
23
            throw new ParseException("Serialization data is not JSON parseable.");
24
        }
25
26
        return $data;
27
    }
28
29
    public function unserialize(string $data): array
30
    {
31
        $data = json_decode($data, true, $this->depth, $this->flags);
32
33
        if (json_last_error() != JSON_ERROR_NONE) {
34
            throw new ParseException("Received data is not JSON parseable: $data.");
35
        }
36
37
        return $data;
38
    }
39
40
    public function getContentType(): string
41
    {
42
        return 'application/json';
43
    }
44
}
45