/** * Test unit conversion functions with various numbers */ public function testAddHtml() { $content = ''; // Default $section = new Section(1); $this->assertCount(0, $section->getElements()); // Heading $styles = array('strong', 'em', 'sup', 'sub'); for ($level = 1; $level <= 6; $level++) { $content .= "<h{$level}>Heading {$level}</h{$level}>"; } // Styles $content .= '<p style="text-decoration: underline; text-decoration: line-through; ' . 'text-align: center; color: #999; background-color: #000;">'; foreach ($styles as $style) { $content .= "<{$style}>{$style}</{$style}>"; } $content .= '</p>'; // Add HTML Html::addHtml($section, $content); $this->assertCount(7, $section->getElements()); // Other parts $section = new Section(1); $content = ''; $content .= '<table><tr><th>Header</th><td>Content</td></tr></table>'; $content .= '<ul><li>Bullet</li><ul><li>Bullet</li></ul></ul>'; $content .= '<ol><li>Bullet</li></ol>'; $content .= "'Single Quoted Text'"; $content .= '"Double Quoted Text"'; $content .= '& Ampersand'; $content .= '<>“‘’«»‹›'; $content .= '&•°…™©®—'; $content .= '–   ²³¼½¾'; Html::addHtml($section, $content); }
/** * Test unit conversion functions with various numbers */ public function testAddHtml() { $content = ''; // Default $section = new Section(1); $this->assertEquals(0, $section->countElements()); // Heading $styles = array('strong', 'em', 'sup', 'sub'); for ($level = 1; $level <= 6; $level++) { $content .= "<h{$level}>Heading {$level}</h{$level}>"; } // Styles $content .= '<p style="text-decoration: underline; text-decoration: line-through; ' . 'text-align: center; color: #999; background-color: #000;">'; foreach ($styles as $style) { $content .= "<{$style}>{$style}</{$style}>"; } $content .= '</p>'; // Add HTML Html::addHtml($section, $content); $this->assertEquals(7, $section->countElements()); // Other parts $section = new Section(1); $content = ''; $content .= '<table><tr><th>Header</th><td>Content</td></tr></table>'; $content .= '<ul><li>Bullet</li><ul><li>Bullet</li></ul></ul>'; $content .= '<ol><li>Bullet</li></ol>'; Html::addHtml($section, $content); }
/** * Add header exception * * @expectedException Exception * @expectedExceptionMesssage Invalid header/footer type. */ public function testAddHeaderException() { $object = new Section(1); $header = $object->addHeader('ODD'); }
/** * Add header/footer content. * * @param \PhpOffice\PhpWord\Element\Section &$section * @param \PhpOffice\PhpWord\Shared\ZipArchive $zip * @param string $elmType header|footer * @param integer &$rId * @return void */ private function addHeaderFooterContent(Section &$section, ZipArchive $zip, $elmType, &$rId) { $getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters'; $elmCount = ($section->getSectionId() - 1) * 3; $elements = $section->{$getFunction}(); foreach ($elements as &$element) { /** @var \PhpOffice\PhpWord\Element\AbstractElement $element Type hint */ $elmCount++; $element->setRelationId(++$rId); $elmFile = "{$elmType}{$elmCount}.xml"; // e.g. footer1.xml $this->contentTypes['override']["/word/{$elmFile}"] = $elmType; $this->relationships[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId); /** @var \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart $writerPart Type hint */ $writerPart = $this->getWriterPart($elmType)->setElement($element); $zip->addFromString("word/{$elmFile}", $writerPart->write()); } }
/** * Create new section * * @param array $settings * @return \PhpOffice\PhpWord\Element\Section */ public function addSection($settings = null) { $section = new Section(count($this->sections) + 1, $settings); $section->setPhpWord($this); $this->sections[] = $section; return $section; }
/** * Read w:sectPr node * * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader * @param \DOMElement $node * @param \PhpOffice\PhpWord\Element\Section $section */ private function readWSectPrNode(XMLReader $xmlReader, \DOMElement $node, Section &$section) { $settings = $this->readSectionStyle($xmlReader, $node); $section->setSettings($settings); $this->readHeaderFooter($settings, $section); }
/** * Read paragraph. * * @param array $directives * @return void */ private function readParagraph($directives) { list($property, $value) = $directives; $this->textrun = $this->section->addTextRun(); $this->flags[$property] = $value; }
/** * Add header/footer content * * @param mixed $objZip * @param string $elmType * @param integer $rId */ private function addHeaderFooterContent(Section &$section, $objZip, $elmType, &$rId) { $getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters'; $writeFunction = $elmType == 'header' ? 'writeHeader' : 'writeFooter'; $elmCount = ($section->getSectionId() - 1) * 3; $elmObjects = $section->{$getFunction}(); foreach ($elmObjects as &$elmObject) { $elmCount++; $elmObject->setRelationId(++$rId); $elmFile = "{$elmType}{$elmCount}.xml"; $objZip->addFromString("word/{$elmFile}", $this->getWriterPart($elmType)->{$writeFunction}($elmObject)); $this->cTypes['override']["/word/{$elmFile}"] = $elmType; $this->docRels[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId); } }
/** * Write end section. * * @param \PhpOffice\Common\XMLWriter $xmlWriter * @param \PhpOffice\PhpWord\Element\Section $section * @return void */ private function writeSectionSettings(XMLWriter $xmlWriter, Section $section) { $xmlWriter->startElement('w:sectPr'); // Header reference foreach ($section->getHeaders() as $header) { $rId = $header->getRelationId(); $xmlWriter->startElement('w:headerReference'); $xmlWriter->writeAttribute('w:type', $header->getType()); $xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->endElement(); } // Footer reference foreach ($section->getFooters() as $footer) { $rId = $footer->getRelationId(); $xmlWriter->startElement('w:footerReference'); $xmlWriter->writeAttribute('w:type', $footer->getType()); $xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->endElement(); } // Different first page if ($section->hasDifferentFirstPage()) { $xmlWriter->startElement('w:titlePg'); $xmlWriter->endElement(); } // Section settings $styleWriter = new SectionStyleWriter($xmlWriter, $section->getStyle()); $styleWriter->write(); $xmlWriter->endElement(); // w:sectPr }