DocPropsCustom.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\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. * @return void
  33. */
  34. public function read(PhpWord $phpWord)
  35. {
  36. $xmlReader = new XMLReader();
  37. $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
  38. $docProps = $phpWord->getDocInfo();
  39. $nodes = $xmlReader->getElements('*');
  40. if ($nodes->length > 0) {
  41. foreach ($nodes as $node) {
  42. $propertyName = $xmlReader->getAttribute('name', $node);
  43. $attributeNode = $xmlReader->getElement('*', $node);
  44. $attributeType = $attributeNode->nodeName;
  45. $attributeValue = $attributeNode->nodeValue;
  46. $attributeValue = DocInfo::convertProperty($attributeValue, $attributeType);
  47. $attributeType = DocInfo::convertPropertyType($attributeType);
  48. $docProps->setCustomProperty($propertyName, $attributeValue, $attributeType);
  49. }
  50. }
  51. }
  52. }