6
6
use Illuminate \Contracts \Support \Arrayable ;
7
7
use Illuminate \Support \Arr ;
8
8
use Illuminate \Support \Str ;
9
+ use InvalidArgumentException ;
9
10
10
11
/**
11
- * @method void setDriver(string $value)
12
12
* @method void setHost(string $value)
13
- * @method void setPort(string $value)
14
13
* @method void setDatabase(string|null $value)
15
14
* @method void setUsername(string $value)
16
15
* @method void setPassword(string $value)
17
- * @method void setSchema(string $value )
18
- * @method void setSslmode(string $value )
16
+ * @method bool hasDatabase( )
17
+ * @method bool doesntDatabase( )
19
18
*/
20
19
final class Configuration implements Arrayable
21
20
{
22
21
use Makeable;
23
22
24
- protected $ config = [
25
- 'driver ' => null ,
26
- 'url ' => null ,
27
- 'host ' => null ,
28
- 'port ' => null ,
29
- 'database ' => null ,
30
- 'username ' => null ,
31
- 'password ' => null ,
32
- 'unix_socket ' => '' ,
33
- 'charset ' => 'utf8mb4 ' ,
34
- 'collation ' => 'utf8mb4_unicode_ci ' ,
35
- 'prefix ' => '' ,
36
- 'prefix_indexes ' => true ,
37
- 'strict ' => true ,
38
- 'engine ' => null ,
39
- 'options ' => [],
40
- ];
41
-
42
- public function __call (string $ name , array $ value ): void
23
+ protected $ config = [];
24
+
25
+ public function __call (string $ name , array $ value )
43
26
{
44
- Arr::set ($ this ->config , $ this ->resolveKeyName ($ name ), $ this ->castValue ($ value [0 ]));
27
+ $ key = $ this ->resolveKeyName ($ name );
28
+
29
+ switch (true ) {
30
+ case Str::startsWith ($ name , 'set ' ):
31
+ return $ this ->set ($ key , $ value );
32
+
33
+ case Str::startsWith ($ name , 'has ' ):
34
+ return $ this ->has ($ key );
35
+
36
+ case Str::startsWith ($ name , 'doesnt ' ):
37
+ return ! $ this ->has ($ key );
38
+
39
+ default :
40
+ throw new InvalidArgumentException ('Unknown method: ' . $ name );
41
+ }
45
42
}
46
43
47
44
public function merge (array $ config ): self
@@ -56,6 +53,20 @@ public function toArray(): array
56
53
return $ this ->config ;
57
54
}
58
55
56
+ protected function set (string $ key , $ value ): self
57
+ {
58
+ Arr::set ($ this ->config , $ key , $ this ->castValue ($ value [0 ]));
59
+
60
+ return $ this ;
61
+ }
62
+
63
+ protected function has (string $ key ): bool
64
+ {
65
+ $ value = Arr::get ($ this ->config , $ key );
66
+
67
+ return ! empty ($ value );
68
+ }
69
+
59
70
protected function resolveKeyName (string $ name ): string
60
71
{
61
72
return (string ) Str::of ($ name )->snake ()->after ('_ ' );
0 commit comments