From b2e0750d1eeadb16ceb0b8463ef68403fc24a1cd Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 5 Apr 2024 09:52:50 +0300 Subject: [PATCH] init Address class --- README.md | 21 ++++++ src/Address.php | 168 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 src/Address.php diff --git a/README.md b/README.md index 3d93d2c..5bce5aa 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,27 @@ var_dump( ); ``` +### Address + +Includes useful methods to work with network addresses + +``` +$address = new \Yggverse\Net\Address( + 'http://yo.ygg' +); + +var_dump( + $address->getScheme() // Just scheme substring +); + +var_dump( + $address->absolute( + './some/uri' + ) // return http://yo.ygg/some/uri +); +``` + ## Integrations * [Network API with native Yggdrasil/IPv6 support](https://github.com/YGGverse/web-api) +* [Yo! Search Crawler for different networks](https://github.com/YGGverse/Yo) diff --git a/src/Address.php b/src/Address.php new file mode 100644 index 0000000..065f9a4 --- /dev/null +++ b/src/Address.php @@ -0,0 +1,168 @@ +setScheme( + (string) $scheme + ); + } + + if ($user = parse_url($address, PHP_URL_USER)) + { + $this->setUser( + (string) $user + ); + } + + if ($pass = parse_url($address, PHP_URL_PASS)) + { + $this->setPath( + (string) $pass + ); + } + + if ($host = parse_url($address, PHP_URL_HOST)) + { + $this->setHost( + (string) $host + ); + } + + if ($port = parse_url($address, PHP_URL_PORT)) + { + $this->setPort( + (int) $port + ); + } + + if ($path = parse_url($address, PHP_URL_PATH)) + { + $this->setPath( + (string) $path + ); + } + + if ($query = parse_url($address, PHP_URL_QUERY)) + { + $this->setQuery( + (string) $query + ); + } + } + } + + public function isAbsolute(): bool + { + return ($this->_scheme && $this->_host); + } + + public function isRelative(): bool + { + return !$this->isAbsolute(); + } + + public function getScheme(): string + { + return $this->_scheme; + } + + public function setScheme(string $value): void + { + $this->_scheme = $value; + } + + public function getHost(): string + { + return $this->_host; + } + + public function setHost(string $value): void + { + $this->_host = $value; + } + + public function getUser(): string + { + return $this->_user; + } + + public function setUser(string $value): void + { + $this->_user = $value; + } + + public function getPass(): string + { + return $this->_pass; + } + + public function setPass(string $value): void + { + $this->_pass = $value; + } + + public function getPort(): int + { + return $this->_port; + } + + public function setPort(int $value): void + { + $this->_port = $value; + } + + public function getPath(): string + { + return $this->_path; + } + + public function setPath(string $value): void + { + $this->_path = $value; + + $this->_dirs = explode( + '/', + $value + ); + } + + public function getDirs(): array + { + return $this->_dirs; + } + + public function getQuery(): string + { + return $this->_query; + } + + public function setQuery(string $value): void + { + $this->_query = $value; + } + + public function absolute(string $address): string + { + // @TODO + } +} \ No newline at end of file