Field.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // Not fully implemented
  18. // - supports only PAGE and NUMPAGES
  19. // - supports only default formats and options
  20. // - supports style only if specified by name
  21. // - spaces before and after field may be dropped
  22. namespace PhpOffice\PhpWord\Writer\ODText\Element;
  23. /**
  24. * Field element writer
  25. *
  26. * @since 0.11.0
  27. */
  28. class Field extends Text
  29. {
  30. /**
  31. * Write field element.
  32. */
  33. public function write()
  34. {
  35. $element = $this->getElement();
  36. if (!$element instanceof \PhpOffice\PhpWord\Element\Field) {
  37. return;
  38. }
  39. $type = strtolower($element->getType());
  40. switch ($type) {
  41. case 'date':
  42. case 'page':
  43. case 'numpages':
  44. $this->writeDefault($element, $type);
  45. break;
  46. }
  47. }
  48. private function writeDefault(\PhpOffice\PhpWord\Element\Field $element, $type)
  49. {
  50. $xmlWriter = $this->getXmlWriter();
  51. $xmlWriter->startElement('text:span');
  52. if (method_exists($element, 'getFontStyle')) {
  53. $fstyle = $element->getFontStyle();
  54. if (is_string($fstyle)) {
  55. $xmlWriter->writeAttribute('text:style-name', $fstyle);
  56. }
  57. }
  58. switch ($type) {
  59. case 'date':
  60. $xmlWriter->startElement('text:date');
  61. $xmlWriter->writeAttribute('text:fixed', 'false');
  62. $xmlWriter->endElement();
  63. break;
  64. case 'page':
  65. $xmlWriter->startElement('text:page-number');
  66. $xmlWriter->writeAttribute('text:fixed', 'false');
  67. $xmlWriter->endElement();
  68. break;
  69. case 'numpages':
  70. $xmlWriter->startElement('text:page-count');
  71. $xmlWriter->endElement();
  72. break;
  73. }
  74. $xmlWriter->endElement(); // text:span
  75. }
  76. }