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

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

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Yggverse\Gemtext\Parser; namespace Yggverse\Gemtext\Parser;
class Code class Code implements \Yggverse\Gemtext\Interface\Parser
{ {
public static function match( public static function match(
string $line, string $line,
@ -37,23 +37,27 @@ class Code
} }
public static function getAlt( public static function getAlt(
string $line string $line,
array $matches = []
): ?string ): ?string
{ {
$matches = []; if (!$matches)
if (self::match($line, $matches))
{ {
if (isset($matches['alt'])) self::match(
{ $line,
$alt = trim( $matches
$matches['alt'] );
); }
if ($alt) if (isset($matches['alt']))
{ {
return $alt; $alt = trim(
} $matches['alt']
);
if ($alt)
{
return $alt;
} }
} }
@ -61,16 +65,20 @@ class Code
} }
public static function isInline( public static function isInline(
string $line string $line,
array $matches = []
): bool ): bool
{ {
$matches = []; if (!$matches)
if (self::match($line, $matches))
{ {
return isset($matches['close']); self::match(
$line,
$matches
);
} }
return false; return isset(
$matches['close']
);
} }
} }

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

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

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

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Yggverse\Gemtext\Parser; namespace Yggverse\Gemtext\Parser;
class Link class Link implements \Yggverse\Gemtext\Interface\Parser
{ {
public static function match( public static function match(
string $line, string $line,
@ -19,23 +19,27 @@ class Link
} }
public static function getAddress( public static function getAddress(
string $line string $line,
array $matches = []
): ?string ): ?string
{ {
$matches = []; if (!$matches)
if (self::match($line, $matches))
{ {
if (isset($matches['address'])) self::match(
{ $line,
$address = trim( $matches
$matches['address'] );
); }
if ($address) if (isset($matches['address']))
{ {
return $address; $address = trim(
} $matches['address']
);
if ($address)
{
return $address;
} }
} }
@ -43,23 +47,27 @@ class Link
} }
public static function getDate( public static function getDate(
string $line string $line,
array $matches = []
): ?string ): ?string
{ {
$matches = []; if (!$matches)
if (self::match($line, $matches))
{ {
if (isset($matches['date'])) self::match(
{ $line,
$date = trim( $matches
$matches['date'] );
); }
if ($date) if (isset($matches['date']))
{ {
return $date; $date = trim(
} $matches['date']
);
if ($date)
{
return $date;
} }
} }
@ -67,23 +75,27 @@ class Link
} }
public static function getAlt( public static function getAlt(
string $line string $line,
array $matches = []
): ?string ): ?string
{ {
$matches = []; if (!$matches)
if (self::match($line, $matches))
{ {
if (isset($matches['alt'])) self::match(
{ $line,
$alt = trim( $matches
$matches['alt'] );
); }
if ($alt) if (isset($matches['alt']))
{ {
return $alt; $alt = trim(
} $matches['alt']
);
if ($alt)
{
return $alt;
} }
} }

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

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

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

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