6
6
use PTS \Hydrator \ExtractorInterface ;
7
7
use PTS \Hydrator \Hydrator ;
8
8
use PTS \Hydrator \HydratorInterface ;
9
- use function get_class ;
10
- use function is_callable ;
11
9
12
10
class DataTransformer implements DataTransformerInterface
13
11
{
14
- protected const FILTER_TYPE_POPULATE = 'populate ' ;
15
- protected const FILTER_TYPE_EXTRACT = 'extract ' ;
16
-
17
12
protected ExtractorInterface $ extractor ;
18
13
protected HydratorInterface $ hydrator ;
19
14
protected MapsManager $ mapsManager ;
@@ -37,19 +32,16 @@ public function toModel(string $class, array $dto, string $mapName = 'dto'): obj
37
32
{
38
33
$ map = $ this ->mapsManager ->getMap ($ class , $ mapName );
39
34
$ dto = $ map ['refs ' ] ? $ this ->resolveRefPopulate ($ dto , $ map ['refs ' ]) : $ dto ;
40
- $ dto = $ map ['pipe ' ] ? $ this ->applyPipes ($ dto , $ map ['pipe ' ]) : $ dto ;
35
+ $ dto = $ map ['pipe-populate ' ] ? $ this ->pipes ($ dto , $ map ['pipe-populate ' ]) : $ dto ;
41
36
return $ this ->hydrator ->hydrate ($ dto , $ class , $ map ['rules ' ]);
42
37
}
43
38
44
39
public function toModelsCollection (string $ model , iterable $ dtoCollection , string $ mapType = 'dto ' ): array
45
40
{
46
- $ map = $ this ->mapsManager ->getMap ($ model , $ mapType );
47
-
48
41
$ models = [];
49
- foreach ($ dtoCollection as $ dto ) {
50
- $ dto = $ map ['refs ' ] ? $ this ->resolveRefPopulate ($ dto , $ map ['refs ' ]) : $ dto ;
51
- $ dto = $ map ['pipe ' ] ? $ this ->applyPipes ($ dto , $ map ['pipe ' ]) : $ dto ;
52
- $ models [] = $ this ->hydrator ->hydrate ($ dto , $ model , $ map ['rules ' ]);
42
+ foreach ($ dtoCollection as $ key => $ dto ) {
43
+ $ dto = $ this ->toModel ($ model , $ dto , $ mapType );
44
+ $ models [$ key ] = $ dto ;
53
45
}
54
46
55
47
return $ models ;
@@ -59,7 +51,7 @@ public function fillModel(object $model, array $dto, string $mapName = 'dto'): o
59
51
{
60
52
$ map = $ this ->mapsManager ->getMap ($ model ::class, $ mapName );
61
53
$ dto = $ map ['refs ' ] ? $ this ->resolveRefPopulate ($ dto , $ map ['refs ' ]) : $ dto ;
62
- $ dto = $ map ['pipe ' ] ? $ this ->applyPipes ($ dto , $ map ['pipe ' ]) : $ dto ;
54
+ $ dto = $ map ['pipe-populate ' ] ? $ this ->pipes ($ dto , $ map ['pipe-populate ' ]) : $ dto ;
63
55
$ this ->hydrator ->hydrateModel ($ dto , $ model , $ map ['rules ' ]);
64
56
65
57
return $ model ;
@@ -86,7 +78,7 @@ public function toDTO(object $model, string $mapName = 'dto', array $options = [
86
78
}
87
79
88
80
$ dto = $ this ->extractor ->extract ($ model , $ map ['rules ' ]);
89
- $ dto = $ map ['pipe ' ] ? $ this ->applyPipes ($ dto , $ map ['pipe ' ], self :: FILTER_TYPE_EXTRACT ) : $ dto ;
81
+ $ dto = $ map ['pipe-extract ' ] ? $ this ->pipes ($ dto , $ map ['pipe-extract ' ] ) : $ dto ;
90
82
return $ map ['refs ' ] ? $ this ->resolveRefExtract ($ dto , $ map ['refs ' ]) : $ dto ;
91
83
}
92
84
@@ -124,28 +116,18 @@ protected function resolveRefPopulate(array $dto, array $refsRules): array
124
116
return $ dto ;
125
117
}
126
118
127
- protected function applyPipes (array $ dto , array $ pipes, string $ type = self :: FILTER_TYPE_POPULATE ): array
119
+ protected function pipes (array $ dto , array $ pipes ): array
128
120
{
129
121
$ fieldsPipes = array_intersect_key ($ pipes , $ dto );
130
122
foreach ($ fieldsPipes as $ name => $ filters ) {
131
123
$ value = $ dto [$ name ] ?? null ;
132
- $ dto [$ name ] = $ this ->applyFilters ($ value , $ filters , $ type );
133
- }
134
-
135
- return $ dto ;
136
- }
137
-
138
- protected function applyFilters ($ value , array $ filters , string $ type ): mixed
139
- {
140
- foreach ($ filters as $ filter ) {
141
- if (is_callable ($ filter )) {
124
+ foreach ($ filters as $ filter ) {
142
125
$ value = $ filter ($ value );
143
- continue ;
144
126
}
145
127
146
- $ value = ( $ filter [ $ type ] ?? false ) ? $ filter [ $ type ]( $ value ) : $ value ;
128
+ $ dto [ $ name ] = $ value ;
147
129
}
148
130
149
- return $ value ;
131
+ return $ dto ;
150
132
}
151
133
}
0 commit comments