mirror of
https://github.com/YGGverse/gemtext-php.git
synced 2026-03-31 09:45:33 +00:00
implement link parser class
This commit is contained in:
parent
e4303e6a98
commit
dc581d6071
1 changed files with 97 additions and 0 deletions
97
src/Parser/Line/Link.php
Normal file
97
src/Parser/Line/Link.php
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yggverse\Gemtext\Parser\Line;
|
||||
|
||||
class Link
|
||||
{
|
||||
public static function match(
|
||||
\Yggverse\Gemtext\Entity\Line $line,
|
||||
array &$matches = []
|
||||
): bool
|
||||
{
|
||||
if ($line->isEscaped())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) preg_match(
|
||||
'/^=>\s*(?<address>[^\s]+)(\s(?<date>\d{4}-\d{2}-\d{2}))?(\s(?<alt>.+))?$/m',
|
||||
$line->getData(),
|
||||
$matches
|
||||
);
|
||||
}
|
||||
|
||||
public static function getAddress(
|
||||
\Yggverse\Gemtext\Entity\Line $line
|
||||
): ?string
|
||||
{
|
||||
$matches = [];
|
||||
|
||||
if (self::match($line, $matches))
|
||||
{
|
||||
if (isset($matches['address']))
|
||||
{
|
||||
$address = trim(
|
||||
$matches['address']
|
||||
);
|
||||
|
||||
if ($address)
|
||||
{
|
||||
return $address;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getDate(
|
||||
\Yggverse\Gemtext\Entity\Line $line
|
||||
): ?string
|
||||
{
|
||||
$matches = [];
|
||||
|
||||
if (self::match($line, $matches))
|
||||
{
|
||||
if (isset($matches['date']))
|
||||
{
|
||||
$date = trim(
|
||||
$matches['date']
|
||||
);
|
||||
|
||||
if ($date)
|
||||
{
|
||||
return $date;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getAlt(
|
||||
\Yggverse\Gemtext\Entity\Line $line
|
||||
): ?string
|
||||
{
|
||||
$matches = [];
|
||||
|
||||
if (self::match($line, $matches))
|
||||
{
|
||||
if (isset($matches['alt']))
|
||||
{
|
||||
$alt = trim(
|
||||
$matches['alt']
|
||||
);
|
||||
|
||||
if ($alt)
|
||||
{
|
||||
return $alt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue