xref: /webtrees/app/Report/ReportBaseElement.php (revision 9b152ff9230017d2c03aa1bf603a98b18250446d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Report;
21
22use function htmlspecialchars_decode;
23use function str_replace;
24use function strip_tags;
25use function trim;
26
27/**
28 * Class ReportBaseElement
29 */
30class ReportBaseElement
31{
32    // Special value for X or Y position, to indicate the current position.
33    public const CURRENT_POSITION = -1.0;
34
35    /** @var string Text */
36    public $text = '';
37
38    /**
39     * Element renderer
40     *
41     * @param HtmlRenderer|PdfRenderer $renderer
42     *
43     * @return void
44     */
45    public function render($renderer)
46    {
47        //-- to be implemented in inherited classes
48    }
49
50    /**
51     * Get the height.
52     *
53     * @param HtmlRenderer|PdfRenderer $renderer
54     *
55     * @return float
56     */
57    public function getHeight($renderer): float
58    {
59        return 0.0;
60    }
61
62    /**
63     * Get the width.
64     *
65     * @param HtmlRenderer|PdfRenderer $renderer
66     *
67     * @return float|array
68     */
69    public function getWidth($renderer)
70    {
71        return 0.0;
72    }
73
74    /**
75     * Add text.
76     *
77     * @param string $t
78     *
79     * @return void
80     */
81    public function addText(string $t): void
82    {
83        $t          = trim($t, "\r\n\t");
84        $t          = str_replace([
85            '<br>',
86            '&nbsp;',
87        ], [
88            "\n",
89            ' ',
90        ], $t);
91        $t          = strip_tags($t);
92        $t          = htmlspecialchars_decode($t);
93        $this->text .= $t;
94    }
95
96    /**
97     * Add an end-of-line.
98     *
99     * @return void
100     */
101    public function addNewline(): void
102    {
103        $this->text .= "\n";
104    }
105
106    /**
107     * Get the current text.
108     *
109     * @return string
110     */
111    public function getValue(): string
112    {
113        return $this->text;
114    }
115
116    /**
117     * Set the width to wrap text.
118     *
119     * @param float $wrapwidth
120     * @param float $cellwidth
121     *
122     * @return float
123     */
124    public function setWrapWidth(float $wrapwidth, float $cellwidth): float
125    {
126        return 0;
127    }
128
129    /**
130     * Render the footnotes.
131     *
132     * @param HtmlRenderer|PdfRenderer $renderer
133     *
134     * @return void
135     */
136    public function renderFootnote($renderer): void
137    {
138    }
139
140    /**
141     * Set the text.
142     *
143     * @param string $text
144     *
145     * @return void
146     */
147    public function setText(string $text): void
148    {
149        $this->text = $text;
150    }
151}
152