Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Doctrine\DBAL\Driver\IBMDB2;
6
 
7
use function implode;
8
use function sprintf;
9
use function strpos;
10
 
11
/**
12
 * IBM DB2 DSN
13
 */
14
final class DataSourceName
15
{
16
    private string $string;
17
 
18
    private function __construct(string $string)
19
    {
20
        $this->string = $string;
21
    }
22
 
23
    public function toString(): string
24
    {
25
        return $this->string;
26
    }
27
 
28
    /**
29
     * Creates the object from an array representation
30
     *
31
     * @param array<string,mixed> $params
32
     */
33
    public static function fromArray(array $params): self
34
    {
35
        $chunks = [];
36
 
37
        foreach ($params as $key => $value) {
38
            $chunks[] = sprintf('%s=%s', $key, $value);
39
        }
40
 
41
        return new self(implode(';', $chunks));
42
    }
43
 
44
    /**
45
     * Creates the object from the given DBAL connection parameters.
46
     *
47
     * @param array<string,mixed> $params
48
     */
49
    public static function fromConnectionParameters(array $params): self
50
    {
51
        if (isset($params['dbname']) && strpos($params['dbname'], '=') !== false) {
52
            return new self($params['dbname']);
53
        }
54
 
55
        $dsnParams = [];
56
 
57
        foreach (
58
            [
59
                'host'     => 'HOSTNAME',
60
                'port'     => 'PORT',
61
                'protocol' => 'PROTOCOL',
62
                'dbname'   => 'DATABASE',
63
                'user'     => 'UID',
64
                'password' => 'PWD',
65
            ] as $dbalParam => $dsnParam
66
        ) {
67
            if (! isset($params[$dbalParam])) {
68
                continue;
69
            }
70
 
71
            $dsnParams[$dsnParam] = $params[$dbalParam];
72
        }
73
 
74
        return self::fromArray($dsnParams);
75
    }
76
}