Completed
Push — develop ( 782b4e...557e80 )
by Adrien
43:38
created

RichText::createText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172 1
        $vars = get_object_vars($this);
173 1
        foreach ($vars as $key => $value) {
174 1
            if (is_object($value)) {
175
                $this->$key = clone $value;
176
            } else {
177 1
                $this->$key = $value;
178
            }
179
        }
180 1
    }
181
}
182