TalismanFR /
rosselhozbank-api
| 1 | <?php |
||
| 2 | |||
| 3 | |||
| 4 | namespace talismanfr\rosselhozbank\shared; |
||
| 5 | |||
| 6 | |||
| 7 | class UrlValue |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var string |
||
| 11 | */ |
||
| 12 | private $url; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Constructor. |
||
| 16 | * |
||
| 17 | * @param string $url The URL. |
||
| 18 | */ |
||
| 19 | public function __construct($url) |
||
| 20 | { |
||
| 21 | if (false === filter_var($url, FILTER_VALIDATE_URL)) { |
||
| 22 | throw new \InvalidArgumentException('Invalid URL.'); |
||
| 23 | } |
||
| 24 | |||
| 25 | $this->url = (string) $url; |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Parses an URL into an object. |
||
| 30 | * |
||
| 31 | * @param string $url The URL. |
||
| 32 | * |
||
| 33 | * @return UrlValue |
||
| 34 | */ |
||
| 35 | public static function fromString($url) |
||
| 36 | { |
||
| 37 | return new self($url); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Returns the scheme (e.g. http). |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | public function getScheme() |
||
| 46 | { |
||
| 47 | return $this->getComponent(PHP_URL_SCHEME); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Returns the user. |
||
| 52 | * |
||
| 53 | * @return string|null |
||
| 54 | */ |
||
| 55 | public function getUser() |
||
| 56 | { |
||
| 57 | return $this->getComponent(PHP_URL_USER); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Returns the password. |
||
| 62 | * |
||
| 63 | * @return string|null |
||
| 64 | */ |
||
| 65 | public function getPassword() |
||
| 66 | { |
||
| 67 | return $this->getComponent(PHP_URL_PASS); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns the host. |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function getHost() |
||
| 76 | { |
||
| 77 | return $this->getComponent(PHP_URL_HOST); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Returns the port. |
||
| 82 | * |
||
| 83 | * @return string|null |
||
| 84 | */ |
||
| 85 | public function getPort() |
||
| 86 | { |
||
| 87 | return $this->getComponent(PHP_URL_PORT); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns the path. |
||
| 92 | * |
||
| 93 | * @return string|null |
||
| 94 | */ |
||
| 95 | public function getPath() |
||
| 96 | { |
||
| 97 | return $this->getComponent(PHP_URL_PATH); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns the query. |
||
| 102 | * |
||
| 103 | * @return string|null |
||
| 104 | */ |
||
| 105 | public function getQuery() |
||
| 106 | { |
||
| 107 | return $this->getComponent(PHP_URL_QUERY); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns the fragment. |
||
| 112 | * |
||
| 113 | * @return string|null |
||
| 114 | */ |
||
| 115 | public function getFragment() |
||
| 116 | { |
||
| 117 | return $this->getComponent(PHP_URL_FRAGMENT); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | public function equals($other) |
||
| 124 | { |
||
| 125 | if (!$other instanceof self) { |
||
| 126 | return false; |
||
| 127 | } |
||
| 128 | |||
| 129 | return (string) $this === (string) $other; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns the URL as string. |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function __toString() |
||
| 138 | { |
||
| 139 | return $this->url; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Extracts a component from the URL. |
||
| 144 | * |
||
| 145 | * @param string $component The component. |
||
| 146 | * |
||
| 147 | * @return string|null |
||
| 148 | */ |
||
| 149 | private function getComponent($component) |
||
| 150 | { |
||
| 151 | return parse_url($this->url, $component); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 152 | } |
||
| 153 | } |