make performance optimization, require parser interface

This commit is contained in:
yggverse 2024-06-24 21:53:56 +03:00
parent 588a8750fb
commit f00f23aaf2
5 changed files with 148 additions and 112 deletions

32
src/Parser/Code.php Normal file → Executable file
View file

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Yggverse\Gemtext\Parser;
class Code
class Code implements \Yggverse\Gemtext\Interface\Parser
{
public static function match(
string $line,
@ -37,13 +37,18 @@ class Code
}
public static function getAlt(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
self::match(
$line,
$matches
);
}
if (isset($matches['alt']))
{
$alt = trim(
@ -55,22 +60,25 @@ class Code
return $alt;
}
}
}
return null;
}
public static function isInline(
string $line
string $line,
array $matches = []
): bool
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
return isset($matches['close']);
self::match(
$line,
$matches
);
}
return false;
return isset(
$matches['close']
);
}
}

30
src/Parser/Header.php Normal file → Executable file
View file

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Yggverse\Gemtext\Parser;
class Header
class Header implements \Yggverse\Gemtext\Interface\Parser
{
public static function match(
string $line,
@ -19,32 +19,41 @@ class Header
}
public static function getLevel(
string $line
string $line,
array $matches = []
): ?int
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
self::match(
$line,
$matches
);
}
if (isset($matches['level']))
{
return (int) strlen(
$matches['level']
);
}
}
return null;
}
public static function getText(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
self::match(
$line,
$matches
);
}
if (isset($matches['text']))
{
$text = trim(
@ -56,7 +65,6 @@ class Header
return $text;
}
}
}
return null;
}

44
src/Parser/Link.php Normal file → Executable file
View file

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Yggverse\Gemtext\Parser;
class Link
class Link implements \Yggverse\Gemtext\Interface\Parser
{
public static function match(
string $line,
@ -19,13 +19,18 @@ class Link
}
public static function getAddress(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
self::match(
$line,
$matches
);
}
if (isset($matches['address']))
{
$address = trim(
@ -37,19 +42,23 @@ class Link
return $address;
}
}
}
return null;
}
public static function getDate(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
self::match(
$line,
$matches
);
}
if (isset($matches['date']))
{
$date = trim(
@ -61,19 +70,23 @@ class Link
return $date;
}
}
}
return null;
}
public static function getAlt(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
self::match(
$line,
$matches
);
}
if (isset($matches['alt']))
{
$alt = trim(
@ -85,7 +98,6 @@ class Link
return $alt;
}
}
}
return null;
}

16
src/Parser/Listing.php Normal file → Executable file
View file

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Yggverse\Gemtext\Parser;
class Listing
class Listing implements \Yggverse\Gemtext\Interface\Parser
{
public static function match(
string $line,
@ -19,13 +19,18 @@ class Listing
}
public static function getItem(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
self::match(
$line,
$matches
);
}
if (isset($matches['item']))
{
$item = trim(
@ -37,7 +42,6 @@ class Listing
return $item;
}
}
}
return null;
}

16
src/Parser/Quote.php Normal file → Executable file
View file

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Yggverse\Gemtext\Parser;
class Quote
class Quote implements \Yggverse\Gemtext\Interface\Parser
{
public static function match(
string $line,
@ -19,13 +19,18 @@ class Quote
}
public static function getText(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
self::match(
$line,
$matches
);
}
if (isset($matches['text']))
{
$text = trim(
@ -37,7 +42,6 @@ class Quote
return $text;
}
}
}
return null;
}