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