mirror of
https://github.com/YGGverse/gemini-php.git
synced 2025-01-29 16:24:13 +00:00
Implement Gemtext/Link class
This commit is contained in:
parent
be8b86102f
commit
7916351299
44
README.md
44
README.md
@ -79,10 +79,52 @@ $body = new \Yggverse\Gemini\Gemtext\Body(
|
||||
|
||||
```
|
||||
var_dump(
|
||||
$body->getLinks() // returns array of clickable links
|
||||
$body->getLinks() // returns array of inline links
|
||||
);
|
||||
```
|
||||
|
||||
### Link
|
||||
|
||||
Inline links parser.
|
||||
|
||||
Allows to extract address, date with timestamp and alt text from link line given
|
||||
|
||||
```
|
||||
foreach ($body->getLinks() as $line)
|
||||
{
|
||||
$link = new \Yggverse\Gemini\Gemtext\Link(
|
||||
$line
|
||||
);
|
||||
|
||||
var_dump(
|
||||
$link->getAddress()
|
||||
);
|
||||
|
||||
var_dump(
|
||||
$link->getAlt()
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
#### Link::getAddress
|
||||
#### Link::getDate
|
||||
|
||||
This method also validates time format and returns the unix timestamp as linked argument
|
||||
|
||||
```
|
||||
var_dump(
|
||||
$link->getDate(
|
||||
$timestamp // get unix time from this variable
|
||||
)
|
||||
);
|
||||
|
||||
var_dump(
|
||||
$timestamp
|
||||
);
|
||||
```
|
||||
|
||||
#### Link::getAlt
|
||||
|
||||
## DokuWiki
|
||||
|
||||
Toolkit provides DokuWiki API for Gemini.
|
||||
|
63
src/Gemtext/Link.php
Normal file
63
src/Gemtext/Link.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yggverse\Gemini\Gemtext;
|
||||
|
||||
class Link
|
||||
{
|
||||
private string $_line;
|
||||
|
||||
public function __construct(string $line)
|
||||
{
|
||||
$this->_line = $line;
|
||||
}
|
||||
|
||||
public function getAddress(): ?string
|
||||
{
|
||||
if (preg_match('/^([^\s]+)\s.*/', trim($this->_line), $match))
|
||||
{
|
||||
return trim(
|
||||
$match[1]
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getDate(?int &$timestamp = null): ?string
|
||||
{
|
||||
if (preg_match('/\s([\d]+-[\d+]+-[\d]+)\s/', trim($this->_line), $match))
|
||||
{
|
||||
if ($result = strtotime($match[1]))
|
||||
{
|
||||
$timestamp = $result;
|
||||
|
||||
return trim(
|
||||
$match[1]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getAlt(): ?string
|
||||
{
|
||||
if (preg_match('/\s[\d]+-[\d+]+-[\d]+\s(.*)$/', trim($this->_line), $match))
|
||||
{
|
||||
return trim(
|
||||
$match[1]
|
||||
);
|
||||
}
|
||||
|
||||
else if (preg_match('/\s(.*)$/', trim($this->_line), $match))
|
||||
{
|
||||
return trim(
|
||||
$match[1]
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user