Compare commits

...

8 commits
0.1.0 ... main

Author SHA1 Message Date
ghost
74de022cd5 fix namespace 2023-10-09 04:52:01 +03:00
ghost
26111fce28 remove neitanod/forceutf8 dependency 2023-09-20 15:18:16 +03:00
ghost
47c7d13697 add neitanod/forceutf8 dependency 2023-09-20 14:28:52 +03:00
ghost
81f019be7a fix 0x1220 multihash prefix for v2 btmh part 2023-09-05 19:15:29 +03:00
ghost
c32a3b5f7d add trim to filterInfoHash method 2023-09-04 01:38:55 +03:00
ghost
6c73a68024 add info hash v2 support 2023-09-04 00:16:37 +03:00
ghost
80c94381ab add magnet links parser 2023-08-26 21:28:40 +03:00
ghost
5e1003cca2 add URN parser 2023-08-26 21:28:13 +03:00
4 changed files with 160 additions and 2 deletions

119
src/Magnet.php Normal file
View 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;
}
}

View file

@ -1,6 +1,6 @@
<?php
namespace YGGverse\Parser;
namespace Yggverse\Parser;
class Url {

39
src/Urn.php Normal file
View 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;
}
}

View file

@ -1,6 +1,6 @@
<?php
namespace YGGverse\Parser\Tests;
namespace Yggverse\Parser\Tests;
use PHPUnit\Framework\TestCase;