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; 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,13 +37,18 @@ 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))
{ {
self::match(
$line,
$matches
);
}
if (isset($matches['alt'])) if (isset($matches['alt']))
{ {
$alt = trim( $alt = trim(
@ -55,22 +60,25 @@ class Code
return $alt; return $alt;
} }
} }
}
return null; return null;
} }
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']
);
} }
} }

30
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,32 +19,41 @@ 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))
{ {
self::match(
$line,
$matches
);
}
if (isset($matches['level'])) if (isset($matches['level']))
{ {
return (int) strlen( return (int) strlen(
$matches['level'] $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))
{ {
self::match(
$line,
$matches
);
}
if (isset($matches['text'])) if (isset($matches['text']))
{ {
$text = trim( $text = trim(
@ -56,7 +65,6 @@ class Header
return $text; return $text;
} }
} }
}
return null; 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; 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,13 +19,18 @@ 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))
{ {
self::match(
$line,
$matches
);
}
if (isset($matches['address'])) if (isset($matches['address']))
{ {
$address = trim( $address = trim(
@ -37,19 +42,23 @@ class Link
return $address; return $address;
} }
} }
}
return null; return null;
} }
public static function getDate( public static function getDate(
string $line string $line,
array $matches = []
): ?string ): ?string
{ {
$matches = []; if (!$matches)
if (self::match($line, $matches))
{ {
self::match(
$line,
$matches
);
}
if (isset($matches['date'])) if (isset($matches['date']))
{ {
$date = trim( $date = trim(
@ -61,19 +70,23 @@ class Link
return $date; return $date;
} }
} }
}
return null; return null;
} }
public static function getAlt( public static function getAlt(
string $line string $line,
array $matches = []
): ?string ): ?string
{ {
$matches = []; if (!$matches)
if (self::match($line, $matches))
{ {
self::match(
$line,
$matches
);
}
if (isset($matches['alt'])) if (isset($matches['alt']))
{ {
$alt = trim( $alt = trim(
@ -85,7 +98,6 @@ class Link
return $alt; return $alt;
} }
} }
}
return null; 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; 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,13 +19,18 @@ 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))
{ {
self::match(
$line,
$matches
);
}
if (isset($matches['item'])) if (isset($matches['item']))
{ {
$item = trim( $item = trim(
@ -37,7 +42,6 @@ class Listing
return $item; return $item;
} }
} }
}
return null; 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; 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,13 +19,18 @@ 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))
{ {
self::match(
$line,
$matches
);
}
if (isset($matches['text'])) if (isset($matches['text']))
{ {
$text = trim( $text = trim(
@ -37,7 +42,6 @@ class Quote
return $text; return $text;
} }
} }
}
return null; return null;
} }