Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.55% |
56 / 58 |
|
96.30% |
26 / 27 |
CRAP | |
0.00% |
0 / 1 |
| Paragraph | |
96.55% |
56 / 58 |
|
96.30% |
26 / 27 |
32 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| __clone | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getAlignment | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setAlignment | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getFont | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setFont | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getBulletStyle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setBulletStyle | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| createText | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| addText | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| createBreak | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| createTextRun | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPlainText | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getRichTextElements | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setRichTextElements | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getHashCode | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getHashIndex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setHashIndex | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getLineSpacing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setLineSpacing | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getLineSpacingMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setLineSpacingMode | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| getSpacingBefore | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setSpacingBefore | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getSpacingAfter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setSpacingAfter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of PHPPresentation - A pure PHP library for reading and writing |
| 4 | * presentations documents. |
| 5 | * |
| 6 | * PHPPresentation is free software distributed under the terms of the GNU Lesser |
| 7 | * General Public License version 3 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * For the full copyright and license information, please read the LICENSE |
| 10 | * file that was distributed with this source code. For the full list of |
| 11 | * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors. |
| 12 | * |
| 13 | * @see https://github.com/PHPOffice/PHPPresentation |
| 14 | * |
| 15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace PhpOffice\PhpPresentation\Shape\RichText; |
| 21 | |
| 22 | use PhpOffice\PhpPresentation\ComparableInterface; |
| 23 | use PhpOffice\PhpPresentation\Style\Alignment; |
| 24 | use PhpOffice\PhpPresentation\Style\Bullet; |
| 25 | use PhpOffice\PhpPresentation\Style\Font; |
| 26 | |
| 27 | /** |
| 28 | * \PhpOffice\PhpPresentation\Shape\RichText\Paragraph. |
| 29 | */ |
| 30 | class Paragraph implements ComparableInterface |
| 31 | { |
| 32 | public const LINE_SPACING_MODE_PERCENT = 'percent'; |
| 33 | public const LINE_SPACING_MODE_POINT = 'point'; |
| 34 | |
| 35 | /** |
| 36 | * Rich text elements. |
| 37 | * |
| 38 | * @var array<TextElementInterface> |
| 39 | */ |
| 40 | private $richTextElements = []; |
| 41 | |
| 42 | /** |
| 43 | * Alignment. |
| 44 | * |
| 45 | * @var Alignment |
| 46 | */ |
| 47 | private $alignment; |
| 48 | |
| 49 | /** |
| 50 | * Font. |
| 51 | * |
| 52 | * @var null|Font |
| 53 | */ |
| 54 | private $font; |
| 55 | |
| 56 | /** |
| 57 | * Bullet style. |
| 58 | * |
| 59 | * @var Bullet |
| 60 | */ |
| 61 | private $bulletStyle; |
| 62 | |
| 63 | /** |
| 64 | * @var int |
| 65 | */ |
| 66 | private $lineSpacing = 100; |
| 67 | |
| 68 | /** |
| 69 | * @var string |
| 70 | */ |
| 71 | private $lineSpacingMode = self::LINE_SPACING_MODE_PERCENT; |
| 72 | |
| 73 | /** |
| 74 | * @var int |
| 75 | */ |
| 76 | private $spacingBefore = 0; |
| 77 | |
| 78 | /** |
| 79 | * @var int |
| 80 | */ |
| 81 | private $spacingAfter = 0; |
| 82 | |
| 83 | /** |
| 84 | * Hash index. |
| 85 | * |
| 86 | * @var int |
| 87 | */ |
| 88 | private $hashIndex; |
| 89 | |
| 90 | /** |
| 91 | * Create a new \PhpOffice\PhpPresentation\Shape\RichText\Paragraph instance. |
| 92 | */ |
| 93 | public function __construct() |
| 94 | { |
| 95 | $this->alignment = new Alignment(); |
| 96 | $this->font = new Font(); |
| 97 | $this->bulletStyle = new Bullet(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Magic Method : clone. |
| 102 | */ |
| 103 | public function __clone() |
| 104 | { |
| 105 | // Clone each text |
| 106 | foreach ($this->richTextElements as &$rtElement) { |
| 107 | $rtElement = clone $rtElement; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get alignment. |
| 113 | */ |
| 114 | public function getAlignment(): Alignment |
| 115 | { |
| 116 | return $this->alignment; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Set alignment. |
| 121 | */ |
| 122 | public function setAlignment(Alignment $alignment): self |
| 123 | { |
| 124 | $this->alignment = $alignment; |
| 125 | |
| 126 | return $this; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get font. |
| 131 | */ |
| 132 | public function getFont(): ?Font |
| 133 | { |
| 134 | return $this->font; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Set font. |
| 139 | * |
| 140 | * @param null|Font $pFont Font |
| 141 | */ |
| 142 | public function setFont(?Font $pFont = null): self |
| 143 | { |
| 144 | $this->font = $pFont; |
| 145 | |
| 146 | return $this; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get bullet style. |
| 151 | */ |
| 152 | public function getBulletStyle(): ?Bullet |
| 153 | { |
| 154 | return $this->bulletStyle; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Set bullet style. |
| 159 | */ |
| 160 | public function setBulletStyle(?Bullet $style = null): self |
| 161 | { |
| 162 | $this->bulletStyle = $style; |
| 163 | |
| 164 | return $this; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Create text (can not be formatted !). |
| 169 | * |
| 170 | * @param string $pText Text |
| 171 | */ |
| 172 | public function createText(string $pText = ''): TextElement |
| 173 | { |
| 174 | $objText = new TextElement($pText); |
| 175 | $this->addText($objText); |
| 176 | |
| 177 | return $objText; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Add text. |
| 182 | * |
| 183 | * @param null|TextElementInterface $pText Rich text element |
| 184 | */ |
| 185 | public function addText(?TextElementInterface $pText = null): self |
| 186 | { |
| 187 | $this->richTextElements[] = $pText; |
| 188 | |
| 189 | return $this; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Create break. |
| 194 | */ |
| 195 | public function createBreak(): BreakElement |
| 196 | { |
| 197 | $objText = new BreakElement(); |
| 198 | $this->addText($objText); |
| 199 | |
| 200 | return $objText; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Create text run (can be formatted). |
| 205 | * |
| 206 | * @param string $pText Text |
| 207 | */ |
| 208 | public function createTextRun(string $pText = ''): Run |
| 209 | { |
| 210 | $objText = new Run($pText); |
| 211 | $objText->setFont(clone $this->font); |
| 212 | $this->addText($objText); |
| 213 | |
| 214 | return $objText; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Convert to string. |
| 219 | * |
| 220 | * @return string |
| 221 | */ |
| 222 | public function __toString() |
| 223 | { |
| 224 | return $this->getPlainText(); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Get plain text. |
| 229 | */ |
| 230 | public function getPlainText(): string |
| 231 | { |
| 232 | // Return value |
| 233 | $returnValue = ''; |
| 234 | |
| 235 | // Loop trough all TextElementInterface |
| 236 | foreach ($this->richTextElements as $text) { |
| 237 | if ($text instanceof TextElementInterface) { |
| 238 | $returnValue .= $text->getText(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // Return |
| 243 | return $returnValue; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Get Rich Text elements. |
| 248 | * |
| 249 | * @return array<TextElementInterface> |
| 250 | */ |
| 251 | public function getRichTextElements(): array |
| 252 | { |
| 253 | return $this->richTextElements; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Set Rich Text elements. |
| 258 | * |
| 259 | * @param array<TextElementInterface> $pElements Array of elements |
| 260 | */ |
| 261 | public function setRichTextElements(array $pElements = []): self |
| 262 | { |
| 263 | $this->richTextElements = $pElements; |
| 264 | |
| 265 | return $this; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Get hash code. |
| 270 | * |
| 271 | * @return string Hash code |
| 272 | */ |
| 273 | public function getHashCode(): string |
| 274 | { |
| 275 | $hashElements = ''; |
| 276 | foreach ($this->richTextElements as $element) { |
| 277 | $hashElements .= $element->getHashCode(); |
| 278 | } |
| 279 | |
| 280 | return md5($hashElements . $this->font->getHashCode() . __CLASS__); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Get hash index. |
| 285 | * |
| 286 | * Note that this index may vary during script execution! Only reliable moment is |
| 287 | * while doing a write of a workbook and when changes are not allowed. |
| 288 | * |
| 289 | * @return null|int Hash index |
| 290 | */ |
| 291 | public function getHashIndex(): ?int |
| 292 | { |
| 293 | return $this->hashIndex; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Set hash index. |
| 298 | * |
| 299 | * Note that this index may vary during script execution! Only reliable moment is |
| 300 | * while doing a write of a workbook and when changes are not allowed. |
| 301 | * |
| 302 | * @param int $value Hash index |
| 303 | * |
| 304 | * @return $this |
| 305 | */ |
| 306 | public function setHashIndex(int $value) |
| 307 | { |
| 308 | $this->hashIndex = $value; |
| 309 | |
| 310 | return $this; |
| 311 | } |
| 312 | |
| 313 | public function getLineSpacing(): int |
| 314 | { |
| 315 | return $this->lineSpacing; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Value in points. |
| 320 | * |
| 321 | * @param int $lineSpacing |
| 322 | */ |
| 323 | public function setLineSpacing($lineSpacing): self |
| 324 | { |
| 325 | $this->lineSpacing = $lineSpacing; |
| 326 | |
| 327 | return $this; |
| 328 | } |
| 329 | |
| 330 | public function getLineSpacingMode(): string |
| 331 | { |
| 332 | return $this->lineSpacingMode; |
| 333 | } |
| 334 | |
| 335 | public function setLineSpacingMode(string $lineSpacingMode): self |
| 336 | { |
| 337 | if (in_array($lineSpacingMode, [ |
| 338 | self::LINE_SPACING_MODE_PERCENT, |
| 339 | self::LINE_SPACING_MODE_POINT, |
| 340 | ])) { |
| 341 | $this->lineSpacingMode = $lineSpacingMode; |
| 342 | } |
| 343 | |
| 344 | return $this; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Value in points. |
| 349 | */ |
| 350 | public function getSpacingBefore(): int |
| 351 | { |
| 352 | return $this->spacingBefore; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Value in points. |
| 357 | */ |
| 358 | public function setSpacingBefore(int $spacingBefore): self |
| 359 | { |
| 360 | $this->spacingBefore = $spacingBefore; |
| 361 | |
| 362 | return $this; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Value in points. |
| 367 | */ |
| 368 | public function getSpacingAfter(): int |
| 369 | { |
| 370 | return $this->spacingAfter; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Value in points. |
| 375 | */ |
| 376 | public function setSpacingAfter(int $spacingAfter): self |
| 377 | { |
| 378 | $this->spacingAfter = $spacingAfter; |
| 379 | |
| 380 | return $this; |
| 381 | } |
| 382 | } |