1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Yep package. |
4
|
|
|
* Copyright (c) 2016 Martin Zeman (Zemistr) (http://www.zemistr.eu) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Yep\Dsn; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class PostgreSqlDsn |
11
|
|
|
* |
12
|
|
|
* @package Yep\Dsn |
13
|
|
|
* @author Martin Zeman (Zemistr) <[email protected]> |
14
|
|
|
* |
15
|
|
|
* PDO_PGSQL |
16
|
|
|
* pgsql:host=localhost;port=5432;dbname=testdb;user=bruce;password=mypass |
17
|
|
|
*/ |
18
|
|
|
class PostgreSqlDsn extends BaseDns { |
19
|
|
|
/** @var string */ |
20
|
|
|
protected $user; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $password; |
24
|
|
|
|
25
|
4 |
|
public function __construct(string $dbName, string $host = 'localhost', int $port = 5432, string $user = null, string $password = null) { |
26
|
4 |
|
$this->setDbName($dbName); |
27
|
4 |
|
$this->setHost($host); |
28
|
4 |
|
$this->setPort($port); |
29
|
4 |
|
$this->setUser($user); |
30
|
4 |
|
$this->setPassword($password); |
31
|
4 |
|
} |
32
|
|
|
|
33
|
1 |
|
public function toString() : string { |
34
|
1 |
|
$dsn = 'pgsql:host=' . $this->getHost() . ';dbname=' . $this->getDbName() . ';port=' . $this->getPort(); |
35
|
|
|
|
36
|
1 |
|
if ($this->hasUser()) { |
37
|
1 |
|
$dsn .= ';user=' . $this->getUser(); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
if ($this->hasPassword()) { |
41
|
1 |
|
$dsn .= ';password=' . $this->getPassword(); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
return $dsn; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string|null $user |
49
|
|
|
* @return PostgreSqlDsn |
|
|
|
|
50
|
|
|
*/ |
51
|
4 |
|
public function setUser(string $user = null) : self { |
52
|
4 |
|
$this->user = $user; |
53
|
|
|
|
54
|
4 |
|
if ($user !== null) { |
55
|
1 |
|
$this->user = trim($user); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
1 |
|
public function hasUser() : bool { |
65
|
1 |
|
return $this->user !== null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
1 |
|
public function getUser() : string { |
72
|
1 |
|
return (string)$this->user; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string|null $password |
77
|
|
|
* @return PostgreSqlDsn |
|
|
|
|
78
|
|
|
*/ |
79
|
4 |
|
public function setPassword(string $password = null) : self { |
80
|
4 |
|
$this->password = $password; |
81
|
|
|
|
82
|
4 |
|
if ($this->password !== null) { |
83
|
1 |
|
$this->password = trim($password); |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
1 |
|
public function hasPassword() : bool { |
93
|
1 |
|
return $this->password !== null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
1 |
|
public function getPassword() : string { |
100
|
1 |
|
return (string)$this->password; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.