PageRenderTime 30ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Atomic Projects/vendor/phpoffice/phpword/src/PhpWord/Element/AbstractContainer.php

https://gitlab.com/imamul68e/137619_PHP31
PHP | 282 lines | 193 code | 9 blank | 80 comment | 6 complexity | 10a19ab1fdb76a2bf1373ecb29c7aaa1 MD5 | raw file
  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-2016 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. * @codeCoverageIgnore
  147. */
  148. public function getElements()
  149. {
  150. return $this->elements;
  151. }
  152. /**
  153. * Count elements
  154. *
  155. * @return int
  156. */
  157. public function countElements()
  158. {
  159. return count($this->elements);
  160. }
  161. /**
  162. * Check if a method is allowed for the current container
  163. *
  164. * @param string $method
  165. *
  166. * @return bool
  167. *
  168. * @throws \BadMethodCallException
  169. */
  170. private function checkValidity($method)
  171. {
  172. $generalContainers = array(
  173. 'Section', 'Header', 'Footer', 'Footnote', 'Endnote', 'Cell', 'TextRun', 'TextBox', 'ListItemRun',
  174. );
  175. $validContainers = array(
  176. 'Text' => $generalContainers,
  177. 'Bookmark' => $generalContainers,
  178. 'Link' => $generalContainers,
  179. 'TextBreak' => $generalContainers,
  180. 'Image' => $generalContainers,
  181. 'Object' => $generalContainers,
  182. 'Field' => $generalContainers,
  183. 'Line' => $generalContainers,
  184. 'Shape' => $generalContainers,
  185. 'FormField' => $generalContainers,
  186. 'SDT' => $generalContainers,
  187. 'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
  188. 'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
  189. 'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
  190. 'Table' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
  191. 'CheckBox' => array('Section', 'Header', 'Footer', 'Cell'),
  192. 'TextBox' => array('Section', 'Header', 'Footer', 'Cell'),
  193. 'Footnote' => array('Section', 'TextRun', 'Cell'),
  194. 'Endnote' => array('Section', 'TextRun', 'Cell'),
  195. 'PreserveText' => array('Header', 'Footer', 'Cell'),
  196. 'Title' => array('Section'),
  197. 'TOC' => array('Section'),
  198. 'PageBreak' => array('Section'),
  199. 'Chart' => array('Section'),
  200. );
  201. // Special condition, e.g. preservetext can only exists in cell when
  202. // the cell is located in header or footer
  203. $validSubcontainers = array(
  204. 'PreserveText' => array(array('Cell'), array('Header', 'Footer')),
  205. 'Footnote' => array(array('Cell', 'TextRun'), array('Section')),
  206. 'Endnote' => array(array('Cell', 'TextRun'), array('Section')),
  207. );
  208. // Check if a method is valid for current container
  209. if (isset($validContainers[$method])) {
  210. if (!in_array($this->container, $validContainers[$method])) {
  211. throw new \BadMethodCallException("Cannot add {$method} in {$this->container}.");
  212. }
  213. }
  214. // Check if a method is valid for current container, located in other container
  215. if (isset($validSubcontainers[$method])) {
  216. $rules = $validSubcontainers[$method];
  217. $containers = $rules[0];
  218. $allowedDocParts = $rules[1];
  219. foreach ($containers as $container) {
  220. if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) {
  221. throw new \BadMethodCallException("Cannot add {$method} in {$this->container}.");
  222. }
  223. }
  224. }
  225. return true;
  226. }
  227. /**
  228. * Create textrun element
  229. *
  230. * @deprecated 0.10.0
  231. *
  232. * @param mixed $paragraphStyle
  233. *
  234. * @return \PhpOffice\PhpWord\Element\TextRun
  235. *
  236. * @codeCoverageIgnore
  237. */
  238. public function createTextRun($paragraphStyle = null)
  239. {
  240. return $this->addTextRun($paragraphStyle);
  241. }
  242. /**
  243. * Create footnote element
  244. *
  245. * @deprecated 0.10.0
  246. *
  247. * @param mixed $paragraphStyle
  248. *
  249. * @return \PhpOffice\PhpWord\Element\Footnote
  250. *
  251. * @codeCoverageIgnore
  252. */
  253. public function createFootnote($paragraphStyle = null)
  254. {
  255. return $this->addFootnote($paragraphStyle);
  256. }
  257. }