AbstractContainer.php 12 KB

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