mirror of
https://github.com/YGGverse/parser-php.git
synced 2026-03-31 17:25:28 +00:00
Compare commits
8 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74de022cd5 | ||
|
|
26111fce28 | ||
|
|
47c7d13697 | ||
|
|
81f019be7a | ||
|
|
c32a3b5f7d | ||
|
|
6c73a68024 | ||
|
|
80c94381ab | ||
|
|
5e1003cca2 |
4 changed files with 160 additions and 2 deletions
119
src/Magnet.php
Normal file
119
src/Magnet.php
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
namespace Yggverse\Parser;
|
||||
|
||||
class Magnet {
|
||||
|
||||
public static function is(string $link) : bool
|
||||
{
|
||||
return ('magnet' == parse_url($link, PHP_URL_SCHEME));
|
||||
}
|
||||
|
||||
public static function isXTv1(string $xt) : bool
|
||||
{
|
||||
return ('urn' == parse_url($xt, PHP_URL_SCHEME) && false !== strpos($xt, ':btih:'));
|
||||
}
|
||||
|
||||
public static function isXTv2(string $xt) : bool
|
||||
{
|
||||
return ('urn' == parse_url($xt, PHP_URL_SCHEME) && false !== strpos($xt, ':btmh:1220'));
|
||||
}
|
||||
|
||||
public static function filterInfoHash(string $value) : string
|
||||
{
|
||||
return trim(
|
||||
str_replace(
|
||||
[
|
||||
'urn:',
|
||||
'btih:',
|
||||
'btmh:1220',
|
||||
],
|
||||
false,
|
||||
$value
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function parse(string $link) : mixed
|
||||
{
|
||||
$result =
|
||||
[
|
||||
'dn' => null,
|
||||
'xl' => 0,
|
||||
'xt' => [],
|
||||
'tr' => [],
|
||||
'ws' => [],
|
||||
'as' => [],
|
||||
'xs' => [],
|
||||
'kt' => [],
|
||||
'mt' => [],
|
||||
'so' => [],
|
||||
'x.pe' => [],
|
||||
];
|
||||
|
||||
if (!self::is($link))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$link = urldecode($link);
|
||||
|
||||
$link = str_replace(
|
||||
[
|
||||
'magnet:',
|
||||
'?',
|
||||
'xt=',
|
||||
'tr=',
|
||||
'ws=',
|
||||
'as=',
|
||||
'xs=',
|
||||
'mt=',
|
||||
'x.pe=',
|
||||
],
|
||||
[
|
||||
false,
|
||||
false,
|
||||
'xt[]=',
|
||||
'tr[]=',
|
||||
'ws[]=',
|
||||
'as[]=',
|
||||
'xs[]=',
|
||||
'mt[]=',
|
||||
'x.pe[]=',
|
||||
],
|
||||
$link
|
||||
);
|
||||
|
||||
parse_str($link, $attributes);
|
||||
|
||||
switch (true)
|
||||
{
|
||||
case empty($attributes['xt']):
|
||||
case empty($attributes['tr']):
|
||||
// ...
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ((array) $attributes as $key => $value)
|
||||
{
|
||||
|
||||
switch ($key)
|
||||
{
|
||||
case 'kt':
|
||||
|
||||
foreach ((array) explode(' ', $value) as $keyword)
|
||||
{
|
||||
$result[$key][] = trim($keyword);
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
|
||||
$result[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return (object) $result;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace YGGverse\Parser;
|
||||
namespace Yggverse\Parser;
|
||||
|
||||
class Url {
|
||||
|
||||
|
|
|
|||
39
src/Urn.php
Normal file
39
src/Urn.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace Yggverse\Parser;
|
||||
|
||||
class Urn {
|
||||
|
||||
public static function is(string $urn) : bool
|
||||
{
|
||||
return ('urn' == parse_url($urn, PHP_URL_SCHEME));
|
||||
}
|
||||
|
||||
public static function parse(string $urn) : mixed
|
||||
{
|
||||
if (!self::is($urn))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($part = explode(':', $urn))
|
||||
{
|
||||
if (empty($part[0]) || empty($part[1]) || empty($part[2]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
unset($part[0]);
|
||||
|
||||
$result =
|
||||
[
|
||||
'urn' => implode(':', $part),
|
||||
'parts' => $part,
|
||||
];
|
||||
|
||||
return (object) $result;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace YGGverse\Parser\Tests;
|
||||
namespace Yggverse\Parser\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue