1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle Database package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Config; |
13
|
|
|
|
14
|
|
|
abstract class ConnectionConfig |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array<non-empty-string> |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
protected array $nonPrintableOptions = [ |
20
|
|
|
// Postgres and MySQL |
21
|
|
|
'password', |
22
|
|
|
// IBM, ODBC and DB2 |
23
|
|
|
'PWD', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param non-empty-string|null $user |
|
|
|
|
28
|
|
|
* @param non-empty-string|null $password |
29
|
|
|
*/ |
30
|
60 |
|
public function __construct( |
31
|
|
|
public ?string $user = null, |
32
|
|
|
public ?string $password = null, |
33
|
|
|
) { |
34
|
60 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return non-empty-string|null |
|
|
|
|
38
|
|
|
*/ |
39
|
62 |
|
public function getUsername(): ?string |
40
|
|
|
{ |
41
|
62 |
|
return $this->user; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return non-empty-string|null |
|
|
|
|
46
|
|
|
*/ |
47
|
62 |
|
public function getPassword(): ?string |
48
|
|
|
{ |
49
|
62 |
|
return $this->password; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param bool $secure |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
protected function toArray(bool $secure = true): array |
58
|
|
|
{ |
59
|
|
|
$options = \get_object_vars($this); |
60
|
|
|
|
61
|
|
|
foreach ($options as $key => $value) { |
62
|
|
|
if ($secure && \in_array($key, $this->nonPrintableOptions, true)) { |
63
|
|
|
$value = '<hidden>'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$options[$key] = $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $options; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function __debugInfo(): array |
76
|
|
|
{ |
77
|
|
|
return $this->toArray(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @throws \ReflectionException |
82
|
|
|
*/ |
83
|
|
|
public static function __set_state(array $properties): static |
84
|
|
|
{ |
85
|
|
|
$ref = new \ReflectionClass(static::class); |
86
|
|
|
|
87
|
|
|
$arguments = []; |
88
|
|
|
foreach ($ref->getConstructor()?->getParameters() ?? [] as $parameter) { |
89
|
|
|
$arguments[$parameter->getName()] = \array_key_exists($parameter->getName(), $properties) |
90
|
|
|
? $properties[$parameter->getName()] |
91
|
|
|
: $parameter->getDefaultValue(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return new static(...$arguments); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|