xref: /webtrees/app/Report/ReportBaseElement.php (revision 78606cf427772fbdc5222ff607ee23f43078f94b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Report;
21
22use function strip_tags;
23use function trim;
24
25/**
26 * Class ReportBaseElement
27 */
28class ReportBaseElement
29{
30    // Special value for X or Y position, to indicate the current position.
31    public const CURRENT_POSITION = -1.0;
32
33    public string $text = '';
34
35    /**
36     * Element renderer
37     *
38     * @param HtmlRenderer|PdfRenderer $renderer
39     *
40     * @return void
41     */
42    public function render($renderer): void
43    {
44        //-- to be implemented in inherited classes
45    }
46
47    /**
48     * Get the height.
49     *
50     * @param HtmlRenderer|PdfRenderer $renderer
51     *
52     * @return float
53     */
54    public function getHeight($renderer): float
55    {
56        return 0.0;
57    }
58
59    /**
60     * Get the width.
61     *
62     * @param HtmlRenderer|PdfRenderer $renderer
63     *
64     * @return array{0:float,1:int,2:float}
65     */
66    public function getWidth($renderer): array
67    {
68        return [0.0, 1, 0.0];
69    }
70
71    /**
72     * Add text.
73     *
74     * @param string $t
75     *
76     * @return void
77     */
78    public function addText(string $t): void
79    {
80        $t = trim($t, "\r\n\t");
81        $t = strtr($t, ['<br>' => "\n", '&nbsp;' => ' ']);
82
83        $this->text .= strip_tags($t);
84    }
85
86    /**
87     * Add an end-of-line.
88     *
89     * @return void
90     */
91    public function addNewline(): void
92    {
93        $this->text .= "\n";
94    }
95
96    /**
97     * Get the current text.
98     *
99     * @return string
100     */
101    public function getValue(): string
102    {
103        return $this->text;
104    }
105
106    /**
107     * Set the width to wrap text.
108     *
109     * @param float $wrapwidth
110     * @param float $cellwidth
111     *
112     * @return float
113     */
114    public function setWrapWidth(float $wrapwidth, float $cellwidth): float
115    {
116        return 0;
117    }
118
119    /**
120     * Render the footnotes.
121     *
122     * @param HtmlRenderer|PdfRenderer $renderer
123     *
124     * @return void
125     */
126    public function renderFootnote($renderer): void
127    {
128    }
129
130    /**
131     * Set the text.
132     *
133     * @param string $text
134     *
135     * @return void
136     */
137    public function setText(string $text): void
138    {
139        $this->text = $text;
140    }
141}
142