merge/add test/cleanup

This commit is contained in:
troosan 2018-03-25 22:46:50 +02:00
parent 9b722a5b0c
commit 566e625b85
8 changed files with 73 additions and 200 deletions

View File

@ -61,7 +61,7 @@
"php": "^5.3.3 || ^7.0", "php": "^5.3.3 || ^7.0",
"ext-xml": "*", "ext-xml": "*",
"zendframework/zend-escaper": "^2.2", "zendframework/zend-escaper": "^2.2",
"phpoffice/common": "^0.2" "phpoffice/common": "dev-develop"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^4.8.36 || ^5.0", "phpunit/phpunit": "^4.8.36 || ^5.0",

View File

@ -35,7 +35,7 @@ namespace PhpOffice\PhpWord\Element;
* @method TOC addTOC(mixed $fontStyle = null, mixed $tocStyle = null, int $minDepth = 1, int $maxDepth = 9) * @method TOC addTOC(mixed $fontStyle = null, mixed $tocStyle = null, int $minDepth = 1, int $maxDepth = 9)
* @method PageBreak addPageBreak() * @method PageBreak addPageBreak()
* @method Table addTable(mixed $style = null) * @method Table addTable(mixed $style = null)
* @method Image addImage(string $source, mixed $style = null, bool $isWatermark = false) * @method Image addImage(string $source, mixed $style = null, bool $isWatermark = false, $name = null)
* @method OLEObject addOLEObject(string $source, mixed $style = null) * @method OLEObject addOLEObject(string $source, mixed $style = null)
* @method TextBox addTextBox(mixed $style = null) * @method TextBox addTextBox(mixed $style = null)
* @method Field addField(string $type = null, array $properties = array(), array $options = array(), mixed $text = null) * @method Field addField(string $type = null, array $properties = array(), array $options = array(), mixed $text = null)

View File

