initial commit

This commit is contained in:
ghost 2023-08-07 12:35:57 +03:00
parent 932676e236
commit f5b1beb51e
4 changed files with 117 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
/.vscode/
/vendor/
composer.lock

17
composer.json Normal file
View file

@ -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/"
}
}
}

84
src/Url.php Normal file
View file

@ -0,0 +1,84 @@
<?php
namespace YGGverse\Parser;
class Url {
public static function is(string $url) : bool {
return filter_var($url, FILTER_VALIDATE_URL);
}
public static function parse(string $url) : mixed {
$result = (object)
[
'host' => (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;
}
}

12
tests/UrlTest.php Normal file
View file

@ -0,0 +1,12 @@
<?php
namespace YGGverse\Parser\Tests;
use PHPUnit\Framework\TestCase;
class UrlTest extends TestCase
{
public function testIs(){}
public function testParse(){}
}