Config.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace inter\storage\mysql;
  3. use Exception;
  4. class Config
  5. {
  6. private string $host;
  7. private string $port;
  8. private string $user;
  9. private string $password;
  10. private string $database;
  11. public function __construct(string $host, string $port, string $user, string $password, string $database)
  12. {
  13. if ($host == "") {
  14. throw new Exception("数据库连接host为空");
  15. }
  16. if ($user == "") {
  17. throw new Exception("数据库用户为空");
  18. }
  19. if ($password == "") {
  20. throw new Exception("数据库密码为空");
  21. }
  22. if ($database == "") {
  23. throw new Exception("数据库名字为空");
  24. }
  25. $this->host = $host;
  26. $this->port = $port;
  27. $this->user = $user;
  28. $this->password = $password;
  29. $this->database = $database;
  30. }
  31. public function GetPdoDSN(): string
  32. {
  33. return "mysql:host={$this->host}" . (empty($this->port) ? "" : ":{$this->port}") . ";dbname={$this->database}";
  34. }
  35. public function GetUser(): string
  36. {
  37. return $this->user;
  38. }
  39. public function GetPassword(): string
  40. {
  41. return $this->password;
  42. }
  43. }