@ -134,6 +134,7 @@ class Image extends AbstractElement
* @param string $source * @param string $source
* @param mixed $style * @param mixed $style
* @param bool $watermark * @param bool $watermark
* @param string $name
* *
* @throws \PhpOffice\PhpWord\Exception\InvalidImageException * @throws \PhpOffice\PhpWord\Exception\InvalidImageException
* @throws \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException * @throws \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException
@ -141,9 +142,9 @@ class Image extends AbstractElement
public function __construct($source, $style = null, $watermark = false, $name = null) public function __construct($source, $style = null, $watermark = false, $name = null)
{ {
$this->source = $source; $this->source = $source;
$this->setIsWatermark($watermark);
$this->style = $this->setNewStyle(new ImageStyle(), $style, true); $this->style = $this->setNewStyle(new ImageStyle(), $style, true);
$this->name = $name; $this->setIsWatermark($watermark);
$this->setName($name);
$this->checkImage(); $this->checkImage();
} }
@ -178,6 +179,16 @@ class Image extends AbstractElement
return $this->sourceType; return $this->sourceType;
} }
/**
* Sets the image name
*
* @param string $value
*/
public function setName($value)
{
$this->name = $value;
}
/** /**
* Get image name * Get image name
* *

View File

@ -261,7 +261,12 @@ abstract class AbstractPart
$parent->addImage($imageSource); $parent->addImage($imageSource);
} }
} elseif ($node->nodeName == 'w:drawing') { } elseif ($node->nodeName == 'w:drawing') {
// Office 2011 Images // Office 2011 Image
$xmlReader->registerNamespace('wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing');
$xmlReader->registerNamespace('r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
$xmlReader->registerNamespace('pic', 'http://schemas.openxmlformats.org/drawingml/2006/picture');
$xmlReader->registerNamespace('a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
$name = $xmlReader->getAttribute('name', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:nvPicPr/pic:cNvPr'); $name = $xmlReader->getAttribute('name', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:nvPicPr/pic:cNvPr');
$embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip'); $embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip');
$target = $this->getMediaTarget($docPart, $embedId); $target = $this->getMediaTarget($docPart, $embedId);
@ -573,6 +578,8 @@ abstract class AbstractPart
return $possibleAttribute; return $possibleAttribute;
} }
} }
return null;
} }
return $attributes; return $attributes;

View File

@ -1,195 +0,0 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Shared;
use PhpOffice\PhpWord\Exception\Exception;
/**
* XML Reader wrapper
*
* @since 0.10.0
*/
class XMLReader
{
/**
* DOMDocument object
*
* @var \DOMDocument
*/
private $dom = null;
/**
* DOMXpath object
*
* @var \DOMXpath
*/
private $xpath = null;
/**
* Get DOMDocument from ZipArchive
*
* @param string $zipFile
* @param string $xmlFile
* @return \DOMDocument|false
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function getDomFromZip($zipFile, $xmlFile)
{
if (file_exists($zipFile) === false) {
throw new Exception('Cannot find archive file.');
}
$zip = new ZipArchive();
$zip->open($zipFile);
$content = $zip->getFromName($xmlFile);
$zip->close();
if ($content === false) {
return false;
} else {
return $this->getDomFromString($content);
}
}
/**
* Get DOMDocument from content string
*
* @param string $content
* @return \DOMDocument
*/
public function getDomFromString($content)
{
$this->dom = new \DOMDocument();
$this->dom->loadXML($content);
return $this->dom;
}
/**
* Get elements
*
* @param string $path
* @param \DOMElement $contextNode
* @return \DOMNodeList
*/
public function getElements($path, \DOMElement $contextNode = null)
{
if ($this->dom === null) {
return array();
}
if ($this->xpath === null) {
$this->xpath = new \DOMXpath($this->dom);
// GT Mod - required for reading images
$this->xpath->registerNamespace('a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
$this->xpath->registerNamespace('pic', 'http://schemas.openxmlformats.org/drawingml/2006/picture');
}
if (is_null($contextNode)) {
return $this->xpath->query($path);
} else {
return $this->xpath->query($path, $contextNode);
}
}
/**
* Get element
*
* @param string $path
* @param \DOMElement $contextNode
* @return \DOMElement|null
*/
public function getElement($path, \DOMElement $contextNode = null)
{
$elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) {
return $elements->item(0);
} else {
return null;
}
}
/**
* Get element attribute
*
* @param string $attribute
* @param \DOMElement $contextNode
* @param string $path
* @return string|null
*/
public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null)
{
$return = null;
if ($path !== null) {
$elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) {
/** @var \DOMElement $node Type hint */
$node = $elements->item(0);
$return = $node->getAttribute($attribute);
}
} else {
if ($contextNode !== null) {
$return = $contextNode->getAttribute($attribute);
}
}
return ($return == '') ? null : $return;
}
/**
* Get element value
*
* @param string $path
* @param \DOMElement $contextNode
* @return string|null
*/
public function getValue($path, \DOMElement $contextNode = null)
{
$elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) {
return $elements->item(0)->nodeValue;
} else {
return null;
}
}
/**
* Count elements
*
* @param string $path
* @param \DOMElement $contextNode
* @return integer
*/
public function countElements($path, \DOMElement $contextNode = null)
{
$elements = $this->getElements($path, $contextNode);
return $elements->length;
}
/**
* Element exists
*
* @param string $path
* @param \DOMElement $contextNode
* @return boolean
*/
public function elementExists($path, \DOMElement $contextNode = null)
{
return $this->getElements($path, $contextNode)->length > 0;
}
}

View File

@ -236,4 +236,40 @@ class ElementTest extends AbstractTestReader
$this->assertEquals('Title', $formattedTitle->getStyle()); $this->assertEquals('Title', $formattedTitle->getStyle());
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $formattedTitle->getText()); $this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $formattedTitle->getText());
} }
/**
* Test reading Drawing
*/
public function testReadDrawing()
{
$documentXml = '<w:p>
<w:r>
<w:drawing xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="5727700" cy="6621145"/>
<wp:docPr id="1" name="Picture 1"/>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="1" name="file_name.jpg"/>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId4" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
</a:blip>
</pic:blipFill>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>
</w:p>';
$phpWord = $this->getDocumentFromString(array('document' => $documentXml));
$elements = $phpWord->getSection(0)->getElements();
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);
}
} }

View File

@ -64,4 +64,18 @@ class Word2007Test extends \PHPUnit\Framework\TestCase
$doc = TestHelperDOCX::getDocument($phpWord); $doc = TestHelperDOCX::getDocument($phpWord);
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b')); $this->assertFalse($doc->elementExists('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b'));
} }
/**
* Load a Word 2011 file
*/
public function testLoadWord2011()
{
$filename = __DIR__ . '/../_files/documents/reader-2011.docx';
$phpWord = IOFactory::load($filename);
$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord);
$doc = TestHelperDOCX::getDocument($phpWord);
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[3]/w:r/w:pict/v:shape/v:imagedata'));
}
} }

Binary file not shown.