2020-08-18 16:31:28 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-18 16:31:28 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2020-08-18 16:31:28 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Entities;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
use CodeIgniter\Entity\Entity;
|
2021-05-12 14:00:25 +00:00
|
|
|
use CodeIgniter\I18n\Time;
|
2022-02-02 17:01:19 +00:00
|
|
|
use League\CommonMark\Environment\Environment;
|
|
|
|
use League\CommonMark\Extension\Autolink\AutolinkExtension;
|
|
|
|
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
|
|
|
|
use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlExtension;
|
|
|
|
use League\CommonMark\Extension\SmartPunct\SmartPunctExtension;
|
|
|
|
use League\CommonMark\MarkdownConverter;
|
2020-08-18 16:31:28 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
/**
|
|
|
|
* @property int $id
|
|
|
|
* @property string $title
|
|
|
|
* @property string $link
|
|
|
|
* @property string $slug
|
|
|
|
* @property string $content_markdown
|
|
|
|
* @property string $content_html
|
|
|
|
* @property Time $created_at
|
|
|
|
* @property Time $updated_at
|
|
|
|
* @property Time|null $delete_at
|
|
|
|
*/
|
2020-08-18 16:31:28 +00:00
|
|
|
class Page extends Entity
|
|
|
|
{
|
2021-05-14 17:59:35 +00:00
|
|
|
protected string $link;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
protected string $content_html;
|
2020-08-18 16:31:28 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
2020-08-18 16:31:28 +00:00
|
|
|
protected $casts = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'id' => 'integer',
|
|
|
|
'title' => 'string',
|
|
|
|
'slug' => 'string',
|
2021-05-12 14:00:25 +00:00
|
|
|
'content_markdown' => 'string',
|
2023-06-12 14:47:38 +00:00
|
|
|
'content_html' => 'string',
|
2020-08-18 16:31:28 +00:00
|
|
|
];
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function getLink(): string
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
return url_to('page', $this->attributes['slug']);
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function setContentMarkdown(string $contentMarkdown): static
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
2022-02-02 17:01:19 +00:00
|
|
|
$config = [
|
2020-08-18 16:31:28 +00:00
|
|
|
'allow_unsafe_links' => false,
|
2022-02-02 17:01:19 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$environment = new Environment($config);
|
|
|
|
$environment->addExtension(new CommonMarkCoreExtension());
|
|
|
|
$environment->addExtension(new AutolinkExtension());
|
|
|
|
$environment->addExtension(new SmartPunctExtension());
|
|
|
|
$environment->addExtension(new DisallowedRawHtmlExtension());
|
|
|
|
|
|
|
|
$converter = new MarkdownConverter($environment);
|
2020-08-18 16:31:28 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
$this->attributes['content_markdown'] = $contentMarkdown;
|
2022-02-02 17:01:19 +00:00
|
|
|
$this->attributes['content_html'] = $converter->convert($contentMarkdown);
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
|
|
return $this;
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
}
|