2323
2424final class Mapper implements NormalizerInterface, DenormalizerInterface
2525{
26- /**
27- * @var \WeakMap<DirectionInterface, TypeRepositoryInterface>
28- */
29- private readonly \WeakMap $ repository ;
26+ private readonly TypeRepositoryInterface $ types ;
3027
3128 public readonly MapperContext $ context ;
3229
3330 public function __construct (
3431 private readonly PlatformInterface $ platform = new StandardPlatform (),
3532 private readonly Configuration $ config = new Configuration (),
3633 ) {
37- $ this ->repository = new \WeakMap ();
38-
3934 $ this ->context = $ this ->createMapperContext (
4035 context: $ this ->createBootContext (),
4136 );
37+
38+ $ this ->types = $ this ->createTypeRepository (
39+ context: $ this ->context ,
40+ );
4241 }
4342
4443 private function createBootContext (): BootContext
@@ -72,15 +71,11 @@ private function createTypeParser(BootContext $context): TypeParserInterface
7271 return $ factory ->createTypeParser ($ context );
7372 }
7473
75- private function getTypeRepository ( DirectionInterface $ direction ): TypeRepositoryInterface
74+ private function createTypeRepository ( MapperContext $ context ): TypeRepositoryInterface
7675 {
77- $ factory = $ this ->config ->getTypeRepositoryFactory ();
76+ $ factory = $ context ->config ->getTypeRepositoryFactory ();
7877
79- return $ this ->repository [$ direction ]
80- ??= $ factory ->createTypeRepository (
81- context: $ this ->context ,
82- direction: $ direction ,
83- );
78+ return $ factory ->createTypeRepository ($ context );
8479 }
8580
8681 private function createRuntimeContext (mixed $ value , DirectionInterface $ direction ): RuntimeContext
@@ -89,7 +84,7 @@ private function createRuntimeContext(mixed $value, DirectionInterface $directio
8984 context: $ this ->context ,
9085 value: $ value ,
9186 direction: $ direction ,
92- types: $ this ->getTypeRepository ( $ direction ) ,
87+ types: $ this ->types ,
9388 );
9489 }
9590
@@ -166,11 +161,9 @@ public function canMap(DirectionInterface $direction, mixed $value, #[Language('
166161 * @throws TypeNotFoundException in case of type not found
167162 * @throws \Throwable in case of internal error occurs
168163 */
169- public function getType (DirectionInterface $ direction , #[Language('PHP ' )] string $ type ): TypeInterface
164+ public function getType (#[Language('PHP ' )] string $ type ): TypeInterface
170165 {
171- $ repository = $ this ->getTypeRepository ($ direction );
172-
173- return $ repository ->getTypeByStatement (
166+ return $ this ->types ->getTypeByStatement (
174167 statement: $ this ->context ->getStatementByDefinition ($ type ),
175168 );
176169 }
@@ -183,12 +176,11 @@ public function getType(DirectionInterface $direction, #[Language('PHP')] string
183176 * @throws TypeNotFoundException in case of type not found
184177 * @throws \Throwable in case of internal error occurs
185178 */
186- public function getTypeByValue (DirectionInterface $ direction , mixed $ value ): TypeInterface
179+ public function getTypeByValue (mixed $ value ): TypeInterface
187180 {
188- return $ this ->getType (
189- direction: $ direction ,
190- type: $ this ->context ->getDefinitionByValue ($ value ),
191- );
181+ $ definition = $ this ->context ->getDefinitionByValue ($ value );
182+
183+ return $ this ->getType ($ definition );
192184 }
193185
194186 /**
@@ -204,12 +196,12 @@ public function getTypeByValue(DirectionInterface $direction, mixed $value): Typ
204196 * @throws TypeNotFoundException
205197 * @throws \Throwable
206198 */
207- public function warmup (DirectionInterface $ direction , string |object $ class ): void
199+ public function warmup (string |object $ class ): void
208200 {
209201 if (\is_object ($ class )) {
210202 $ class = $ class ::class;
211203 }
212204
213- $ this ->getType ($ direction , $ class );
205+ $ this ->getType ($ class );
214206 }
215207}
0 commit comments