Completed
Push — master ( 4413ea...c99ea4 )
by Aurimas
02:59
created

DataMapper::mapCollection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Thruster\Component\DataMapper;
4
5
use Symfony\Component\Validator\Validator\ValidatorInterface;
6
use Thruster\Component\DataMapper\Exception\DataMapperOutputNotValidException;
7
use Thruster\Component\DataMapper\Exception\NotSupportedInputForDataMapperException;
8
9
/**
10
 * Class DataMapper
11
 *
12
 * @package Thruster\Component\DataMapper
13
 * @author  Aurimas Niekis <[email protected]>
14
 */
15
class DataMapper
16
{
17
    /**
18
     * @var DataMapperInterface
19
     */
20
    protected $dataMapper;
21
22
    /**
23
     * @var ValidatorInterface
24
     */
25
    protected $validator;
26
27 6
    public function __construct(DataMapperInterface $dataMapper)
28
    {
29 6
        $this->dataMapper = $dataMapper;
30 6
    }
31
32
    /**
33
     * @return ValidatorInterface
34
     */
35 2
    public function getValidator()
36
    {
37 2
        return $this->validator;
38
    }
39
40
    /**
41
     * @param ValidatorInterface $validator
42
     *
43
     * @return $this
44
     */
45 2
    public function setValidator($validator)
46
    {
47 2
        $this->validator = $validator;
48
49 2
        return $this;
50
    }
51
52
    /**
53
     * @return DataMapperInterface
54
     */
55 6
    public function getDataMapper()
56
    {
57 6
        return $this->dataMapper;
58
    }
59
60
    /**
61
     * @param mixed $input
62
     * @param bool $preserveKey
63
     *
64
     * @return array
65
     * @throws DataMapperOutputNotValidException
66
     * @throws NotSupportedInputForDataMapperException
67
     */
68 2
    public function mapCollection($input, bool $preserveKey = true) : array
69
    {
70 2
        $result = [];
71
72 2
        foreach ($input as $key => $item) {
73 2
            if (true === $preserveKey) {
74 2
                $result[$key] = $this->map($item);
75 2
                continue;
76
            }
77
78 1
            $result[] = $this->map($item);
79
        }
80
81 2
        return $result;
82
    }
83
84
    /**
85
     * @param mixed $input
86
     *
87
     * @return mixed
88
     * @throws DataMapperOutputNotValidException
89
     * @throws NotSupportedInputForDataMapperException
90
     */
91 5
    public function map($input)
92
    {
93 5
        $dataMapper = $this->getDataMapper();
94
95 5
        if (false === $dataMapper->supports($input)) {
96 1
            throw new NotSupportedInputForDataMapperException($input, $dataMapper);
97
        }
98
99 4
        $output = $dataMapper->map($input);
100
101 4
        if ($dataMapper instanceof ValidateableDataMapperInterface) {
102 1
            $violations = $this->getValidator()->validate(
103
                $output,
104 1
                null,
105 1
                $dataMapper->getValidationGroups($input)
106
            );
107
108 1
            if (count($violations) > 0) {
109 1
                throw new DataMapperOutputNotValidException($violations);
110
            }
111
        }
112
113 3
        return $output;
114
    }
115
}
116