From 80c94381abaa7dc159497f9229fe2455432d9886 Mon Sep 17 00:00:00 2001 From: ghost Date: Sat, 26 Aug 2023 21:28:40 +0300 Subject: [PATCH] add magnet links parser --- src/Magnet.php | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/Magnet.php diff --git a/src/Magnet.php b/src/Magnet.php new file mode 100644 index 0000000..949c887 --- /dev/null +++ b/src/Magnet.php @@ -0,0 +1,92 @@ + null, + 'dn' => null, + 'xl' => 0, + 'tr' => [], + 'ws' => [], + 'as' => [], + 'xs' => [], + 'kt' => [], + 'mt' => [], + 'so' => [], + 'x.pe' => [], + ]; + + if (!self::is($link)) + { + return false; + } + + $link = urldecode($link); + + $link = str_replace( + [ + 'magnet:', + '?', + 'tr=', + 'ws=', + 'as=', + 'xs=', + 'mt=', + 'x.pe=', + ], + [ + false, + false, + '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; + } +} \ No newline at end of file