DataMapper::instantiateCollection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
namespace Balloon\Mapper;
3
4
use ICanBoogie\Inflector;
5
use JMS\Serializer\Serializer;
6
7
/**
8
 * Class DataMapper
9
 * @package Balloon\Mapper
10
 * @author Raphaël Lefebvre <[email protected]>
11
 */
12
class DataMapper 
13
{
14
    /**
15
     * @var string
16
     */
17
    private $className;
18
19
    /**
20
     * @var Inflector
21
     */
22
    private $inflector;
23
24
    /**
25
     * @var Serializer
26
     */
27
    private $serializer;
28
29
    /**
30
     * @param Serializer $serializer
31
     * @param Inflector $inflector
32
     * @param string $className
33
     */
34
    public function __construct(Serializer $serializer, Inflector $inflector, $className = '')
35
    {
36
        $this->serializer = $serializer;
37
        $this->inflector = $inflector;
38
        $this->setClassName($className);
39
    }
40
41
    /**
42
     * @param array $dataList
43
     * @return \ArrayObject
44
     */
45
    public function mapDataList(array $dataList)
46
    {
47
        if(!$this->getClassName()){
48
            return $this->instantiateCollection($dataList);
49
        }
50
51
        $result = [];
52
        foreach($dataList as $key => $data){
53
            $result[$key] = $this->mapData($data);
54
        }
55
        return $this->instantiateCollection($result);
56
    }
57
58
    /**
59
     * @param mixed[] $objects
60
     * @return array
61
     */
62
    public function unmapObjects(array $objects)
63
    {
64
        $result = [];
65
        foreach($objects as $key => $object){
66
            $result[$key] = $this->unmapObject($object);
67
        }
68
        return $result;
69
    }
70
71
    /**
72
     * @param array $data
73
     * @return mixed
74
     */
75
    public function mapData(array $data)
76
    {
77
        if(!$this->getClassName()){
78
            return $data;
79
        }
80
        return $this->serializer->fromArray($data, $this->getClassName());
81
    }
82
83
84
    /**
85
     * @param mixed $object
86
     * @return array
87
     */
88
    public function unmapObject($object)
89
    {
90
        if(is_array($object)){
91
            return $object;
92
        }
93
        return $this->serializer->toArray($object);
94
    }
95
96
    /**
97
     * Getter of $className
98
     *
99
     * @return string
100
     */
101
    public function getClassName()
102
    {
103
        return $this->className;
104
    }
105
106
    /**
107
     * Setter of $className
108
     *
109
     * @param string $className
110
     */
111
    private function setClassName($className)
112
    {
113
        $this->className = (string)$className;
114
    }
115
116
    /**
117
     * @param array $data
118
     * @return \ArrayObject
119
     */
120
    private function instantiateCollection(array $data)
121
    {
122
        $collectionClassName = $this->inflector->pluralize($this->className);
123
        if(class_exists($collectionClassName)){
124
            return new $collectionClassName($data);
125
        }
126
        return $data;
127
    }
128
}
129