File tree Expand file tree Collapse file tree 6 files changed +60
-6
lines changed
Expand file tree Collapse file tree 6 files changed +60
-6
lines changed Original file line number Diff line number Diff line change 88final readonly class MapProvider
99{
1010 /**
11- * @param ?string $source from which source type this provider should apply
12- * @param string $provider the provider class name or service identifier
11+ * @param ?string $source from which source type this provider should apply
12+ * @param false| string $provider the provider class name or service identifier, set false to disable any provider
1313 */
1414 public function __construct (
15- public string $ provider ,
15+ public false | string $ provider ,
1616 public ?string $ source = null ,
1717 ) {
1818 }
Original file line number Diff line number Diff line change @@ -68,7 +68,7 @@ public function __invoke(GenerateMapperEvent $event): void
6868 }
6969
7070 if ($ event ->mapperMetadata ->source === 'array ' && $ this ->resourceClassResolver ->isResourceClass ($ event ->mapperMetadata ->target )) {
71- $ event ->provider = IriProvider::class;
71+ $ event ->provider ?? = IriProvider::class;
7272 }
7373 }
7474}
Original file line number Diff line number Diff line change @@ -21,6 +21,6 @@ public function __invoke(GenerateMapperEvent $event): void
2121 return ;
2222 }
2323
24- $ event ->provider = DoctrineProvider::class;
24+ $ event ->provider ?? = DoctrineProvider::class;
2525 }
2626}
Original file line number Diff line number Diff line change @@ -51,6 +51,16 @@ public function __invoke(GenerateMapperEvent $event): void
5151 }
5252 }
5353
54- $ event ->provider ??= $ provider ?? $ defaultMapProvider ;
54+ $ eventProvider = $ provider ?? $ defaultMapProvider ;
55+
56+ if (null === $ eventProvider ) {
57+ return ;
58+ }
59+
60+ if (false === $ eventProvider ) {
61+ $ event ->provider = null ;
62+ } else {
63+ $ event ->provider = $ eventProvider ;
64+ }
5565 }
5666}
Original file line number Diff line number Diff line change 88use AutoMapper \Tests \AutoMapperBuilder ;
99use AutoMapper \Tests \AutoMapperTestCase ;
1010use AutoMapper \Tests \Doctrine \Entity \Book ;
11+ use AutoMapper \Tests \Doctrine \Entity \Foo ;
1112use AutoMapper \Tests \Doctrine \Entity \Review ;
1213use AutoMapper \Tests \Doctrine \Entity \User ;
1314use Doctrine \DBAL \DriverManager ;
@@ -115,4 +116,26 @@ public function testAutoMappingMultipleIdentifiers(): void
115116 $ this ->assertEquals (5 , $ review ->rating );
116117 $ this ->assertEquals ('Bar ' , $ review ->user ->name );
117118 }
119+
120+ public function testDisabledProvider (): void
121+ {
122+ $ foo = new Foo ();
123+ $ foo ->foo = 'Initial ' ;
124+
125+ $ this ->entityManager ->persist ($ foo );
126+ $ this ->entityManager ->flush ();
127+
128+ $ this ->assertNotNull ($ foo ->id );
129+
130+ $ bookArray = $ this ->autoMapper ->map ($ foo , 'array ' );
131+ $ bookArray ['foo ' ] = 'John Doe ' ;
132+
133+ $ this ->autoMapper ->map ($ bookArray , Foo::class);
134+ $ this ->entityManager ->flush ();
135+ $ this ->entityManager ->clear ();
136+
137+ $ foo = $ this ->entityManager ->find (Foo::class, $ foo ->id );
138+
139+ $ this ->assertEquals ('Initial ' , $ foo ->foo );
140+ }
118141}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace AutoMapper \Tests \Doctrine \Entity ;
6+
7+ use AutoMapper \Attribute \MapProvider ;
8+ use Doctrine \ORM \Mapping as ORM ;
9+
10+ #[ORM \Entity]
11+ #[MapProvider(provider: false )]
12+ class Foo
13+ {
14+ #[ORM \Id]
15+ #[ORM \Column(type: 'integer ' )]
16+ #[ORM \GeneratedValue]
17+ public ?int $ id = null ;
18+
19+ #[ORM \Column(type: 'string ' )]
20+ public string $ foo = '' ;
21+ }
You can’t perform that action at this time.
0 commit comments