Table.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Writer\ODText\Element;
  18. use PhpOffice\PhpWord\Element\Row as RowElement;
  19. use PhpOffice\PhpWord\Element\Table as TableElement;
  20. use PhpOffice\PhpWord\Shared\XMLWriter;
  21. /**
  22. * Table element writer
  23. *
  24. * @since 0.10.0
  25. */
  26. class Table extends AbstractElement
  27. {
  28. /**
  29. * Write element
  30. */
  31. public function write()
  32. {
  33. $xmlWriter = $this->getXmlWriter();
  34. $element = $this->getElement();
  35. if (!$element instanceof \PhpOffice\PhpWord\Element\Table) {
  36. return;
  37. }
  38. $rows = $element->getRows();
  39. $rowCount = count($rows);
  40. if ($rowCount > 0) {
  41. $xmlWriter->startElement('table:table');
  42. $xmlWriter->writeAttribute('table:name', $element->getElementId());
  43. $xmlWriter->writeAttribute('table:style', $element->getElementId());
  44. // Write columns
  45. $this->writeColumns($xmlWriter, $element);
  46. // Write rows
  47. foreach ($rows as $row) {
  48. $this->writeRow($xmlWriter, $row);
  49. }
  50. $xmlWriter->endElement(); // table:table
  51. }
  52. }
  53. /**
  54. * Write column.
  55. *
  56. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  57. * @param \PhpOffice\PhpWord\Element\Table $element
  58. */
  59. private function writeColumns(XMLWriter $xmlWriter, TableElement $element)
  60. {
  61. $colCount = $element->countColumns();
  62. for ($i = 0; $i < $colCount; $i++) {
  63. $xmlWriter->startElement('table:table-column');
  64. $xmlWriter->writeAttribute('table:style-name', $element->getElementId() . '.' . $i);
  65. $xmlWriter->endElement();
  66. }
  67. }
  68. /**
  69. * Write row.
  70. *
  71. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  72. * @param \PhpOffice\PhpWord\Element\Row $row
  73. */
  74. private function writeRow(XMLWriter $xmlWriter, RowElement $row)
  75. {
  76. $xmlWriter->startElement('table:table-row');
  77. /** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
  78. foreach ($row->getCells() as $cell) {
  79. $xmlWriter->startElement('table:table-cell');
  80. $xmlWriter->writeAttribute('office:value-type', 'string');
  81. $containerWriter = new Container($xmlWriter, $cell);
  82. $containerWriter->write();
  83. $xmlWriter->endElement(); // table:table-cell
  84. }
  85. $xmlWriter->endElement(); // table:table-row
  86. }
  87. }