update Parser API, implement Document:getEntities(), Document:append() methods

This commit is contained in:
yggverse 2024-06-24 21:56:16 +03:00
parent f00f23aaf2
commit d5cd8c184d

52
src/Document.php Normal file → Executable file
View file

@ -15,71 +15,83 @@ class Document
) { ) {
foreach ((array) explode(PHP_EOL, $data) as $line) foreach ((array) explode(PHP_EOL, $data) as $line)
{ {
// Init match
$matches = [];
// Add entity // Add entity
switch (true) switch (true)
{ {
// Code // Code
case Parser\Code::match($line): case Parser\Code::match($line, $matches):
$this->_entity[] = new Entity\Code( $this->_entity[] = new Entity\Code(
Parser\Code::getAlt( Parser\Code::getAlt(
$line $line,
$matches
), ),
Parser\Code::isInline( Parser\Code::isInline(
$line $line,
$matches
) )
); );
break; break;
// Header // Header
case Parser\Header::match($line): case Parser\Header::match($line, $matches):
$this->_entity[] = new Entity\Header( $this->_entity[] = new Entity\Header(
Parser\Header::getText( Parser\Header::getText(
$line $line,
$matches
), ),
Parser\Header::getLevel( Parser\Header::getLevel(
$line $line,
$matches
) )
); );
break; break;
// Link // Link
case Parser\Link::match($line): case Parser\Link::match($line, $matches):
$this->_entity[] = new Entity\Link( $this->_entity[] = new Entity\Link(
Parser\Link::getAddress( Parser\Link::getAddress(
$line $line,
$matches
), ),
Parser\Link::getAlt( Parser\Link::getAlt(
$line $line,
$matches
), ),
Parser\Link::getDate( Parser\Link::getDate(
$line $line,
$matches
) )
); );
break; break;
// Listing // Listing
case Parser\Listing::match($line): case Parser\Listing::match($line, $matches):
$this->_entity[] = new Entity\Listing( $this->_entity[] = new Entity\Listing(
Parser\Listing::getItem( Parser\Listing::getItem(
$line $line,
$matches
) )
); );
break; break;
// Quote // Quote
case Parser\Quote::match($line): case Parser\Quote::match($line, $matches):
$this->_entity[] = new Entity\Quote( $this->_entity[] = new Entity\Quote(
Parser\Quote::getText( Parser\Quote::getText(
$line $line,
$matches
) )
); );
@ -95,6 +107,11 @@ class Document
} }
} }
public function getEntities(): array
{
return $this->_entity;
}
public function getHeaders(): array public function getHeaders(): array
{ {
$headers = []; $headers = [];
@ -155,6 +172,13 @@ class Document
return $quotes; return $quotes;
} }
public function append(
\Yggverse\Gemtext\Interface\Entity $entity
): void
{
$this->_entity[] = $entity;
}
public function toString(): string public function toString(): string
{ {
$lines = []; $lines = [];