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
|
10 |
|
public function __construct(DataMapperInterface $dataMapper) |
28
|
|
|
{ |
29
|
10 |
|
$this->dataMapper = $dataMapper; |
30
|
10 |
|
} |
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
|
3 |
|
public function setValidator($validator) |
46
|
|
|
{ |
47
|
3 |
|
$this->validator = $validator; |
48
|
|
|
|
49
|
3 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return DataMapperInterface |
54
|
|
|
*/ |
55
|
7 |
|
public function getDataMapper() |
56
|
|
|
{ |
57
|
7 |
|
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
|
3 |
|
public function mapCollection($input, bool $preserveKey = true) : array |
69
|
|
|
{ |
70
|
3 |
|
if (empty($input)) { |
71
|
1 |
|
return []; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$result = []; |
75
|
|
|
|
76
|
2 |
|
foreach ($input as $key => $item) { |
77
|
2 |
|
if (true === $preserveKey) { |
78
|
2 |
|
$result[$key] = $this->map($item); |
79
|
2 |
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$result[] = $this->map($item); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param mixed $input |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
* @throws DataMapperOutputNotValidException |
93
|
|
|
* @throws NotSupportedInputForDataMapperException |
94
|
|
|
*/ |
95
|
6 |
|
public function map($input) |
96
|
|
|
{ |
97
|
6 |
|
if (null === $input) { |
98
|
1 |
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
5 |
|
$dataMapper = $this->getDataMapper(); |
102
|
|
|
|
103
|
5 |
|
if (false === $dataMapper->supports($input)) { |
104
|
1 |
|
throw new NotSupportedInputForDataMapperException($input, $dataMapper); |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
$output = $dataMapper->map($input); |
108
|
|
|
|
109
|
4 |
|
if ($dataMapper instanceof ValidateableDataMapperInterface) { |
110
|
1 |
|
$violations = $this->getValidator()->validate( |
111
|
|
|
$output, |
112
|
1 |
|
null, |
113
|
1 |
|
$dataMapper->getValidationGroups($input) |
114
|
|
|
); |
115
|
|
|
|
116
|
1 |
|
if (count($violations) > 0) { |
117
|
1 |
|
throw new DataMapperOutputNotValidException($violations); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
return $output; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|