mirror of
https://github.com/YGGverse/parser-php.git
synced 2026-03-31 09:15:29 +00:00
initial commit
This commit is contained in:
parent
932676e236
commit
f5b1beb51e
4 changed files with 117 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
/.vscode/
|
||||
/vendor/
|
||||
|
||||
composer.lock
|
||||
17
composer.json
Normal file
17
composer.json
Normal 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
84
src/Url.php
Normal 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
12
tests/UrlTest.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace YGGverse\Parser\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class UrlTest extends TestCase
|
||||
{
|
||||
public function testIs(){}
|
||||
|
||||
public function testParse(){}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue