Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Chart;
4
 
5
use PhpOffice\PhpSpreadsheet\RichText\RichText;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PhpOffice\PhpSpreadsheet\Style\Font;
8
 
9
class Title
10
{
11
    public const TITLE_CELL_REFERENCE
12
        = '/^(.*)!' // beginning of string, everything up to ! is match[1]
13
        . '[$]([A-Z]{1,3})' // absolute column string match[2]
14
        . '[$](\d{1,7})$/i'; // absolute row string match[3]
15
 
16
    /**
17
     * Title Caption.
18
     *
19
     * @var array<RichText|string>|RichText|string
20
     */
21
    private array|RichText|string $caption;
22
 
23
    /**
24
     * Allow overlay of other elements?
25
     */
26
    private bool $overlay = true;
27
 
28
    /**
29
     * Title Layout.
30
     */
31
    private ?Layout $layout;
32
 
33
    private string $cellReference = '';
34
 
35
    private ?Font $font = null;
36
 
37
    /**
38
     * Create a new Title.
39
     */
40
    public function __construct(array|RichText|string $caption = '', ?Layout $layout = null, bool $overlay = false)
41
    {
42
        $this->caption = $caption;
43
        $this->layout = $layout;
44
        $this->setOverlay($overlay);
45
    }
46
 
47
    /**
48
     * Get caption.
49
     */
50
    public function getCaption(): array|RichText|string
51
    {
52
        return $this->caption;
53
    }
54
 
55
    public function getCaptionText(?Spreadsheet $spreadsheet = null): string
56
    {
57
        if ($spreadsheet !== null) {
58
            $caption = $this->getCalculatedTitle($spreadsheet);
59
            if ($caption !== null) {
60
                return $caption;
61
            }
62
        }
63
        $caption = $this->caption;
64
        if (is_string($caption)) {
65
            return $caption;
66
        }
67
        if ($caption instanceof RichText) {
68
            return $caption->getPlainText();
69
        }
70
        $retVal = '';
71
        foreach ($caption as $textx) {
72
            /** @var RichText|string $text */
73
            $text = $textx;
74
            if ($text instanceof RichText) {
75
                $retVal .= $text->getPlainText();
76
            } else {
77
                $retVal .= $text;
78
            }
79
        }
80
 
81
        return $retVal;
82
    }
83
 
84
    /**
85
     * Set caption.
86
     *
87
     * @return $this
88
     */
89
    public function setCaption(array|RichText|string $caption): static
90
    {
91
        $this->caption = $caption;
92
 
93
        return $this;
94
    }
95
 
96
    /**
97
     * Get allow overlay of other elements?
98
     */
99
    public function getOverlay(): bool
100
    {
101
        return $this->overlay;
102
    }
103
 
104
    /**
105
     * Set allow overlay of other elements?
106
     */
107
    public function setOverlay(bool $overlay): self
108
    {
109
        $this->overlay = $overlay;
110
 
111
        return $this;
112
    }
113
 
114
    public function getLayout(): ?Layout
115
    {
116
        return $this->layout;
117
    }
118
 
119
    public function setCellReference(string $cellReference): self
120
    {
121
        $this->cellReference = $cellReference;
122
 
123
        return $this;
124
    }
125
 
126
    public function getCellReference(): string
127
    {
128
        return $this->cellReference;
129
    }
130
 
131
    public function getCalculatedTitle(?Spreadsheet $spreadsheet): ?string
132
    {
133
        preg_match(self::TITLE_CELL_REFERENCE, $this->cellReference, $matches);
134
        if (count($matches) === 0 || $spreadsheet === null) {
135
            return null;
136
        }
137
        $sheetName = preg_replace("/^'(.*)'$/", '$1', $matches[1]) ?? '';
138
 
139
        return $spreadsheet->getSheetByName($sheetName)?->getCell($matches[2] . $matches[3])?->getFormattedValue();
140
    }
141
 
142
    public function getFont(): ?Font
143
    {
144
        return $this->font;
145
    }
146
 
147
    public function setFont(?Font $font): self
148
    {
149
        $this->font = $font;
150
 
151
        return $this;
152
    }
153
 
154
    /**
155
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
156
     */
157
    public function __clone()
158
    {
159
        $this->layout = ($this->layout === null) ? null : clone $this->layout;
160
        $this->font = ($this->font === null) ? null : clone $this->font;
161
        if (is_array($this->caption)) {
162
            $captions = $this->caption;
163
            $this->caption = [];
164
            foreach ($captions as $caption) {
165
                $this->caption[] = is_object($caption) ? (clone $caption) : $caption;
166
            }
167
        } else {
168
            $this->caption = is_object($this->caption) ? (clone $this->caption) : $this->caption;
169
        }
170
    }
171
}