ListSerializer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 38
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serializeList() 0 11 2
A deserializeList() 0 19 3
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\ReadModel\ValueObject;
7
8
9
use Gica\Iterator\IteratorTransformer\IteratorMapper;
10
use Gica\Types\SerializableInterface;
11
12
class ListSerializer
13
{
14
    /**
15
     * @param SerializableInterface[] $objectList
16
     * @return array
17
     */
18 1
    public static function serializeList($objectList)
19
    {
20 1
        $objectList = $objectList ?: [];
21
22 1
        $iteratorMapper = new IteratorMapper(function ($object) {
23
            /** @var SerializableInterface $object */
24 1
            return $object->serialize();
25 1
        });
26
27 1
        return iterator_to_array($iteratorMapper($objectList));
28
    }
29
30 4
    public static function deserializeList(string $objectClass, $serializedObjectList = null)
31
    {
32 4
        if ($serializedObjectList instanceof \Iterator) {
33 1
            $serializedObjectList = iterator_to_array($serializedObjectList);
34
        }
35
36 4
        if (!$serializedObjectList) {
37 3
            return [];
38
        }
39
40 1
        $serializedObjectList = array_filter($serializedObjectList, function ($objectArray) use ($serializedObjectList) {
41 1
            return $objectArray;
42 1
        });
43
44 1
        return array_map(function ($objectArray) use ($objectClass) {
45
46 1
            return call_user_func([$objectClass, 'deserialize'], $objectArray);
47 1
        }, $serializedObjectList);
48
    }
49
}