Title.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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;
  20. /**
  21. * Title element
  22. */
  23. class Title extends AbstractElement
  24. {
  25. /**
  26. * Title Text content
  27. *
  28. * @var string|TextRun
  29. */
  30. private $text;
  31. /**
  32. * Title depth
  33. *
  34. * @var int
  35. */
  36. private $depth = 1;
  37. /**
  38. * Name of the heading style, e.g. 'Heading1'
  39. *
  40. * @var string
  41. */
  42. private $style;
  43. /**
  44. * Is part of collection
  45. *
  46. * @var bool
  47. */
  48. protected $collectionRelation = true;
  49. /**
  50. * Create a new Title Element
  51. *
  52. * @param string|TextRun $text
  53. * @param int $depth
  54. */
  55. public function __construct($text, $depth = 1)
  56. {
  57. if (is_string($text)) {
  58. $this->text = SharedText::toUTF8($text);
  59. } elseif ($text instanceof TextRun) {
  60. $this->text = $text;
  61. } else {
  62. throw new \InvalidArgumentException('Invalid text, should be a string or a TextRun');
  63. }
  64. $this->depth = $depth;
  65. $styleName = $depth === 0 ? 'Title' : "Heading_{$this->depth}";
  66. if (array_key_exists($styleName, Style::getStyles())) {
  67. $this->style = str_replace('_', '', $styleName);
  68. }
  69. }
  70. /**
  71. * Get Title Text content
  72. *
  73. * @return string
  74. */
  75. public function getText()
  76. {
  77. return $this->text;
  78. }
  79. /**
  80. * Get depth
  81. *
  82. * @return int
  83. */
  84. public function getDepth()
  85. {
  86. return $this->depth;
  87. }
  88. /**
  89. * Get Title style
  90. *
  91. * @return string
  92. */
  93. public function getStyle()
  94. {
  95. return $this->style;
  96. }
  97. }