Passed
Branch master (0b4094)
by Alexander
01:32
created

JsonSerializer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 11
cp 0
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A unserialize() 0 3 1
A serialize() 0 3 1
A __construct() 0 3 1
1
<?php
2
namespace Yiisoft\Cache\Serializer;
3
4
/**
5
 * JsonSerializer serializes data in JSON format.
6
 */
7
final class JsonSerializer implements SerializerInterface
8
{
9
    private $options;
10
11
    /**
12
     * JsonSerializer constructor.
13
     *
14
     * @param int $options integer the encoding options. For more details please refer to
15
     * <http://www.php.net/manual/en/function.json-encode.php>.
16
     * Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`.
17
     */
18
    public function __construct(int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
19
    {
20
        $this->options = $options;
21
    }
22
23
    public function serialize($value): string
24
    {
25
        return json_encode($value, $this->options);
26
    }
27
28
    public function unserialize(string $value)
29
    {
30
        return json_decode($value, true);
31
    }
32
}
33