mirror of
https://github.com/YGGverse/gemtext-php.git
synced 2026-03-31 17:55:38 +00:00
init Document class
This commit is contained in:
parent
76a34e4ea8
commit
940819fdb7
1 changed files with 76 additions and 0 deletions
76
src/Document.php
Normal file
76
src/Document.php
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Yggverse\Gemtext;
|
||||||
|
|
||||||
|
use \Yggverse\Gemtext;
|
||||||
|
|
||||||
|
class Document
|
||||||
|
{
|
||||||
|
private array $_entities = [];
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
string $data
|
||||||
|
) {
|
||||||
|
foreach ((array) explode(PHP_EOL, $data) as $line)
|
||||||
|
{
|
||||||
|
// Add entity
|
||||||
|
switch (true)
|
||||||
|
{
|
||||||
|
// Code
|
||||||
|
case Parser\Code::match($line):
|
||||||
|
|
||||||
|
$this->_entities[] = new Entity\Code(
|
||||||
|
Parser\Code::getAlt(
|
||||||
|
$line
|
||||||
|
),
|
||||||
|
Parser\Code::isInline(
|
||||||
|
$line
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Link
|
||||||
|
case Parser\Link::match($line):
|
||||||
|
|
||||||
|
$this->_entities[] = new Entity\Link(
|
||||||
|
Parser\Link::getAddress(
|
||||||
|
$line
|
||||||
|
),
|
||||||
|
Parser\Link::getAlt(
|
||||||
|
$line
|
||||||
|
),
|
||||||
|
Parser\Link::getDate(
|
||||||
|
$line
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Plain
|
||||||
|
default:
|
||||||
|
|
||||||
|
$this->_entities[] = new Entity\Text(
|
||||||
|
$line
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
$entities = [];
|
||||||
|
|
||||||
|
foreach ($this->_entities as $entity)
|
||||||
|
{
|
||||||
|
$entities[] = $entity->toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode(
|
||||||
|
PHP_EOL,
|
||||||
|
$entities
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue