/** * Read meta.xml * * @param \PhpOffice\PhpWord\PhpWord $phpWord * @todo Process property type */ public function read(PhpWord &$phpWord) { $xmlReader = new XMLReader(); $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); $docProps = $phpWord->getDocumentProperties(); $metaNode = $xmlReader->getElement('office:meta'); // Standard properties $properties = array('title' => 'dc:title', 'subject' => 'dc:subject', 'description' => 'dc:description', 'keywords' => 'meta:keyword', 'creator' => 'meta:initial-creator', 'lastModifiedBy' => 'dc:creator'); foreach ($properties as $property => $path) { $method = "set{$property}"; $propertyNode = $xmlReader->getElement($path, $metaNode); if ($propertyNode !== null && method_exists($docProps, $method)) { $docProps->{$method}($propertyNode->nodeValue); } } // Custom properties $propertyNodes = $xmlReader->getElements('meta:user-defined', $metaNode); foreach ($propertyNodes as $propertyNode) { $property = $xmlReader->getAttribute('meta:name', $propertyNode); // Set category, company, and manager property if (in_array($property, array('Category', 'Company', 'Manager'))) { $method = "set{$property}"; $docProps->{$method}($propertyNode->nodeValue); // Set other custom properties } else { $docProps->setCustomProperty($property, $propertyNode->nodeValue); } } }
/** * Test get element returns null */ public function testGetElementReturnsNull() { $filename = __DIR__ . "/../_files/documents/reader.docx.zip"; $object = new XMLReader(); $object->getDomFromZip($filename, '[Content_Types].xml'); $element = $object->getElements('*')->item(0); $this->assertNull($object->getElement('yadayadaya', $element)); }
/** * Read custom document properties. * * @param \PhpOffice\PhpWord\PhpWord $phpWord * @return void */ public function read(PhpWord $phpWord) { $xmlReader = new XMLReader(); $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); $docProps = $phpWord->getDocInfo(); $nodes = $xmlReader->getElements('*'); if ($nodes->length > 0) { foreach ($nodes as $node) { $propertyName = $xmlReader->getAttribute('name', $node); $attributeNode = $xmlReader->getElement('*', $node); $attributeType = $attributeNode->nodeName; $attributeValue = $attributeNode->nodeValue; $attributeValue = DocInfo::convertProperty($attributeValue, $attributeType); $attributeType = DocInfo::convertPropertyType($attributeType); $docProps->setCustomProperty($propertyName, $attributeValue, $attributeType); } } }
/** * Read style definition * * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader * @param \DOMElement $parentNode * @param array $styleDefs * @ignoreScrutinizerPatch * @return array */ protected function readStyleDefs(XMLReader $xmlReader, \DOMElement $parentNode = null, $styleDefs = array()) { $styles = array(); foreach ($styleDefs as $styleProp => $styleVal) { @(list($method, $element, $attribute, $expected) = $styleVal); if ($xmlReader->elementExists($element, $parentNode)) { $node = $xmlReader->getElement($element, $parentNode); // Use w:val as default if no attribute assigned $attribute = $attribute === null ? 'w:val' : $attribute; $attributeValue = $xmlReader->getAttribute($attribute, $node); $styleValue = $this->readStyleDef($method, $attributeValue, $expected); if ($styleValue !== null) { $styles[$styleProp] = $styleValue; } } } return $styles; }
/** * Read w:rPr * * @return string|array|null */ protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode) { $style = null; // Hyperlink has an extra w:r child if ($domNode->nodeName == 'w:hyperlink') { $domNode = $xmlReader->getElement('w:r', $domNode); } if (is_null($domNode)) { return $style; } if ($xmlReader->elementExists('w:rPr', $domNode)) { if ($xmlReader->elementExists('w:rPr/w:rStyle', $domNode)) { $style = $xmlReader->getAttribute('w:val', $domNode, 'w:rPr/w:rStyle'); } else { $style = array(); $mapping = array('w:b' => 'bold', 'w:i' => 'italic', 'w:color' => 'color', 'w:strike' => 'strikethrough', 'w:u' => 'underline', 'w:highlight' => 'fgColor', 'w:sz' => 'size', 'w:rFonts' => 'name', 'w:vertAlign' => 'superScript'); $nodes = $xmlReader->getElements('w:rPr/*', $domNode); foreach ($nodes as $node) { if (!array_key_exists($node->nodeName, $mapping)) { continue; } $property = $mapping[$node->nodeName]; switch ($node->nodeName) { case 'w:rFonts': $style['name'] = $xmlReader->getAttribute('w:ascii', $node); $style['hint'] = $xmlReader->getAttribute('w:hint', $node); break; case 'w:b': case 'w:i': case 'w:strike': $style[$property] = true; break; case 'w:u': case 'w:highlight': case 'w:color': $style[$property] = $xmlReader->getAttribute('w:val', $node); break; case 'w:sz': $style[$property] = $xmlReader->getAttribute('w:val', $node) / 2; break; case 'w:vertAlign': $style[$property] = $xmlReader->getAttribute('w:val', $node); if ($style[$property] == 'superscript') { $style['superScript'] = true; } else { $style['superScript'] = false; $style['subScript'] = true; } break; } } } } return $style; }