1<?php 2 3namespace Nyholm\Dsn\Configuration; 4 5/** 6 * Base DSN object. 7 * 8 * Example: 9 * - null:// 10 * - redis:?host[h1]&host[h2]&host[/foo:] 11 * 12 * @author Tobias Nyholm <tobias.nyholm@gmail.com> 13 */ 14class Dsn 15{ 16 /** 17 * @var string|null 18 */ 19 private $scheme; 20 21 /** 22 * @var array 23 */ 24 private $parameters = []; 25 26 public function __construct(?string $scheme, array $parameters = []) 27 { 28 $this->scheme = $scheme; 29 $this->parameters = $parameters; 30 } 31 32 public function getScheme(): ?string 33 { 34 return $this->scheme; 35 } 36 37 /** 38 * @return static 39 */ 40 public function withScheme(?string $scheme) 41 { 42 $new = clone $this; 43 $new->scheme = $scheme; 44 45 return $new; 46 } 47 48 public function getParameters(): array 49 { 50 return $this->parameters; 51 } 52 53 /** 54 * @param mixed|null $default 55 * 56 * @return mixed 57 */ 58 public function getParameter(string $key, $default = null) 59 { 60 return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default; 61 } 62 63 /** 64 * @param mixed $value 65 * 66 * @return static 67 */ 68 public function withParameter(string $key, $value) 69 { 70 $new = clone $this; 71 $new->parameters[$key] = $value; 72 73 return $new; 74 } 75 76 /** 77 * @return static 78 */ 79 public function withoutParameter(string $key) 80 { 81 $new = clone $this; 82 unset($new->parameters[$key]); 83 84 return $new; 85 } 86 87 public function getHost(): ?string 88 { 89 return null; 90 } 91 92 public function getPort(): ?int 93 { 94 return null; 95 } 96 97 public function getPath(): ?string 98 { 99 return null; 100 } 101 102 public function getUser(): ?string 103 { 104 return null; 105 } 106 107 public function getPassword(): ?string 108 { 109 return null; 110 } 111 112 /** 113 * @var string 114 */ 115 public function __toString() 116 { 117 $parameters = $this->getParameters(); 118 $scheme = $this->getScheme(); 119 120 return 121 (empty($scheme) ? '' : $scheme.'://'). 122 (empty($parameters) ? '' : '?'.http_build_query($parameters)); 123 } 124} 125