PreserveText.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord 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/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Element;
  18. use PhpOffice\PhpWord\Shared\Text as SharedText;
  19. use PhpOffice\PhpWord\Style\Font;
  20. use PhpOffice\PhpWord\Style\Paragraph;
  21. /**
  22. * Preserve text/field element
  23. */
  24. class PreserveText extends AbstractElement
  25. {
  26. /**
  27. * Text content
  28. *
  29. * @var string|array
  30. */
  31. private $text;
  32. /**
  33. * Text style
  34. *
  35. * @var string|\PhpOffice\PhpWord\Style\Font
  36. */
  37. private $fontStyle;
  38. /**
  39. * Paragraph style
  40. *
  41. * @var string|\PhpOffice\PhpWord\Style\Paragraph
  42. */
  43. private $paragraphStyle;
  44. /**
  45. * Create a new Preserve Text Element
  46. *
  47. * @param string $text
  48. * @param mixed $fontStyle
  49. * @param mixed $paragraphStyle
  50. */
  51. public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
  52. {
  53. $this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle);
  54. $this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);
  55. $this->text = SharedText::toUTF8($text);
  56. $matches = preg_split('/({.*?})/', $this->text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  57. if (isset($matches[0])) {
  58. $this->text = $matches;
  59. }
  60. }
  61. /**
  62. * Get Text style
  63. *
  64. * @return string|\PhpOffice\PhpWord\Style\Font
  65. */
  66. public function getFontStyle()
  67. {
  68. return $this->fontStyle;
  69. }
  70. /**
  71. * Get Paragraph style
  72. *
  73. * @return string|\PhpOffice\PhpWord\Style\Paragraph
  74. */
  75. public function getParagraphStyle()
  76. {
  77. return $this->paragraphStyle;
  78. }
  79. /**
  80. * Get Text content
  81. *
  82. * @return string|array
  83. */
  84. public function getText()
  85. {
  86. return $this->text;
  87. }
  88. }