Meta.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Reader\ODText;
  18. use PhpOffice\PhpWord\PhpWord;
  19. use PhpOffice\PhpWord\Shared\XMLReader;
  20. /**
  21. * Meta reader
  22. *
  23. * @since 0.11.0
  24. */
  25. class Meta extends AbstractPart
  26. {
  27. /**
  28. * Read meta.xml.
  29. *
  30. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  31. * @todo Process property type
  32. */
  33. public function read(PhpWord $phpWord)
  34. {
  35. $xmlReader = new XMLReader();
  36. $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
  37. $docProps = $phpWord->getDocInfo();
  38. $metaNode = $xmlReader->getElement('office:meta');
  39. // Standard properties
  40. $properties = array(
  41. 'title' => 'dc:title',
  42. 'subject' => 'dc:subject',
  43. 'description' => 'dc:description',
  44. 'keywords' => 'meta:keyword',
  45. 'creator' => 'meta:initial-creator',
  46. 'lastModifiedBy' => 'dc:creator',
  47. // 'created' => 'meta:creation-date',
  48. // 'modified' => 'dc:date',
  49. );
  50. foreach ($properties as $property => $path) {
  51. $method = "set{$property}";
  52. $propertyNode = $xmlReader->getElement($path, $metaNode);
  53. if ($propertyNode !== null && method_exists($docProps, $method)) {
  54. $docProps->$method($propertyNode->nodeValue);
  55. }
  56. }
  57. // Custom properties
  58. $propertyNodes = $xmlReader->getElements('meta:user-defined', $metaNode);
  59. foreach ($propertyNodes as $propertyNode) {
  60. $property = $xmlReader->getAttribute('meta:name', $propertyNode);
  61. // Set category, company, and manager property
  62. if (in_array($property, array('Category', 'Company', 'Manager'))) {
  63. $method = "set{$property}";
  64. $docProps->$method($propertyNode->nodeValue);
  65. } else {
  66. // Set other custom properties
  67. $docProps->setCustomProperty($property, $propertyNode->nodeValue);
  68. }
  69. }
  70. }
  71. }