PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PhpWord/Style.php

https://github.com/cyrillkalita/PHPWord
PHP | 199 lines | 77 code | 16 blank | 106 comment | 7 complexity | 0cc6189be41f7636f07891469f524e05 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  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. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord;
  18. use PhpOffice\PhpWord\Style\AbstractStyle;
  19. use PhpOffice\PhpWord\Style\Font;
  20. use PhpOffice\PhpWord\Style\Numbering;
  21. use PhpOffice\PhpWord\Style\Paragraph;
  22. use PhpOffice\PhpWord\Style\Table;
  23. /**
  24. * Style collection
  25. */
  26. class Style
  27. {
  28. /**
  29. * Style register
  30. *
  31. * @var array
  32. */
  33. private static $styles = array();
  34. /**
  35. * Add paragraph style
  36. *
  37. * @param string $styleName
  38. * @param array $styles
  39. * @return \PhpOffice\PhpWord\Style\Paragraph
  40. */
  41. public static function addParagraphStyle($styleName, $styles)
  42. {
  43. return self::setStyleValues($styleName, new Paragraph(), $styles);
  44. }
  45. /**
  46. * Add font style
  47. *
  48. * @param string $styleName
  49. * @param array $fontStyle
  50. * @param array $paragraphStyle
  51. * @return \PhpOffice\PhpWord\Style\Font
  52. */
  53. public static function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
  54. {
  55. return self::setStyleValues($styleName, new Font('text', $paragraphStyle), $fontStyle);
  56. }
  57. /**
  58. * Add link style
  59. *
  60. * @param string $styleName
  61. * @param array $styles
  62. * @return \PhpOffice\PhpWord\Style\Font
  63. */
  64. public static function addLinkStyle($styleName, $styles)
  65. {
  66. return self::setStyleValues($styleName, new Font('link'), $styles);
  67. }
  68. /**
  69. * Add numbering style
  70. *
  71. * @param string $styleName
  72. * @param array $styleValues
  73. * @return \PhpOffice\PhpWord\Style\Numbering
  74. * @since 0.10.0
  75. */
  76. public static function addNumberingStyle($styleName, $styleValues)
  77. {
  78. return self::setStyleValues($styleName, new Numbering(), $styleValues);
  79. }
  80. /**
  81. * Add title style
  82. *
  83. * @param int $depth
  84. * @param array $fontStyle
  85. * @param array $paragraphStyle
  86. * @return \PhpOffice\PhpWord\Style\Font
  87. */
  88. public static function addTitleStyle($depth, $fontStyle, $paragraphStyle = null)
  89. {
  90. return self::setStyleValues("Heading_{$depth}", new Font('title', $paragraphStyle), $fontStyle);
  91. }
  92. /**
  93. * Add table style
  94. *
  95. * @param string $styleName
  96. * @param array $styleTable
  97. * @param array|null $styleFirstRow
  98. * @return \PhpOffice\PhpWord\Style\Table
  99. */
  100. public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
  101. {
  102. return self::setStyleValues($styleName, new Table($styleTable, $styleFirstRow), null);
  103. }
  104. /**
  105. * Count styles
  106. *
  107. * @return int
  108. * @since 0.10.0
  109. */
  110. public static function countStyles()
  111. {
  112. return count(self::$styles);
  113. }
  114. /**
  115. * Reset styles
  116. * @since 0.10.0
  117. */
  118. public static function resetStyles()
  119. {
  120. self::$styles = array();
  121. }
  122. /**
  123. * Set default paragraph style
  124. *
  125. * @param array $styles Paragraph style definition
  126. * @return \PhpOffice\PhpWord\Style\Paragraph
  127. */
  128. public static function setDefaultParagraphStyle($styles)
  129. {
  130. return self::addParagraphStyle('Normal', $styles);
  131. }
  132. /**
  133. * Get all styles
  134. *
  135. * @return \PhpOffice\PhpWord\Style\AbstractStyle[]
  136. */
  137. public static function getStyles()
  138. {
  139. return self::$styles;
  140. }
  141. /**
  142. * Get style by name
  143. *
  144. * @param string $styleName
  145. * @return \PhpOffice\PhpWord\Style\AbstractStyle Paragraph|Font|Table|Numbering
  146. */
  147. public static function getStyle($styleName)
  148. {
  149. if (array_key_exists($styleName, self::$styles)) {
  150. return self::$styles[$styleName];
  151. } else {
  152. return null;
  153. }
  154. }
  155. /**
  156. * Set style values and put it to static style collection
  157. *
  158. * The $styleValues could be an array or object
  159. *
  160. * @param string $name
  161. * @param \PhpOffice\PhpWord\Style\AbstractStyle $style
  162. * @param array|\PhpOffice\PhpWord\Style\AbstractStyle $value
  163. * @return \PhpOffice\PhpWord\Style\AbstractStyle
  164. */
  165. private static function setStyleValues($name, $style, $value = null)
  166. {
  167. if (!array_key_exists($name, self::$styles)) {
  168. if ($value !== null) {
  169. if (is_array($value)) {
  170. $style->setStyleByArray($value);
  171. } elseif ($value instanceof AbstractStyle) {
  172. if (get_class($style) == get_class($value)) {
  173. $style = $value;
  174. }
  175. }
  176. }
  177. $style->setStyleName($name);
  178. $style->setIndex(self::countStyles() + 1); // One based index
  179. self::$styles[$name] = $style;
  180. }
  181. return self::getStyle($name);
  182. }
  183. }