ZanzaraMapper::mapJson()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zanzara;
6
7
use JsonMapper;
8
use JsonMapper_Exception;
9
10
/**
11
 * JsonMapper is used for deserialization.
12
 *
13
 * @see JsonMapper
14
 */
15
class ZanzaraMapper
16
{
17
18
    /**
19
     * @var JsonMapper
20
     */
21
    private $jsonMapper;
22
23
    /**
24
     * @param JsonMapper $jsonMapper
25
     */
26
    public function __construct(JsonMapper $jsonMapper)
27
    {
28
        $this->jsonMapper = $jsonMapper;
29
    }
30
31
    /**
32
     * @param string $json
33
     * @param $class
34
     * @return mixed
35
     * @throws JsonMapper_Exception
36
     */
37
    public function mapJson(string $json, $class)
38
    {
39
        $decoded = json_decode($json);
40
        return is_array($decoded) ?
41
            $this->jsonMapper->mapArray($decoded, [], $class) :
42
            $this->jsonMapper->map($decoded, new $class);
43
    }
44
45
    /**
46
     * @param $object
47
     * @param $class
48
     * @return mixed
49
     * @throws JsonMapper_Exception
50
     */
51
    public function mapObject($object, $class)
52
    {
53
        return is_array($object) ?
54
            $this->jsonMapper->mapArray($object, [], $class) :
55
            $this->jsonMapper->map($object, new $class);
56
    }
57
58
}
59