DocPropsCustom.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\Word2007;
  18. use PhpOffice\PhpWord\Metadata\DocInfo;
  19. use PhpOffice\PhpWord\PhpWord;
  20. use PhpOffice\PhpWord\Shared\XMLReader;
  21. /**
  22. * Custom properties reader
  23. *
  24. * @since 0.11.0
  25. */
  26. class DocPropsCustom extends AbstractPart
  27. {
  28. /**
  29. * Read custom document properties.
  30. *
  31. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  32. */
  33. public function read(PhpWord $phpWord)
  34. {
  35. $xmlReader = new XMLReader();
  36. $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
  37. $docProps = $phpWord->getDocInfo();
  38. $nodes = $xmlReader->getElements('*');
  39. if ($nodes->length > 0) {
  40. foreach ($nodes as $node) {
  41. $propertyName = $xmlReader->getAttribute('name', $node);
  42. $attributeNode = $xmlReader->getElement('*', $node);
  43. $attributeType = $attributeNode->nodeName;
  44. $attributeValue = $attributeNode->nodeValue;
  45. $attributeValue = DocInfo::convertProperty($attributeValue, $attributeType);
  46. $attributeType = DocInfo::convertPropertyType($attributeType);
  47. $docProps->setCustomProperty($propertyName, $attributeValue, $attributeType);
  48. }
  49. }
  50. }
  51. }