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;
class Code
class Code implements \Yggverse\Gemtext\Interface\Parser
{
public static function match(
string $line,
@ -37,23 +37,27 @@ class Code
}
public static function getAlt(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
if (isset($matches['alt']))
{
$alt = trim(
$matches['alt']
);
self::match(
$line,
$matches
);
}
if ($alt)
{
return $alt;
}
if (isset($matches['alt']))
{
$alt = trim(
$matches['alt']
);
if ($alt)
{
return $alt;
}
}
@ -61,16 +65,20 @@ class Code
}
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']
);
}
}

56
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,42 +19,50 @@ class Header
}
public static function getLevel(
string $line
string $line,
array $matches = []
): ?int
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
if (isset($matches['level']))
{
return (int) strlen(
$matches['level']
);
}
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)
{
if (isset($matches['text']))
{
$text = trim(
$matches['text']
);
self::match(
$line,
$matches
);
}
if ($text)
{
return $text;
}
if (isset($matches['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;
class Link
class Link implements \Yggverse\Gemtext\Interface\Parser
{
public static function match(
string $line,
@ -19,23 +19,27 @@ class Link
}
public static function getAddress(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
if (isset($matches['address']))
{
$address = trim(
$matches['address']
);
self::match(
$line,
$matches
);
}
if ($address)
{
return $address;
}
if (isset($matches['address']))
{
$address = trim(
$matches['address']
);
if ($address)
{
return $address;
}
}
@ -43,23 +47,27 @@ class Link
}
public static function getDate(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
if (isset($matches['date']))
{
$date = trim(
$matches['date']
);
self::match(
$line,
$matches
);
}
if ($date)
{
return $date;
}
if (isset($matches['date']))
{
$date = trim(
$matches['date']
);
if ($date)
{
return $date;
}
}
@ -67,23 +75,27 @@ class Link
}
public static function getAlt(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
if (isset($matches['alt']))
{
$alt = trim(
$matches['alt']
);
self::match(
$line,
$matches
);
}
if ($alt)
{
return $alt;
}
if (isset($matches['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;
class Listing
class Listing implements \Yggverse\Gemtext\Interface\Parser
{
public static function match(
string $line,
@ -19,23 +19,27 @@ class Listing
}
public static function getItem(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
if (isset($matches['item']))
{
$item = trim(
$matches['item']
);
self::match(
$line,
$matches
);
}
if ($item)
{
return $item;
}
if (isset($matches['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;
class Quote
class Quote implements \Yggverse\Gemtext\Interface\Parser
{
public static function match(
string $line,
@ -19,23 +19,27 @@ class Quote
}
public static function getText(
string $line
string $line,
array $matches = []
): ?string
{
$matches = [];
if (self::match($line, $matches))
if (!$matches)
{
if (isset($matches['text']))
{
$text = trim(
$matches['text']
);
self::match(
$line,
$matches
);
}
if ($text)
{
return $text;
}
if (isset($matches['text']))
{
$text = trim(
$matches['text']
);
if ($text)
{
return $text;
}
}