-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicoDatabaseCredentials.php
More file actions
199 lines (172 loc) · 4.7 KB
/
PicoDatabaseCredentials.php
File metadata and controls
199 lines (172 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace MagicObject\Database;
use InvalidArgumentException;
use MagicObject\SecretObject;
/**
* PicoDatabaseCredentials class
*
* This class encapsulates database credentials and utilizes the SecretObject to encrypt all attributes,
* ensuring the security of database configuration details from unauthorized access.
*
* It provides getter methods to retrieve database connection parameters such as driver, host, port,
* username, password, database name, schema, and application time zone.
*
* Example usage:
* ```php
* <?php
* $credentials = new PicoDatabaseCredentials();
* $credentials->setDriver('mysql');
* $credentials->setHost('localhost');
* $credentials->setPort(3306);
* $credentials->setUsername('user');
* $credentials->setPassword('password');
* $credentials->setDatabaseName('app');
* ```
*
* The attributes are automatically encrypted when set, providing a secure way to handle sensitive
* information within your application.
*
* @author Kamshory
* @package MagicObject\Database
* @link https://github.com/Planetbiru/MagicObject
*/
class PicoDatabaseCredentials extends SecretObject
{
/**
* Database driver (e.g., 'mysql', 'pgsql', 'mariadb', 'sqlite').
*
* @var string
*/
protected $driver;
/**
* Database file path for SQLite.
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $databaseFilePath;
/**
* Database server host.
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $host;
/**
* Database server port.
*
* @var int
*/
protected $port;
/**
* Database username.
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $username;
/**
* Database user password.
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $password;
/**
* Database name.
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $databaseName;
/**
* Database schema (default: 'public').
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $databaseSchema;
/**
* Application time zone.
*
* @var string
*/
protected $timeZone;
/**
* Charset
*
* @var string
*/
protected $charset;
/**
* Import database credentials from a URL datasource string.
*
* Supported format:
* scheme://username:password@host:port/database?schema=public&charset=utf8&timezone=Asia/Jakarta
*
* @param string $url The datasource URL
* @param string|null $username Optional username to override the one from URL
* @param string|null $password Optional password to override the one from URL
* @return self Returns the current instance for method chaining.
*/
public function importFromUrl($url, $username = null, $password = null) // NOSONAR
{
$parts = parse_url($url);
if (!$parts) {
throw new InvalidArgumentException("Invalid database URL");
}
// Basic connection parts
if (isset($parts['scheme'])) {
$this->setDriver($parts['scheme']);
}
if ($this->getDriver() === 'sqlite' && isset($parts['path'])) {
$this->setDatabaseFilePath($parts['path']);
return;
}
if (isset($parts['host'])) {
$this->setHost($parts['host']);
}
if (isset($parts['port'])) {
$port = (int) $parts['port'];
if ($port < 0) {
throw new InvalidArgumentException("Invalid port: must be a non-negative integer");
}
$this->setPort($port);
}
// Username and password
if ($username !== null) {
$this->setUsername($username);
} elseif (isset($parts['user'])) {
$this->setUsername($parts['user']);
}
if ($password !== null) {
$this->setPassword($password);
} elseif (isset($parts['pass'])) {
$this->setPassword($parts['pass']);
}
if (isset($parts['path'])) {
$dbName = ltrim($parts['path'], '/');
$this->setDatabaseName($dbName);
}
// Optional query parameters
if (isset($parts['query'])) {
parse_str($parts['query'], $query);
if (isset($query['schema'])) {
$this->setDatabaseSchema($query['schema']);
}
if (isset($query['timezone'])) {
$this->setTimeZone($query['timezone']);
}
if (isset($query['charset'])) {
$this->setCharset($query['charset']);
}
}
return $this;
}
}