]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/HtmlDescriptionTrait.php
Maintenance: Continued work towards PHPstan level 2
[bookstack] / app / Entities / Models / HtmlDescriptionTrait.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use BookStack\Util\HtmlContentFilter;
6
7 /**
8  * @property string $description
9  * @property string $description_html
10  */
11 trait HtmlDescriptionTrait
12 {
13     public function descriptionHtml(bool $raw = false): string
14     {
15         $html = $this->description_html ?: '<p>' . nl2br(e($this->description)) . '</p>';
16         if ($raw) {
17             return $html;
18         }
19
20         return HtmlContentFilter::removeScriptsFromHtmlString($html);
21     }
22
23     public function setDescriptionHtml(string $html, string|null $plaintext = null): void
24     {
25         $this->description_html = $html;
26
27         if ($plaintext !== null) {
28             $this->description = $plaintext;
29         }
30
31         if (empty($html) && !empty($plaintext)) {
32             $this->description_html = $this->descriptionHtml();
33         }
34     }
35 }