1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thruster\Component\DataMapper; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
6
|
|
|
use Thruster\Component\DataMapper\Exception\DataMapperNotFoundException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DataMappers |
10
|
|
|
* |
11
|
|
|
* @package Thruster\Component\DataMapper |
12
|
|
|
* @author Aurimas Niekis <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class DataMappers |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DataMapperInterface[] |
18
|
|
|
*/ |
19
|
|
|
protected $dataMappers; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var DataMapper[] |
23
|
|
|
*/ |
24
|
|
|
protected $wrappedDataMappers; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ValidatorInterface |
28
|
|
|
*/ |
29
|
|
|
protected $validator; |
30
|
|
|
|
31
|
5 |
|
public function __construct() |
32
|
|
|
{ |
33
|
5 |
|
$this->dataMappers = []; |
34
|
5 |
|
$this->wrappedDataMappers = []; |
35
|
5 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return ValidatorInterface |
39
|
|
|
*/ |
40
|
2 |
|
public function getValidator() |
41
|
|
|
{ |
42
|
2 |
|
return $this->validator; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ValidatorInterface $validator |
47
|
|
|
* |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
1 |
|
public function setValidator($validator) |
51
|
|
|
{ |
52
|
1 |
|
$this->validator = $validator; |
53
|
|
|
|
54
|
1 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param DataMapperInterface $dataMapper |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
2 |
|
public function addMapper(DataMapperInterface $dataMapper, string $name = null) : self |
63
|
|
|
{ |
64
|
2 |
|
if (null === $name) { |
65
|
1 |
|
$name = get_class($dataMapper); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
$this->dataMappers[$name] = $dataMapper; |
69
|
|
|
|
70
|
2 |
|
$dataMapper->setDataMappers($this); |
71
|
|
|
|
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $class |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
3 |
|
public function hasMapper(string $class) : bool |
81
|
|
|
{ |
82
|
3 |
|
return array_key_exists($class, $this->dataMappers); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $class |
87
|
|
|
* |
88
|
|
|
* @return DataMapper |
89
|
|
|
* @throws DataMapperNotFoundException |
90
|
|
|
*/ |
91
|
3 |
|
public function getMapper(string $class) : DataMapper |
92
|
|
|
{ |
93
|
3 |
|
if (array_key_exists($class, $this->wrappedDataMappers)) { |
94
|
1 |
|
return $this->wrappedDataMappers[$class]; |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
if (false === $this->hasMapper($class)) { |
98
|
1 |
|
throw new DataMapperNotFoundException($class); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
$dataMapper = new DataMapper($this->dataMappers[$class]); |
102
|
2 |
|
$dataMapper->setValidator($this->getValidator()); |
103
|
|
|
|
104
|
2 |
|
$this->wrappedDataMappers[$class] = $dataMapper; |
105
|
|
|
|
106
|
2 |
|
return $dataMapper; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $class |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
1 |
|
public function removeMapper(string $class) : self |
115
|
|
|
{ |
116
|
1 |
|
unset($this->dataMappers[$class]); |
117
|
1 |
|
unset($this->wrappedDataMappers[$class]); |
118
|
|
|
|
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|