22
33namespace PTS \DataTransformer ;
44
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 ;
69use function get_class ;
710use function is_callable ;
811
@@ -11,14 +14,16 @@ class DataTransformer implements DataTransformerInterface
1114 protected const FILTER_TYPE_POPULATE = 'populate ' ;
1215 protected const FILTER_TYPE_EXTRACT = 'extract ' ;
1316
14- protected HydratorService $ hydratorService ;
17+ protected ExtractorInterface $ extractor ;
18+ protected HydratorInterface $ hydrator ;
1519 protected MapsManager $ mapsManager ;
1620
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+ }
2227
2328 public function getMapsManager (): MapsManager
2429 {
@@ -30,18 +35,18 @@ public function toModel(string $class, array $dto, string $mapName = 'dto'): obj
3035 $ map = $ this ->mapsManager ->getMap ($ class , $ mapName );
3136 $ dto = $ map ['refs ' ] ? $ this ->resolveRefPopulate ($ dto , $ map ['refs ' ]) : $ dto ;
3237 $ 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 ' ]);
3439 }
3540
36- public function toModelsCollection (string $ class , array $ dtoCollection , string $ mapName = 'dto ' ): array
41+ public function toModelsCollection (string $ class , iterable $ dtoCollection , string $ mapName = 'dto ' ): array
3742 {
3843 $ map = $ this ->mapsManager ->getMap ($ class , $ mapName );
3944
4045 $ models = [];
4146 foreach ($ dtoCollection as $ dto ) {
4247 $ dto = $ map ['refs ' ] ? $ this ->resolveRefPopulate ($ dto , $ map ['refs ' ]) : $ dto ;
4348 $ 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 ' ]);
4550 }
4651
4752 return $ models ;
@@ -52,12 +57,12 @@ public function fillModel(object $model, array $dto, string $mapName = 'dto'): o
5257 $ map = $ this ->mapsManager ->getMap (get_class ($ model ), $ mapName );
5358 $ dto = $ map ['refs ' ] ? $ this ->resolveRefPopulate ($ dto , $ map ['refs ' ]) : $ dto ;
5459 $ 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 ' ]);
5661
5762 return $ model ;
5863 }
5964
60- public function toDtoCollection (array $ models , string $ mapName = 'dto ' , array $ options = []): array
65+ public function toDtoCollection (iterable $ models , string $ mapName = 'dto ' , array $ options = []): array
6166 {
6267 $ collection = [];
6368 foreach ($ models as $ key => $ model ) {
@@ -77,7 +82,7 @@ public function toDTO(object $model, string $mapName = 'dto', array $options = [
7782 unset($ map ['pipe ' ][$ name ], $ map ['rules ' ][$ name ], $ map ['refs ' ][$ name ]);
7883 }
7984
80- $ dto = $ this ->hydratorService ->extract ($ model , $ map ['rules ' ]);
85+ $ dto = $ this ->extractor ->extract ($ model , $ map ['rules ' ]);
8186 $ dto = $ map ['pipe ' ] ? $ this ->applyPipes ($ dto , $ map ['pipe ' ], self ::FILTER_TYPE_EXTRACT ) : $ dto ;
8287 return $ map ['refs ' ] ? $ this ->resolveRefExtract ($ dto , $ map ['refs ' ]) : $ dto ;
8388 }
0 commit comments