implement Quote support

This commit is contained in:
yggverse 2024-06-24 03:35:08 +03:00
parent 11925c5044
commit fd71028615
3 changed files with 99 additions and 0 deletions

View file

@ -60,6 +60,17 @@ class Document
break;
// Quote
case Parser\Quote::match($line):
$this->_entities[] = new Entity\Quote(
Parser\Quote::getText(
$line
)
);
break;
// Plain
default:

44
src/Entity/Quote.php Normal file
View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Yggverse\Gemtext\Entity;
class Quote
{
public const TAG = '>';
private ?string $_text;
public function __construct(
?string $text = null
) {
$this->setText(
$text
);
}
public function setText(
?string $text
): void
{
if ($text)
{
$text = trim(
$text
);
}
$this->_text = $text;
}
public function Text(): ?string
{
return $this->_text;
}
public function toString(): string
{
return self::TAG . ' ' . $this->_text;
}
}

44
src/Parser/Quote.php Normal file
View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Yggverse\Gemtext\Parser;
class Quote
{
public static function match(
string $line,
array &$matches = []
): bool
{
return (bool) preg_match(
'/^>\s*(?<text>.*)$/m',
$line,
$matches
);
}
public static function getText(
string $line
): ?string
{
$matches = [];
if (self::match($line, $matches))
{
if (isset($matches['text']))
{
$text = trim(
$matches['text']
);
if ($text)
{
return $text;
}
}
}
return null;
}
}