ListItem.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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\Style;
  18. use PhpOffice\PhpWord\Style;
  19. /**
  20. * List item style
  21. *
  22. * Before version 0.10.0, numbering style is defined statically with $listType.
  23. * After version 0.10.0, numbering style is defined by using Numbering and
  24. * recorded by $numStyle. $listStyle is maintained for backward compatility
  25. */
  26. class ListItem extends AbstractStyle
  27. {
  28. const TYPE_SQUARE_FILLED = 1;
  29. const TYPE_BULLET_FILLED = 3; // default
  30. const TYPE_BULLET_EMPTY = 5;
  31. const TYPE_NUMBER = 7;
  32. const TYPE_NUMBER_NESTED = 8;
  33. const TYPE_ALPHANUM = 9;
  34. /**
  35. * Legacy list type
  36. *
  37. * @var int
  38. */
  39. private $listType;
  40. /**
  41. * Numbering style name
  42. *
  43. * @var string
  44. * @since 0.10.0
  45. */
  46. private $numStyle;
  47. /**
  48. * Numbering definition instance ID
  49. *
  50. * @var int
  51. * @since 0.10.0
  52. */
  53. private $numId;
  54. /**
  55. * Create new instance
  56. *
  57. * @param string $numStyle
  58. */
  59. public function __construct($numStyle = null)
  60. {
  61. if ($numStyle !== null) {
  62. $this->setNumStyle($numStyle);
  63. } else {
  64. $this->setListType();
  65. }
  66. }
  67. /**
  68. * Get List Type
  69. *
  70. * @return int
  71. */
  72. public function getListType()
  73. {
  74. return $this->listType;
  75. }
  76. /**
  77. * Set legacy list type for version < 0.10.0
  78. *
  79. * @param int $value
  80. * @return self
  81. */
  82. public function setListType($value = self::TYPE_BULLET_FILLED)
  83. {
  84. $enum = array(
  85. self::TYPE_SQUARE_FILLED, self::TYPE_BULLET_FILLED,
  86. self::TYPE_BULLET_EMPTY, self::TYPE_NUMBER,
  87. self::TYPE_NUMBER_NESTED, self::TYPE_ALPHANUM,
  88. );
  89. $this->listType = $this->setEnumVal($value, $enum, $this->listType);
  90. $this->getListTypeStyle();
  91. return $this;
  92. }
  93. /**
  94. * Get numbering style name
  95. *
  96. * @return string
  97. */
  98. public function getNumStyle()
  99. {
  100. return $this->numStyle;
  101. }
  102. /**
  103. * Set numbering style name
  104. *
  105. * @param string $value
  106. * @return self
  107. */
  108. public function setNumStyle($value)
  109. {
  110. $this->numStyle = $value;
  111. $numStyleObject = Style::getStyle($this->numStyle);
  112. if ($numStyleObject instanceof Numbering) {
  113. $this->numId = $numStyleObject->getIndex();
  114. $numStyleObject->setNumId($this->numId);
  115. }
  116. return $this;
  117. }
  118. /**
  119. * Get numbering Id
  120. *
  121. * @return int
  122. */
  123. public function getNumId()
  124. {
  125. return $this->numId;
  126. }
  127. /**
  128. * Set numbering Id. Same numId means same list
  129. * @param mixed $numInt
  130. */
  131. public function setNumId($numInt)
  132. {
  133. $this->numId = $numInt;
  134. $this->getListTypeStyle();
  135. }
  136. /**
  137. * Get legacy numbering definition
  138. *
  139. * @return array
  140. * @since 0.10.0
  141. */
  142. private function getListTypeStyle()
  143. {
  144. // Check if legacy style already registered in global Style collection
  145. $numStyle = 'PHPWordListType' . $this->listType;
  146. if ($this->numId) {
  147. $numStyle .= 'NumId' . $this->numId;
  148. }
  149. if (Style::getStyle($numStyle) !== null) {
  150. $this->setNumStyle($numStyle);
  151. return;
  152. }
  153. // Property mapping for numbering level information
  154. $properties = array('start', 'format', 'text', 'alignment', 'tabPos', 'left', 'hanging', 'font', 'hint');
  155. // Legacy level information
  156. $listTypeStyles = array(
  157. self::TYPE_SQUARE_FILLED => array(
  158. 'type' => 'hybridMultilevel',
  159. 'levels' => array(
  160. 0 => '1, bullet, , left, 720, 720, 360, Wingdings, default',
  161. 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default',
  162. 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default',
  163. 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default',
  164. 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default',
  165. 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default',
  166. 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default',
  167. 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default',
  168. 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default',
  169. ),
  170. ),
  171. self::TYPE_BULLET_FILLED => array(
  172. 'type' => 'hybridMultilevel',
  173. 'levels' => array(
  174. 0 => '1, bullet, , left, 720, 720, 360, Symbol, default',
  175. 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default',
  176. 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default',
  177. 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default',
  178. 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default',
  179. 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default',
  180. 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default',
  181. 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default',
  182. 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default',
  183. ),
  184. ),
  185. self::TYPE_BULLET_EMPTY => array(
  186. 'type' => 'hybridMultilevel',
  187. 'levels' => array(
  188. 0 => '1, bullet, o, left, 720, 720, 360, Courier New, default',
  189. 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default',
  190. 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default',
  191. 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default',
  192. 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default',
  193. 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default',
  194. 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default',
  195. 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default',
  196. 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default',
  197. ),
  198. ),
  199. self::TYPE_NUMBER => array(
  200. 'type' => 'hybridMultilevel',
  201. 'levels' => array(
  202. 0 => '1, decimal, %1., left, 720, 720, 360, , default',
  203. 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default',
  204. 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default',
  205. 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default',
  206. 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default',
  207. 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default',
  208. 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default',
  209. 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default',
  210. 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default',
  211. ),
  212. ),
  213. self::TYPE_NUMBER_NESTED => array(
  214. 'type' => 'multilevel',
  215. 'levels' => array(
  216. 0 => '1, decimal, %1., left, 360, 360, 360, , ',
  217. 1 => '1, decimal, %1.%2., left, 792, 792, 432, , ',
  218. 2 => '1, decimal, %1.%2.%3., left, 1224, 1224, 504, , ',
  219. 3 => '1, decimal, %1.%2.%3.%4., left, 1800, 1728, 648, , ',
  220. 4 => '1, decimal, %1.%2.%3.%4.%5., left, 2520, 2232, 792, , ',
  221. 5 => '1, decimal, %1.%2.%3.%4.%5.%6., left, 2880, 2736, 936, , ',
  222. 6 => '1, decimal, %1.%2.%3.%4.%5.%6.%7., left, 3600, 3240, 1080, , ',
  223. 7 => '1, decimal, %1.%2.%3.%4.%5.%6.%7.%8., left, 3960, 3744, 1224, , ',
  224. 8 => '1, decimal, %1.%2.%3.%4.%5.%6.%7.%8.%9., left, 4680, 4320, 1440, , ',
  225. ),
  226. ),
  227. self::TYPE_ALPHANUM => array(
  228. 'type' => 'multilevel',
  229. 'levels' => array(
  230. 0 => '1, decimal, %1., left, 720, 720, 360, , ',
  231. 1 => '1, lowerLetter, %2., left, 1440, 1440, 360, , ',
  232. 2 => '1, lowerRoman, %3., right, 2160, 2160, 180, , ',
  233. 3 => '1, decimal, %4., left, 2880, 2880, 360, , ',
  234. 4 => '1, lowerLetter, %5., left, 3600, 3600, 360, , ',
  235. 5 => '1, lowerRoman, %6., right, 4320, 4320, 180, , ',
  236. 6 => '1, decimal, %7., left, 5040, 5040, 360, , ',
  237. 7 => '1, lowerLetter, %8., left, 5760, 5760, 360, , ',
  238. 8 => '1, lowerRoman, %9., right, 6480, 6480, 180, , ',
  239. ),
  240. ),
  241. );
  242. // Populate style and register to global Style register
  243. $style = $listTypeStyles[$this->listType];
  244. $numProperties = count($properties);
  245. foreach ($style['levels'] as $key => $value) {
  246. $level = array();
  247. $levelProperties = explode(', ', $value);
  248. $level['level'] = $key;
  249. for ($i = 0; $i < $numProperties; $i++) {
  250. $property = $properties[$i];
  251. $level[$property] = $levelProperties[$i];
  252. }
  253. $style['levels'][$key] = $level;
  254. }
  255. Style::addNumberingStyle($numStyle, $style);
  256. $this->setNumStyle($numStyle);
  257. }
  258. }