Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
|
100.00% |
4 / 4 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| PdoConfig | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
|
100.00% |
4 / 4 |
|
100.00% |
6 / 6 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
n/a |
0 / 0 |
n/a |
0 / 0 |
|
100.00% |
1 / 1 |
1 | |||||
| credentials | |
100.00% |
1 / 1 |
n/a |
0 / 0 |
n/a |
0 / 0 |
|
100.00% |
1 / 1 |
1 | |||||
| options | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| option | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fetch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| effectiveOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celemas\Quma; |
| 6 | |
| 7 | use PDO; |
| 8 | |
| 9 | /** @api */ |
| 10 | final class PdoConfig |
| 11 | { |
| 12 | private const array DEFAULT_OPTIONS = [ |
| 13 | PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL, |
| 14 | PDO::ATTR_EMULATE_PREPARES => true, |
| 15 | PDO::ATTR_CASE => PDO::CASE_NATURAL, |
| 16 | ]; |
| 17 | private const array REQUIRED_OPTIONS = [ |
| 18 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
| 19 | ]; |
| 20 | |
| 21 | /** @param array<array-key, mixed> $options */ |
| 22 | public function __construct( |
| 23 | public readonly ?string $username = null, |
| 24 | #[\SensitiveParameter] |
| 25 | public readonly ?string $password = null, |
| 26 | public readonly array $options = [], |
| 27 | public readonly int $fetchMode = PDO::FETCH_ASSOC, |
| 28 | ) {} |
| 29 | |
| 30 | public function credentials( |
| 31 | string $username, |
| 32 | #[\SensitiveParameter] |
| 33 | ?string $password = null, |
| 34 | ): self { |
| 35 | return new self($username, $password, $this->options, $this->fetchMode); |
| 36 | } |
| 37 | |
| 38 | /** @param array<array-key, mixed> $options */ |
| 39 | public function options(array $options): self |
| 40 | { |
| 41 | return new self($this->username, $this->password, $options, $this->fetchMode); |
| 42 | } |
| 43 | |
| 44 | public function option(int $attribute, mixed $value): self |
| 45 | { |
| 46 | /** @var array<array-key, mixed> $options */ |
| 47 | $options = array_replace($this->options, [$attribute => $value]); |
| 48 | |
| 49 | return new self($this->username, $this->password, $options, $this->fetchMode); |
| 50 | } |
| 51 | |
| 52 | public function fetch(int $fetchMode): self |
| 53 | { |
| 54 | return new self($this->username, $this->password, $this->options, $fetchMode); |
| 55 | } |
| 56 | |
| 57 | /** @return array<array-key, mixed> */ |
| 58 | public function effectiveOptions(): array |
| 59 | { |
| 60 | return array_replace(self::DEFAULT_OPTIONS, $this->options, self::REQUIRED_OPTIONS); |
| 61 | } |
| 62 | } |