Нет описания

AbstractContainer.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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\Element;
  18. /**
  19. * Container abstract class
  20. *
  21. * @method Text addText(string $text, mixed $fStyle = null, mixed $pStyle = null)
  22. * @method TextRun addTextRun(mixed $pStyle = null)
  23. * @method Bookmark addBookmark(string $name)
  24. * @method Link addLink(string $target, string $text = null, mixed $fStyle = null, mixed $pStyle = null)
  25. * @method PreserveText addPreserveText(string $text, mixed $fStyle = null, mixed $pStyle = null)
  26. * @method void addTextBreak(int $count = 1, mixed $fStyle = null, mixed $pStyle = null)
  27. * @method ListItem addListItem(string $txt, int $depth = 0, mixed $font = null, mixed $list = null, mixed $para = null)
  28. * @method ListItemRun addListItemRun(int $depth = 0, mixed $listStyle = null, mixed $pStyle = null)
  29. * @method Footnote addFootnote(mixed $pStyle = null)
  30. * @method Endnote addEndnote(mixed $pStyle = null)
  31. * @method CheckBox addCheckBox(string $name, $text, mixed $fStyle = null, mixed $pStyle = null)
  32. * @method Title addTitle(string $text, int $depth = 1)
  33. * @method TOC addTOC(mixed $fontStyle = null, mixed $tocStyle = null, int $minDepth = 1, int $maxDepth = 9)
  34. *
  35. * @method PageBreak addPageBreak()
  36. * @method Table addTable(mixed $style = null)
  37. * @method Image addImage(string $source, mixed $style = null, bool $isWatermark = false)
  38. * @method Object addObject(string $source, mixed $style = null)
  39. * @method TextBox addTextBox(mixed $style = null)
  40. * @method Field addField(string $type = null, array $properties = array(), array $options = array())
  41. * @method Line addLine(mixed $lineStyle = null)
  42. * @method Shape addShape(string $type, mixed $style = null)
  43. * @method Chart addChart(string $type, array $categories, array $values, array $style = null)
  44. * @method FormField addFormField(string $type, mixed $fStyle = null, mixed $pStyle = null)
  45. * @method SDT addSDT(string $type)
  46. *
  47. * @since 0.10.0
  48. */
  49. abstract class AbstractContainer extends AbstractElement
  50. {
  51. /**
  52. * Elements collection
  53. *
  54. * @var array
  55. */
  56. protected $elements = array();
  57. /**
  58. * Container type Section|Header|Footer|Footnote|Endnote|Cell|TextRun|TextBox|ListItemRun
  59. *
  60. * @var string
  61. */
  62. protected $container;
  63. /**
  64. * Magic method to catch all 'addElement' variation
  65. *
  66. * This removes addText, addTextRun, etc. When adding new element, we have to
  67. * add the model in the class docblock with `@method`.
  68. *
  69. * Warning: This makes capitalization matters, e.g. addCheckbox or addcheckbox won't work.
  70. *
  71. * @param mixed $function
  72. * @param mixed $args
  73. * @return \PhpOffice\PhpWord\Element\AbstractElement
  74. */
  75. public function __call($function, $args)
  76. {
  77. $elements = array(
  78. 'Text', 'TextRun', 'Bookmark', 'Link', 'PreserveText', 'TextBreak',
  79. 'ListItem', 'ListItemRun', 'Table', 'Image', 'Object',
  80. 'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field',
  81. 'Line', 'Shape', 'Title', 'TOC', 'PageBreak',
  82. 'Chart', 'FormField', 'SDT'
  83. );
  84. $functions = array();
  85. foreach ($elements as $element) {
  86. $functions['add' . strtolower($element)] = $element;
  87. }
  88. // Run valid `add` command
  89. $function = strtolower($function);
  90. if (isset($functions[$function])) {
  91. $element = $functions[$function];
  92. // Special case for TextBreak
  93. // @todo Remove the `$count` parameter in 1.0.0 to make this element similiar to other elements?
  94. if ($element == 'TextBreak') {
  95. @list($count, $fontStyle, $paragraphStyle) = $args; // Suppress error
  96. if ($count === null) {
  97. $count = 1;
  98. }
  99. for ($i = 1; $i <= $count; $i++) {
  100. $this->addElement($element, $fontStyle, $paragraphStyle);
  101. }
  102. // All other elements
  103. } else {
  104. array_unshift($args, $element); // Prepend element name to the beginning of args array
  105. return call_user_func_array(array($this, 'addElement'), $args);
  106. }
  107. }
  108. return null;
  109. }
  110. /**
  111. * Add element
  112. *
  113. * Each element has different number of parameters passed
  114. *
  115. * @param string $elementName
  116. * @return \PhpOffice\PhpWord\Element\AbstractElement
  117. */
  118. protected function addElement($elementName)
  119. {
  120. $elementClass = __NAMESPACE__ . '\\' . $elementName;
  121. $this->checkValidity($elementName);
  122. // Get arguments
  123. $args = func_get_args();
  124. $withoutP = in_array($this->container, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun', 'Field'));
  125. if ($withoutP && ($elementName == 'Text' || $elementName == 'PreserveText')) {
  126. $args[3] = null; // Remove paragraph style for texts in textrun
  127. }
  128. // Create element using reflection
  129. $reflection = new \ReflectionClass($elementClass);
  130. $elementArgs = $args;
  131. array_shift($elementArgs); // Shift the $elementName off the beginning of array
  132. /** @var \PhpOffice\PhpWord\Element\AbstractElement $element Type hint */
  133. $element = $reflection->newInstanceArgs($elementArgs);
  134. // Set parent container
  135. $element->setParentContainer($this);
  136. $element->setElementIndex($this->countElements() + 1);
  137. $element->setElementId();
  138. $this->elements[] = $element;
  139. return $element;
  140. }
  141. /**
  142. * Get all elements
  143. *
  144. * @return array
  145. */
  146. public function getElements()
  147. {
  148. return $this->elements;
  149. }
  150. /**
  151. * Count elements
  152. *
  153. * @return int
  154. */
  155. public function countElements()
  156. {
  157. return count($this->elements);
  158. }
  159. /**
  160. * Check if a method is allowed for the current container
  161. *
  162. * @param string $method
  163. * @return bool
  164. * @throws \BadMethodCallException
  165. */
  166. private function checkValidity($method)
  167. {
  168. $generalContainers = array(
  169. 'Section', 'Header', 'Footer', 'Footnote', 'Endnote', 'Cell', 'TextRun', 'TextBox', 'ListItemRun',
  170. );
  171. $validContainers = array(
  172. 'Text' => $generalContainers,
  173. 'Bookmark' => $generalContainers,
  174. 'Link' => $generalContainers,
  175. 'TextBreak' => $generalContainers,
  176. 'Image' => $generalContainers,
  177. 'Object' => $generalContainers,
  178. 'Field' => $generalContainers,
  179. 'Line' => $generalContainers,
  180. 'Shape' => $generalContainers,
  181. 'FormField' => $generalContainers,
  182. 'SDT' => $generalContainers,
  183. 'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
  184. 'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
  185. 'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
  186. 'Table' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
  187. 'CheckBox' => array('Section', 'Header', 'Footer', 'Cell'),
  188. 'TextBox' => array('Section', 'Header', 'Footer', 'Cell'),
  189. 'Footnote' => array('Section', 'TextRun', 'Cell'),
  190. 'Endnote' => array('Section', 'TextRun', 'Cell'),
  191. 'PreserveText' => array('Header', 'Footer', 'Cell'),
  192. 'Title' => array('Section'),
  193. 'TOC' => array('Section'),
  194. 'PageBreak' => array('Section'),
  195. 'Chart' => array('Section'),
  196. );
  197. // Special condition, e.g. preservetext can only exists in cell when
  198. // the cell is located in header or footer
  199. $validSubcontainers = array(
  200. 'PreserveText' => array(array('Cell'), array('Header', 'Footer')),
  201. 'Footnote' => array(array('Cell', 'TextRun'), array('Section')),
  202. 'Endnote' => array(array('Cell', 'TextRun'), array('Section')),
  203. );
  204. // Check if a method is valid for current container
  205. if (isset($validContainers[$method])) {
  206. if (!in_array($this->container, $validContainers[$method])) {
  207. throw new \BadMethodCallException("Cannot add {$method} in {$this->container}.");
  208. }
  209. }
  210. // Check if a method is valid for current container, located in other container
  211. if (isset($validSubcontainers[$method])) {
  212. $rules = $validSubcontainers[$method];
  213. $containers = $rules[0];
  214. $allowedDocParts = $rules[1];
  215. foreach ($containers as $container) {
  216. if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) {
  217. throw new \BadMethodCallException("Cannot add {$method} in {$this->container}.");
  218. }
  219. }
  220. }
  221. return true;
  222. }
  223. /**
  224. * Add memory image element
  225. *
  226. * @param string $src
  227. * @param mixed $style
  228. * @return \PhpOffice\PhpWord\Element\Image
  229. * @deprecated 0.9.0
  230. * @codeCoverageIgnore
  231. */
  232. public function addMemoryImage($src, $style = null)
  233. {
  234. return $this->addImage($src, $style);
  235. }
  236. /**
  237. * Create textrun element
  238. *
  239. * @param mixed $paragraphStyle
  240. * @return \PhpOffice\PhpWord\Element\TextRun
  241. * @deprecated 0.10.0
  242. * @codeCoverageIgnore
  243. */
  244. public function createTextRun($paragraphStyle = null)
  245. {
  246. return $this->addTextRun($paragraphStyle);
  247. }
  248. /**
  249. * Create footnote element
  250. *
  251. * @param mixed $paragraphStyle
  252. * @return \PhpOffice\PhpWord\Element\Footnote
  253. * @deprecated 0.10.0
  254. * @codeCoverageIgnore
  255. */
  256. public function createFootnote($paragraphStyle = null)
  257. {
  258. return $this->addFootnote($paragraphStyle);
  259. }
  260. }