@@ -28,41 +28,88 @@ final class Factory
28
28
private const TypeConverterOptions = ['convertBoolean ' , 'convertDateTime ' , 'convertDecimal ' , 'newDateTime ' ];
29
29
30
30
31
- /** @internal */
32
- public static function createDriverFromDsn (
31
+ public static function createFromParameters (
32
+ #[\SensitiveParameter]
33
+ ...$ params ,
34
+ ): Explorer
35
+ {
36
+ $ params = count ($ params ) === 1 && is_array ($ params [0 ] ?? null ) ? $ params [0 ] : $ params ;
37
+ $ driver = self ::createDriverFromParameters ($ params );
38
+ return self ::createExplorer ($ driver , $ params );
39
+ }
40
+
41
+
42
+ public static function createFromDsn (
33
43
string $ dsn ,
34
44
?string $ username = null ,
35
45
#[\SensitiveParameter]
36
46
?string $ password = null ,
37
47
array $ options = [],
48
+ ): Explorer
49
+ {
50
+ $ driver = self ::createDriverFromDsn ($ dsn , $ username , $ password , $ options );
51
+ return self ::createExplorer ($ driver , $ options );
52
+ }
53
+
54
+
55
+ private static function createDriverFromParameters (
56
+ #[\SensitiveParameter]
57
+ array $ params ,
38
58
): Drivers \Driver
39
59
{
40
60
if ($ class = $ params ['driverClass ' ] ?? null ) {
41
61
if (!is_subclass_of ($ class , Drivers \Driver::class)) {
42
62
throw new \LogicException ("Driver class ' $ class' is not subclass of " . Drivers \Driver::class);
43
63
}
64
+ unset($ params ['driverClass ' ]);
44
65
45
- } else {
46
- $ driver = explode (': ' , $ dsn )[0 ];
47
- $ class = self ::Drivers['pdo- ' . $ driver ] ?? null ;
66
+ } elseif ($ driver = $ params ['driver ' ] ?? null ) {
67
+ $ class = self ::Drivers[$ driver ] ?? null ;
48
68
if (!$ class ) {
49
- throw new \LogicException ("Unknown PDO driver ' $ driver'. " );
69
+ throw new \LogicException ("Unknown driver ' $ driver'. " );
50
70
}
71
+ unset($ params ['driver ' ]);
72
+
73
+ } elseif ($ params ['dsn ' ] ?? null ) {
74
+ return self ::createDriverFromDsn (...$ params );
75
+
76
+ } else {
77
+ throw new \LogicException ("Missing options 'driver' or 'driverClass'. " );
51
78
}
52
79
80
+ $ params = array_diff_key ($ params , array_flip (self ::TypeConverterOptions));
81
+ return new $ class ($ params );
82
+ }
83
+
84
+
85
+ private static function createDriverFromDsn (
86
+ string $ dsn ,
87
+ ?string $ username ,
88
+ #[\SensitiveParameter]
89
+ ?string $ password ,
90
+ array $ options = [],
91
+ ): Drivers \Driver
92
+ {
93
+ $ options = array_diff_key ($ options , array_flip (self ::TypeConverterOptions));
94
+ $ driver = explode (': ' , $ dsn )[0 ];
95
+ $ class = self ::Drivers['pdo- ' . $ driver ] ?? null ;
96
+ if (!$ class ) {
97
+ throw new \LogicException ("Unknown PDO driver ' $ driver'. " );
98
+ }
53
99
return new $ class (['dsn ' => $ dsn , 'username ' => $ username , 'password ' => $ password , 'options ' => $ options ]);
54
100
}
55
101
56
102
57
- /** @internal */
58
- public static function configure (Explorer $ explorer , array $ options ): void
103
+ private static function createExplorer (Drivers \Driver $ driver , array $ options ): Explorer
59
104
{
105
+ $ explorer = new Explorer ($ driver );
60
106
$ converter = $ explorer ->getTypeConverter ();
61
- foreach (self ::TypeConverterOptions as $ opt ) {
62
- if (isset ($ options [$ opt ])) {
63
- $ converter ->$ opt = (bool ) $ options [$ opt ];
64
- unset($ options [$ opt ]);
107
+ foreach (self ::TypeConverterOptions as $ key ) {
108
+ if (isset ($ options [$ key ])) {
109
+ $ converter ->$ key = (bool ) $ options [$ key ];
110
+ unset($ options [$ key ]);
65
111
}
66
112
}
113
+ return $ explorer ;
67
114
}
68
115
}
0 commit comments