Field.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 2019 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\RTF\Element;
  18. /**
  19. * Field element writer
  20. *
  21. * Note: for now, only date, page and numpages fields are implemented for RTF.
  22. */
  23. class Field extends Text
  24. {
  25. /**
  26. * Write field element.
  27. */
  28. public function write()
  29. {
  30. $element = $this->element;
  31. if (!$element instanceof \PhpOffice\PhpWord\Element\Field) {
  32. return;
  33. }
  34. $this->getStyles();
  35. $content = '';
  36. $content .= $this->writeOpening();
  37. $content .= '{';
  38. $content .= $this->writeFontStyle();
  39. $methodName = 'write' . ucfirst(strtolower($element->getType()));
  40. if (!method_exists($this, $methodName)) {
  41. // Unsupported field
  42. $content .= '';
  43. } else {
  44. $content .= '\\field{\\*\\fldinst ';
  45. $content .= $this->$methodName($element);
  46. $content .= '}{\\fldrslt}';
  47. }
  48. $content .= '}';
  49. $content .= $this->writeClosing();
  50. return $content;
  51. }
  52. protected function writePage()
  53. {
  54. return 'PAGE';
  55. }
  56. protected function writeNumpages()
  57. {
  58. return 'NUMPAGES';
  59. }
  60. protected function writeDate(\PhpOffice\PhpWord\Element\Field $element)
  61. {
  62. $content = '';
  63. $content .= 'DATE';
  64. $properties = $element->getProperties();
  65. if (isset($properties['dateformat'])) {
  66. $content .= ' \\\\@ "' . $properties['dateformat'] . '"';
  67. }
  68. return $content;
  69. }
  70. }