ZanzaraMapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 41
rs 10
c 2
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mapObject() 0 5 2
A __construct() 0 3 1
A mapJson() 0 6 2
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