Completed
Push — master ( d43734...ab0406 )
by Tomasz
03:38
created

JsonFormat::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2.032
1
<?php
2
namespace Thunder\Serializard\Format;
3
4
use Thunder\Serializard\NormalizerContainer\NormalizerContainerInterface as Normalizers;
5
use Thunder\Serializard\HydratorContainer\HydratorContainerInterface as Hydrators;
6
7
/**
8
 * @author Tomasz Kowalczyk <[email protected]>
9
 */
10
final class JsonFormat extends AbstractFormat
11
{
12 5
    public function serialize($var, Normalizers $normalizers)
13
    {
14 5
        $json = json_encode($this->doSerialize($var, $normalizers));
15
16 2
        if(json_last_error() !== JSON_ERROR_NONE) {
17
            throw new \RuntimeException(sprintf('JSON serialization failure: `%s`!', json_last_error_msg()));
18
        }
19
20 2
        return $json;
21
    }
22
23 2
    public function unserialize($var, $class, Hydrators $hydrators)
24
    {
25 2
        return $this->doUnserialize(json_decode($var, true), $class, $hydrators);
26
    }
27
}
28