Table.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. use PhpOffice\PhpWord\Style\Table as TableStyle;
  19. /**
  20. * Table element
  21. */
  22. class Table extends AbstractElement
  23. {
  24. /**
  25. * Table style
  26. *
  27. * @var \PhpOffice\PhpWord\Style\Table
  28. */
  29. private $style;
  30. /**
  31. * Table rows
  32. *
  33. * @var \PhpOffice\PhpWord\Element\Row[]
  34. */
  35. private $rows = array();
  36. /**
  37. * Table width
  38. *
  39. * @var int
  40. */
  41. private $width = null;
  42. /**
  43. * Create a new table
  44. *
  45. * @param mixed $style
  46. */
  47. public function __construct($style = null)
  48. {
  49. $this->style = $this->setNewStyle(new TableStyle(), $style);
  50. }
  51. /**
  52. * Add a row
  53. *
  54. * @param int $height
  55. * @param mixed $style
  56. * @return \PhpOffice\PhpWord\Element\Row
  57. */
  58. public function addRow($height = null, $style = null)
  59. {
  60. $row = new Row($height, $style);
  61. $row->setParentContainer($this);
  62. $this->rows[] = $row;
  63. return $row;
  64. }
  65. /**
  66. * Add a cell
  67. *
  68. * @param int $width
  69. * @param mixed $style
  70. * @return \PhpOffice\PhpWord\Element\Cell
  71. */
  72. public function addCell($width = null, $style = null)
  73. {
  74. $index = count($this->rows) - 1;
  75. $row = $this->rows[$index];
  76. $cell = $row->addCell($width, $style);
  77. return $cell;
  78. }
  79. /**
  80. * Get all rows
  81. *
  82. * @return \PhpOffice\PhpWord\Element\Row[]
  83. */
  84. public function getRows()
  85. {
  86. return $this->rows;
  87. }
  88. /**
  89. * Get table style
  90. *
  91. * @return \PhpOffice\PhpWord\Style\Table
  92. */
  93. public function getStyle()
  94. {
  95. return $this->style;
  96. }
  97. /**
  98. * Get table width
  99. *
  100. * @return int
  101. */
  102. public function getWidth()
  103. {
  104. return $this->width;
  105. }
  106. /**
  107. * Set table width.
  108. *
  109. * @param int $width
  110. */
  111. public function setWidth($width)
  112. {
  113. $this->width = $width;
  114. }
  115. /**
  116. * Get column count
  117. *
  118. * @return int
  119. */
  120. public function countColumns()
  121. {
  122. $columnCount = 0;
  123. $rowCount = count($this->rows);
  124. for ($i = 0; $i < $rowCount; $i++) {
  125. /** @var \PhpOffice\PhpWord\Element\Row $row Type hint */
  126. $row = $this->rows[$i];
  127. $cellCount = count($row->getCells());
  128. if ($columnCount < $cellCount) {
  129. $columnCount = $cellCount;
  130. }
  131. }
  132. return $columnCount;
  133. }
  134. /**
  135. * The first declared cell width for each column
  136. *
  137. * @return int[]
  138. */
  139. public function findFirstDefinedCellWidths()
  140. {
  141. $cellWidths = array();
  142. foreach ($this->rows as $row) {
  143. $cells = $row->getCells();
  144. if (count($cells) <= count($cellWidths)) {
  145. continue;
  146. }
  147. $cellWidths = array();
  148. foreach ($cells as $cell) {
  149. $cellWidths[] = $cell->getWidth();
  150. }
  151. }
  152. return $cellWidths;
  153. }
  154. }