2
2
3
3
namespace PTS \DataTransformer ;
4
4
5
- use PTS \Hydrator \HydratorService ;
5
+ use PTS \Hydrator \Extractor ;
6
+ use PTS \Hydrator \ExtractorInterface ;
7
+ use PTS \Hydrator \Hydrator ;
8
+ use PTS \Hydrator \HydratorInterface ;
6
9
use function get_class ;
7
10
use function is_callable ;
8
11
@@ -11,14 +14,16 @@ class DataTransformer implements DataTransformerInterface
11
14
protected const FILTER_TYPE_POPULATE = 'populate ' ;
12
15
protected const FILTER_TYPE_EXTRACT = 'extract ' ;
13
16
14
- protected HydratorService $ hydratorService ;
17
+ protected ExtractorInterface $ extractor ;
18
+ protected HydratorInterface $ hydrator ;
15
19
protected MapsManager $ mapsManager ;
16
20
17
- public function __construct (HydratorService $ hydratorService = null , MapsManager $ mapsManager = null )
18
- {
19
- $ this ->hydratorService = $ hydratorService ?? new HydratorService ;
20
- $ this ->mapsManager = $ mapsManager ?? new MapsManager ;
21
- }
21
+ public function __construct (ExtractorInterface $ extractor = null , HydratorInterface $ hydrator = null , MapsManager $ mapsManager = null )
22
+ {
23
+ $ this ->extractor = $ extractor ?? new Extractor ;
24
+ $ this ->hydrator = $ hydrator ?? new Hydrator ;
25
+ $ this ->mapsManager = $ mapsManager ?? new MapsManager ;
26
+ }
22
27
23
28
public function getMapsManager (): MapsManager
24
29
{
@@ -30,18 +35,18 @@ public function toModel(string $class, array $dto, string $mapName = 'dto'): obj
30
35
$ map = $ this ->mapsManager ->getMap ($ class , $ mapName );
31
36
$ dto = $ map ['refs ' ] ? $ this ->resolveRefPopulate ($ dto , $ map ['refs ' ]) : $ dto ;
32
37
$ dto = $ map ['pipe ' ] ? $ this ->applyPipes ($ dto , $ map ['pipe ' ]) : $ dto ;
33
- return $ this ->hydratorService ->hydrate ($ dto , $ class , $ map ['rules ' ]);
38
+ return $ this ->hydrator ->hydrate ($ dto , $ class , $ map ['rules ' ]);
34
39
}
35
40
36
- public function toModelsCollection (string $ class , array $ dtoCollection , string $ mapName = 'dto ' ): array
41
+ public function toModelsCollection (string $ class , iterable $ dtoCollection , string $ mapName = 'dto ' ): array
37
42
{
38
43
$ map = $ this ->mapsManager ->getMap ($ class , $ mapName );
39
44
40
45
$ models = [];
41
46
foreach ($ dtoCollection as $ dto ) {
42
47
$ dto = $ map ['refs ' ] ? $ this ->resolveRefPopulate ($ dto , $ map ['refs ' ]) : $ dto ;
43
48
$ dto = $ map ['pipe ' ] ? $ this ->applyPipes ($ dto , $ map ['pipe ' ]) : $ dto ;
44
- $ models [] = $ this ->hydratorService ->hydrate ($ dto , $ class , $ map ['rules ' ]);
49
+ $ models [] = $ this ->hydrator ->hydrate ($ dto , $ class , $ map ['rules ' ]);
45
50
}
46
51
47
52
return $ models ;
@@ -52,12 +57,12 @@ public function fillModel(object $model, array $dto, string $mapName = 'dto'): o
52
57
$ map = $ this ->mapsManager ->getMap (get_class ($ model ), $ mapName );
53
58
$ dto = $ map ['refs ' ] ? $ this ->resolveRefPopulate ($ dto , $ map ['refs ' ]) : $ dto ;
54
59
$ dto = $ map ['pipe ' ] ? $ this ->applyPipes ($ dto , $ map ['pipe ' ]) : $ dto ;
55
- $ this ->hydratorService ->hydrateModel ($ dto , $ model , $ map ['rules ' ]);
60
+ $ this ->hydrator ->hydrateModel ($ dto , $ model , $ map ['rules ' ]);
56
61
57
62
return $ model ;
58
63
}
59
64
60
- public function toDtoCollection (array $ models , string $ mapName = 'dto ' , array $ options = []): array
65
+ public function toDtoCollection (iterable $ models , string $ mapName = 'dto ' , array $ options = []): array
61
66
{
62
67
$ collection = [];
63
68
foreach ($ models as $ key => $ model ) {
@@ -77,7 +82,7 @@ public function toDTO(object $model, string $mapName = 'dto', array $options = [
77
82
unset($ map ['pipe ' ][$ name ], $ map ['rules ' ][$ name ], $ map ['refs ' ][$ name ]);
78
83
}
79
84
80
- $ dto = $ this ->hydratorService ->extract ($ model , $ map ['rules ' ]);
85
+ $ dto = $ this ->extractor ->extract ($ model , $ map ['rules ' ]);
81
86
$ dto = $ map ['pipe ' ] ? $ this ->applyPipes ($ dto , $ map ['pipe ' ], self ::FILTER_TYPE_EXTRACT ) : $ dto ;
82
87
return $ map ['refs ' ] ? $ this ->resolveRefExtract ($ dto , $ map ['refs ' ]) : $ dto ;
83
88
}
0 commit comments