暫無描述

Field.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\Word2007\Element;
  18. /**
  19. * Field element writer
  20. *
  21. * @since 0.11.0
  22. */
  23. class Field extends Text
  24. {
  25. /**
  26. * Write field element.
  27. *
  28. * @return void
  29. */
  30. public function write()
  31. {
  32. $xmlWriter = $this->getXmlWriter();
  33. $element = $this->getElement();
  34. if (!$element instanceof \PhpOffice\PhpWord\Element\Field) {
  35. return;
  36. }
  37. $instruction = ' ' . $element->getType() . ' ';
  38. $properties = $element->getProperties();
  39. foreach ($properties as $propkey => $propval) {
  40. switch ($propkey) {
  41. case 'format':
  42. case 'numformat':
  43. $instruction .= '\* ' . $propval . ' ';
  44. break;
  45. case 'dateformat':
  46. $instruction .= '\@ "' . $propval . '" ';
  47. break;
  48. }
  49. }
  50. $options = $element->getOptions();
  51. foreach ($options as $option) {
  52. switch ($option) {
  53. case 'PreserveFormat':
  54. $instruction .= '\* MERGEFORMAT ';
  55. break;
  56. case 'LunarCalendar':
  57. $instruction .= '\h ';
  58. break;
  59. case 'SakaEraCalendar':
  60. $instruction .= '\s ';
  61. break;
  62. case 'LastUsedFormat':
  63. $instruction .= '\l ';
  64. break;
  65. }
  66. }
  67. $this->startElementP();
  68. $xmlWriter->startElement('w:fldSimple');
  69. $xmlWriter->writeAttribute('w:instr', $instruction);
  70. $xmlWriter->startElement('w:r');
  71. $xmlWriter->startElement('w:rPr');
  72. $xmlWriter->startElement('w:noProof');
  73. $xmlWriter->endElement(); // w:noProof
  74. $xmlWriter->endElement(); // w:rPr
  75. $xmlWriter->startElement('w:t');
  76. $xmlWriter->writeRaw('1');
  77. $xmlWriter->endElement(); // w:t
  78. $xmlWriter->endElement(); // w:r
  79. $xmlWriter->endElement(); // w:fldSimple
  80. $this->endElementP(); // w:p
  81. }
  82. }