EntitySerializerDefault::collectionToArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: christian
5
 * Date: 1/4/17
6
 * Time: 2:30 AM
7
 */
8
9
namespace Veloci\Core\Helper\Serializer;
10
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Symfony\Component\Serializer\Serializer;
14
use Traversable;
15
use Veloci\Core\Entity;
16
17
/**
18
 * Class EntitySerializerDefault
19
 *
20
 * @package Veloci\Core\Helper
21
 */
22
class EntitySerializerDefault  implements EntitySerializer
0 ignored issues
show
Coding Style introduced by
Expected 1 space after class name; 2 found
Loading history...
Coding Style introduced by
Expected 1 space before implements keyword; 2 found
Loading history...
23
{
24
25
    /**
26
     * @var Serializer
27
     */
28
    private $serializer;
29
30
    /**
31
     * EntitySerializerDefault constructor.
32
     *
33
     * @param Serializer $serializer
34
     */
35 4
    public function __construct(Serializer $serializer) {
36
37 4
        $this->serializer = $serializer;
38 4
    }
39
40
    /**
41
     * @param array  $data
42
     * @param string $class
43
     *
44
     * @return Entity
45
     */
46 2
    public function arrayToEntity(array $data, string $class):Entity
47
    {
48 2
        return $this->serializer->denormalize($data, $class);
49
    }
50
51
    /**
52
     * @param Entity $entity
53
     *
54
     * @return array
55
     */
56 2
    public function entityToArray(Entity $entity):array
57
    {
58 2
        return $this->serializer->normalize($entity, 'json');
59
    }
60
61
    /**
62
     * @param array  $data
63
     * @param string $class
64
     *
65
     * @return Traversable
66
     */
67 1
    public function arrayToCollection(array $data, string $class):Traversable
68
    {
69 1
        $result = new ArrayCollection();
70
71 1
        foreach ($data as $item) {
72 1
            $result->add($this->arrayToEntity($item, $class));
73
        }
74
75 1
        return $result;
76
    }
77
78
    /**
79
     * @param Entity [] $collection
80
     *
81
     * @return array
82
     */
83 1
    public function collectionToArray(Traversable $collection):array
84
    {
85 1
        $result = [];
86
87 1
        foreach($collection as $entity) {
88 1
            $result[] = $this->entityToArray($entity);
89
        }
90
91 1
        return $result;
92
    }
93
}