66use Illuminate \Contracts \Support \Arrayable ;
77use Illuminate \Support \Arr ;
88use Illuminate \Support \Str ;
9+ use InvalidArgumentException ;
910
1011/**
11- * @method void setDriver(string $value)
1212 * @method void setHost(string $value)
13- * @method void setPort(string $value)
1413 * @method void setDatabase(string|null $value)
1514 * @method void setUsername(string $value)
1615 * @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( )
1918 */
2019final class Configuration implements Arrayable
2120{
2221 use Makeable;
2322
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 )
4326 {
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+ }
4542 }
4643
4744 public function merge (array $ config ): self
@@ -56,6 +53,20 @@ public function toArray(): array
5653 return $ this ->config ;
5754 }
5855
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+
5970 protected function resolveKeyName (string $ name ): string
6071 {
6172 return (string ) Str::of ($ name )->snake ()->after ('_ ' );
0 commit comments