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\RichText;
4
 
5
use PhpOffice\PhpSpreadsheet\Cell\Cell;
6
use PhpOffice\PhpSpreadsheet\Cell\DataType;
7
use PhpOffice\PhpSpreadsheet\IComparable;
8
use Stringable;
9
 
10
class RichText implements IComparable, Stringable
11
{
12
    /**
13
     * Rich text elements.
14
     *
15
     * @var ITextElement[]
16
     */
17
    private array $richTextElements;
18
 
19
    /**
20
     * Create a new RichText instance.
21
     */
22
    public function __construct(?Cell $cell = null)
23
    {
24
        // Initialise variables
25
        $this->richTextElements = [];
26
 
27
        // Rich-Text string attached to cell?
28
        if ($cell !== null) {
29
            // Add cell text and style
30
            if ($cell->getValueString() !== '') {
31
                $objRun = new Run($cell->getValueString());
32
                $objRun->setFont(clone $cell->getWorksheet()->getStyle($cell->getCoordinate())->getFont());
33
                $this->addText($objRun);
34
            }
35
 
36
            // Set parent value
37
            $cell->setValueExplicit($this, DataType::TYPE_STRING);
38
        }
39
    }
40
 
41
    /**
42
     * Add text.
43
     *
44
     * @param ITextElement $text Rich text element
45
     *
46
     * @return $this
47
     */
48
    public function addText(ITextElement $text): static
49
    {
50
        $this->richTextElements[] = $text;
51
 
52
        return $this;
53
    }
54
 
55
    /**
56
     * Create text.
57
     *
58
     * @param string $text Text
59
     */
60
    public function createText(string $text): TextElement
61
    {
62
        $objText = new TextElement($text);
63
        $this->addText($objText);
64
 
65
        return $objText;
66
    }
67
 
68
    /**
69
     * Create text run.
70
     *
71
     * @param string $text Text
72
     */
73
    public function createTextRun(string $text): Run
74
    {
75
        $objText = new Run($text);
76
        $this->addText($objText);
77
 
78
        return $objText;
79
    }
80
 
81
    /**
82
     * Get plain text.
83
     */
84
    public function getPlainText(): string
85
    {
86
        // Return value
87
        $returnValue = '';
88
 
89
        // Loop through all ITextElements
90
        foreach ($this->richTextElements as $text) {
91
            $returnValue .= $text->getText();
92
        }
93
 
94
        return $returnValue;
95
    }
96
 
97
    /**
98
     * Convert to string.
99
     */
100
    public function __toString(): string
101
    {
102
        return $this->getPlainText();
103
    }
104
 
105
    /**
106
     * Get Rich Text elements.
107
     *
108
     * @return ITextElement[]
109
     */
110
    public function getRichTextElements(): array
111
    {
112
        return $this->richTextElements;
113
    }
114
 
115
    /**
116
     * Set Rich Text elements.
117
     *
118
     * @param ITextElement[] $textElements Array of elements
119
     *
120
     * @return $this
121
     */
122
    public function setRichTextElements(array $textElements): static
123
    {
124
        $this->richTextElements = $textElements;
125
 
126
        return $this;
127
    }
128
 
129
    /**
130
     * Get hash code.
131
     *
132
     * @return string Hash code
133
     */
134
    public function getHashCode(): string
135
    {
136
        $hashElements = '';
137
        foreach ($this->richTextElements as $element) {
138
            $hashElements .= $element->getHashCode();
139
        }
140
 
141
        return md5(
142
            $hashElements
143
            . __CLASS__
144
        );
145
    }
146
 
147
    /**
148
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
149
     */
150
    public function __clone()
151
    {
152
        $vars = get_object_vars($this);
153
        foreach ($vars as $key => $value) {
154
            $newValue = is_object($value) ? (clone $value) : $value;
155
            if (is_array($value)) {
156
                $newValue = [];
157
                foreach ($value as $key2 => $value2) {
158
                    $newValue[$key2] = is_object($value2) ? (clone $value2) : $value2;
159
                }
160
            }
161
            $this->$key = $newValue;
162
        }
163
    }
164
}