aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/forms/Text.php
blob: 611ff832ac7d49d82bc40e31ef2cb9127d090521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php

namespace Studip\Forms;

/**
 * The Text class represents a part of a form that just displays text.
 * The text can either be HTML or unformatted text.
 */
class Text extends Part
{
    /**
     * The text to be displayed.
     */
    protected $text = '';

    /**
     * This attribute defines whether to interpret the text as HTML (true) or as plain text (false).
     */
    protected $text_is_html = true;

    /**
     * Sets the text that shall be displayed in this form part.
     *
     * @param string $text The text to be displayed.
     * @param bool $text_is_html Whether the text is HTML (true) or plain text. Defaults to true.
     * @return $this This form part.
     */
    public function setText(string $text, bool $text_is_html = true): Text
    {
        $this->text = $text;
        $this->text_is_html = $text_is_html;
        return $this;
    }

    /**
     * @return string The "raw form" of the text that shall be displayed.
     */
    public function getText() : string
    {
        return $this->text;
    }

    /**
     * @return bool Whether the text is HTML (true) or not (false).
     */
    public function isHtmlText() : bool
    {
        return $this->text_is_html;
    }

    /**
     * "Renders" the text: Either return it directly, if it is HTML or call htmlReady first before returning it.
     *
     * @return string The text that shall be placed in the form, either as HTML or plain text.
     */
    public function render()
    {
        if ($this->text_is_html) {
            return $this->text;
        } else {
            return htmlReady($this->text);
        }
    }

    /**
     * @see Text::render()
     */
    public function renderWithCondition()
    {
        return $this->render();
    }
}