From f5b1beb51e8557c4f718432d5276519869a2605d Mon Sep 17 00:00:00 2001 From: ghost Date: Mon, 7 Aug 2023 12:35:57 +0300 Subject: [PATCH] initial commit --- .gitignore | 4 +++ composer.json | 17 ++++++++++ src/Url.php | 84 +++++++++++++++++++++++++++++++++++++++++++++++ tests/UrlTest.php | 12 +++++++ 4 files changed, 117 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Url.php create mode 100644 tests/UrlTest.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0604025 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.vscode/ +/vendor/ + +composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..37db3f5 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "yggverse/parser", + "description": "Parser toolkit written on PHP", + "type": "library", + "require": { + "php": ">=8.1" + }, + "require-dev": { + "phpunit/phpunit": ">=10" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "Yggverse\\Parser\\": "src/" + } + } +} diff --git a/src/Url.php b/src/Url.php new file mode 100644 index 0000000..c4bcfd1 --- /dev/null +++ b/src/Url.php @@ -0,0 +1,84 @@ + (object) + [ + 'url' => null, + 'scheme' => null, + 'name' => null, + 'port' => null, + ], + 'page' => (object) + [ + 'url' => null, + 'uri' => null, + 'path' => null, + 'query' => null, + ] + ]; + + // Validate URL + if (!self::is($url)) { + + return false; + } + + // Parse host + if ($scheme = parse_url($url, PHP_URL_SCHEME)) { + + $result->host->url = $scheme . '://'; + $result->host->scheme = $scheme; + + } else { + + return false; + } + + if ($host = parse_url($url, PHP_URL_HOST)) { + + $result->host->url .= $host; + $result->host->name = $host; + + } else { + + return false; + } + + if ($port = parse_url($url, PHP_URL_PORT)) { + + $result->host->url .= ':' . $port; + $result->host->port = $port; + + // port is optional + } + + // Parse page + if ($path = parse_url($url, PHP_URL_PATH)) { + + $result->page->uri = $path; + $result->page->path = $path; + } + + if ($query = parse_url($url, PHP_URL_QUERY)) { + + $result->page->uri .= '?' . $query; + $result->page->query = '?' . $query; + } + + $result->page->url = $result->host->url . $result->page->uri; + + return $result; + } +} \ No newline at end of file diff --git a/tests/UrlTest.php b/tests/UrlTest.php new file mode 100644 index 0000000..19707f6 --- /dev/null +++ b/tests/UrlTest.php @@ -0,0 +1,12 @@ +