Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       46 / 46  | 
               | 
       100.00%  | 
       6 / 6  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| AbstractElement |         | 
       100.00%  | 
       46 / 46  | 
               | 
       100.00%  | 
       6 / 6  | 
       22 |         | 
       100.00%  | 
       1 / 1  | 
      
| write | n/a  | 
       0 / 0  | 
       n/a  | 
       0 / 0  | 
       0 | |||||
| __construct |         | 
       100.00%  | 
       4 / 4  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| getStyles |         | 
       100.00%  | 
       17 / 17  | 
               | 
       100.00%  | 
       1 / 1  | 
       8 | |||
| writeOpening |         | 
       100.00%  | 
       5 / 5  | 
               | 
       100.00%  | 
       1 / 1  | 
       3 | |||
| writeText |         | 
       100.00%  | 
       3 / 3  | 
               | 
       100.00%  | 
       1 / 1  | 
       2 | |||
| writeClosing |         | 
       100.00%  | 
       3 / 3  | 
               | 
       100.00%  | 
       1 / 1  | 
       2 | |||
| writeFontStyle |         | 
       100.00%  | 
       14 / 14  | 
               | 
       100.00%  | 
       1 / 1  | 
       6 | |||
| 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 | |
| 19 | namespace PhpOffice\PhpWord\Writer\RTF\Element; | 
| 20 | |
| 21 | use PhpOffice\PhpWord\Element\AbstractElement as Element; | 
| 22 | use PhpOffice\PhpWord\Escaper\Rtf; | 
| 23 | use PhpOffice\PhpWord\Settings; | 
| 24 | use PhpOffice\PhpWord\Shared\Text as SharedText; | 
| 25 | use PhpOffice\PhpWord\Style; | 
| 26 | use PhpOffice\PhpWord\Style\Font as FontStyle; | 
| 27 | use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle; | 
| 28 | use PhpOffice\PhpWord\Writer\RTF as WriterRTF; | 
| 29 | use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter; | 
| 30 | use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter; | 
| 31 | |
| 32 | /** | 
| 33 | * Abstract RTF element writer. | 
| 34 | * | 
| 35 | * @since 0.11.0 | 
| 36 | */ | 
| 37 | abstract class AbstractElement | 
| 38 | { | 
| 39 | /** | 
| 40 | * Parent writer. | 
| 41 | * | 
| 42 | * @var WriterRTF | 
| 43 | */ | 
| 44 | protected $parentWriter; | 
| 45 | |
| 46 | /** | 
| 47 | * Element. | 
| 48 | * | 
| 49 | * @var Element | 
| 50 | */ | 
| 51 | protected $element; | 
| 52 | |
| 53 | /** | 
| 54 | * Without paragraph. | 
| 55 | * | 
| 56 | * @var bool | 
| 57 | */ | 
| 58 | protected $withoutP = false; | 
| 59 | |
| 60 | /** | 
| 61 | * Write element. | 
| 62 | * | 
| 63 | * @return string | 
| 64 | */ | 
| 65 | abstract public function write(); | 
| 66 | |
| 67 | /** | 
| 68 | * Font style. | 
| 69 | * | 
| 70 | * @var FontStyle | 
| 71 | */ | 
| 72 | protected $fontStyle; | 
| 73 | |
| 74 | /** | 
| 75 | * Paragraph style. | 
| 76 | * | 
| 77 | * @var ParagraphStyle | 
| 78 | */ | 
| 79 | protected $paragraphStyle; | 
| 80 | |
| 81 | /** | 
| 82 | * @var \PhpOffice\PhpWord\Escaper\EscaperInterface | 
| 83 | */ | 
| 84 | protected $escaper; | 
| 85 | |
| 86 | public function __construct(WriterRTF $parentWriter, Element $element, bool $withoutP = false) | 
| 87 | { | 
| 88 | $this->parentWriter = $parentWriter; | 
| 89 | $this->element = $element; | 
| 90 | $this->withoutP = $withoutP; | 
| 91 | $this->escaper = new Rtf(); | 
| 92 | } | 
| 93 | |
| 94 | /** | 
| 95 | * Get font and paragraph styles. | 
| 96 | */ | 
| 97 | protected function getStyles(): void | 
| 98 | { | 
| 99 | /** @var WriterRTF $parentWriter Type hint */ | 
| 100 | $parentWriter = $this->parentWriter; | 
| 101 | |
| 102 | /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ | 
| 103 | $element = $this->element; | 
| 104 | |
| 105 | // Font style | 
| 106 | if (method_exists($element, 'getFontStyle')) { | 
| 107 | $this->fontStyle = $element->getFontStyle(); | 
| 108 | if (is_string($this->fontStyle)) { | 
| 109 | $this->fontStyle = Style::getStyle($this->fontStyle); | 
| 110 | } | 
| 111 | } | 
| 112 | |
| 113 | // Paragraph style | 
| 114 | if (method_exists($element, 'getParagraphStyle')) { | 
| 115 | $this->paragraphStyle = $element->getParagraphStyle(); | 
| 116 | if (is_string($this->paragraphStyle)) { | 
| 117 | $this->paragraphStyle = Style::getStyle($this->paragraphStyle); | 
| 118 | } | 
| 119 | |
| 120 | if ($this->paragraphStyle !== null && !$this->withoutP) { | 
| 121 | if ($parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) { | 
| 122 | $parentWriter->setLastParagraphStyle($element->getParagraphStyle()); | 
| 123 | } else { | 
| 124 | $parentWriter->setLastParagraphStyle(); | 
| 125 | $this->paragraphStyle = null; | 
| 126 | } | 
| 127 | } else { | 
| 128 | $parentWriter->setLastParagraphStyle(); | 
| 129 | $this->paragraphStyle = null; | 
| 130 | } | 
| 131 | } | 
| 132 | } | 
| 133 | |
| 134 | /** | 
| 135 | * Write opening. | 
| 136 | * | 
| 137 | * @return string | 
| 138 | */ | 
| 139 | protected function writeOpening() | 
| 140 | { | 
| 141 | if ($this->withoutP || !$this->paragraphStyle instanceof ParagraphStyle) { | 
| 142 | return ''; | 
| 143 | } | 
| 144 | |
| 145 | $styleWriter = new ParagraphStyleWriter($this->paragraphStyle); | 
| 146 | $styleWriter->setNestedLevel($this->element->getNestedLevel()); | 
| 147 | |
| 148 | return $styleWriter->write(); | 
| 149 | } | 
| 150 | |
| 151 | /** | 
| 152 | * Write text. | 
| 153 | * | 
| 154 | * @param string $text | 
| 155 | * | 
| 156 | * @return string | 
| 157 | */ | 
| 158 | protected function writeText($text) | 
| 159 | { | 
| 160 | if (Settings::isOutputEscapingEnabled()) { | 
| 161 | return $this->escaper->escape($text); | 
| 162 | } | 
| 163 | |
| 164 | return SharedText::toUnicode($text); // todo: replace with `return $text;` later. | 
| 165 | } | 
| 166 | |
| 167 | /** | 
| 168 | * Write closing. | 
| 169 | * | 
| 170 | * @return string | 
| 171 | */ | 
| 172 | protected function writeClosing() | 
| 173 | { | 
| 174 | if ($this->withoutP) { | 
| 175 | return ''; | 
| 176 | } | 
| 177 | |
| 178 | return '\par' . PHP_EOL; | 
| 179 | } | 
| 180 | |
| 181 | /** | 
| 182 | * Write font style. | 
| 183 | * | 
| 184 | * @return string | 
| 185 | */ | 
| 186 | protected function writeFontStyle() | 
| 187 | { | 
| 188 | if (!$this->fontStyle instanceof FontStyle) { | 
| 189 | return ''; | 
| 190 | } | 
| 191 | |
| 192 | /** @var WriterRTF $parentWriter Type hint */ | 
| 193 | $parentWriter = $this->parentWriter; | 
| 194 | |
| 195 | // Create style writer and set color/name index | 
| 196 | $styleWriter = new FontStyleWriter($this->fontStyle); | 
| 197 | if ($this->fontStyle->getColor() != null) { | 
| 198 | $colorIndex = array_search($this->fontStyle->getColor(), $parentWriter->getColorTable()); | 
| 199 | if ($colorIndex !== false) { | 
| 200 | $styleWriter->setColorIndex($colorIndex + 1); | 
| 201 | } | 
| 202 | } | 
| 203 | if ($this->fontStyle->getName() != null) { | 
| 204 | $fontIndex = array_search($this->fontStyle->getName(), $parentWriter->getFontTable()); | 
| 205 | if ($fontIndex !== false) { | 
| 206 | $styleWriter->setNameIndex($fontIndex); | 
| 207 | } | 
| 208 | } | 
| 209 | |
| 210 | // Write style | 
| 211 | $content = $styleWriter->write(); | 
| 212 | |
| 213 | return $content; | 
| 214 | } | 
| 215 | } |