Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TextRun
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
4 / 4
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParagraphStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setParagraphStyle
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 getText
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * This file is part of PHPWord - A pure PHP library for reading and writing
5 * word processing documents.
6 *
7 * PHPWord is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @see         https://github.com/PHPOffice/PHPWord
15 *
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19namespace PhpOffice\PhpWord\Element;
20
21use PhpOffice\PhpWord\Style\Paragraph;
22
23/**
24 * Textrun/paragraph element.
25 */
26class TextRun extends AbstractContainer
27{
28    /**
29     * @var string Container type
30     */
31    protected $container = 'TextRun';
32
33    /**
34     * Paragraph style.
35     *
36     * @var Paragraph|string
37     */
38    protected $paragraphStyle;
39
40    /**
41     * Create new instance.
42     *
43     * @param array|Paragraph|string $paragraphStyle
44     */
45    public function __construct($paragraphStyle = null)
46    {
47        $this->paragraphStyle = $this->setParagraphStyle($paragraphStyle);
48    }
49
50    /**
51     * Get Paragraph style.
52     *
53     * @return Paragraph|string
54     */
55    public function getParagraphStyle()
56    {
57        return $this->paragraphStyle;
58    }
59
60    /**
61     * Set Paragraph style.
62     *
63     * @param array|Paragraph|string $style
64     *
65     * @return Paragraph|string
66     */
67    public function setParagraphStyle($style = null)
68    {
69        if (is_array($style)) {
70            $this->paragraphStyle = new Paragraph();
71            $this->paragraphStyle->setStyleByArray($style);
72        } elseif ($style instanceof Paragraph) {
73            $this->paragraphStyle = $style;
74        } elseif (null === $style) {
75            $this->paragraphStyle = new Paragraph();
76        } else {
77            $this->paragraphStyle = $style;
78        }
79
80        return $this->paragraphStyle;
81    }
82
83    public function getText(): string
84    {
85        $outstr = '';
86        foreach ($this->getElements() as $element) {
87            if ($element instanceof Text) {
88                $outstr .= $element->getText();
89            } elseif ($element instanceof Ruby) {
90                $outstr .= $element->getBaseTextRun()->getText() .
91                    ' (' . $element->getRubyTextRun()->getText() . ')';
92            }
93        }
94
95        return $outstr;
96    }
97}