Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table

This commit is contained in:
Ivan Lanin 2014-04-08 16:09:31 +07:00
parent 6456255300
commit 2cdad4b247
25 changed files with 762 additions and 520 deletions

View File

@ -25,10 +25,9 @@ This release marked heavy refactorings on internal code structure with the creat
- CheckBox: Ability to add checkbox in header/footer - @ivanlanin GH-187 - CheckBox: Ability to add checkbox in header/footer - @ivanlanin GH-187
- Link: Ability to add link in header/footer - @ivanlanin GH-187 - Link: Ability to add link in header/footer - @ivanlanin GH-187
- Object: Ability to add object in header, footer, textrun, and footnote - @ivanlanin GH-187 - Object: Ability to add object in header, footer, textrun, and footnote - @ivanlanin GH-187
- Media: Add `Media::reset()` to reset all media data - @juzi GH-19 - Media: Add `Media::resetElements()` to reset all media data - @juzi GH-19
- Style: Add `Style::reset()` to reset all styles - General: Add `Style::resetStyles()`, `Footnote::resetElements()`, and `TOC::resetTitles()` - @ivanlanin GH-187
- Footnote: Add `Footnote::reset()` to reset all footnotes - Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table - @ivanlanin
- TOC: Add `TOC::reset()` to reset all TOC
### Bugfixes ### Bugfixes

View File

@ -138,7 +138,7 @@ class DocumentProperties
* Set Creator * Set Creator
* *
* @param string $pValue * @param string $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setCreator($pValue = '') public function setCreator($pValue = '')
{ {
@ -160,7 +160,7 @@ class DocumentProperties
* Set Last Modified By * Set Last Modified By
* *
* @param string $pValue * @param string $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setLastModifiedBy($pValue = '') public function setLastModifiedBy($pValue = '')
{ {
@ -182,7 +182,7 @@ class DocumentProperties
* Set Created * Set Created
* *
* @param int $pValue * @param int $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setCreated($pValue = null) public function setCreated($pValue = null)
{ {
@ -207,7 +207,7 @@ class DocumentProperties
* Set Modified * Set Modified
* *
* @param int $pValue * @param int $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setModified($pValue = null) public function setModified($pValue = null)
{ {
@ -232,7 +232,7 @@ class DocumentProperties
* Set Title * Set Title
* *
* @param string $pValue * @param string $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setTitle($pValue = '') public function setTitle($pValue = '')
{ {
@ -254,7 +254,7 @@ class DocumentProperties
* Set Description * Set Description
* *
* @param string $pValue * @param string $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setDescription($pValue = '') public function setDescription($pValue = '')
{ {
@ -276,7 +276,7 @@ class DocumentProperties
* Set Subject * Set Subject
* *
* @param string $pValue * @param string $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setSubject($pValue = '') public function setSubject($pValue = '')
{ {
@ -298,7 +298,7 @@ class DocumentProperties
* Set Keywords * Set Keywords
* *
* @param string $pValue * @param string $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setKeywords($pValue = '') public function setKeywords($pValue = '')
{ {
@ -320,7 +320,7 @@ class DocumentProperties
* Set Category * Set Category
* *
* @param string $pValue * @param string $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setCategory($pValue = '') public function setCategory($pValue = '')
{ {
@ -342,7 +342,7 @@ class DocumentProperties
* Set Company * Set Company
* *
* @param string $pValue * @param string $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setCompany($pValue = '') public function setCompany($pValue = '')
{ {
@ -364,7 +364,7 @@ class DocumentProperties
* Set Manager * Set Manager
* *
* @param string $pValue * @param string $pValue
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setManager($pValue = '') public function setManager($pValue = '')
{ {
@ -432,7 +432,7 @@ class DocumentProperties
* 's': String * 's': String
* 'd': Date/Time * 'd': Date/Time
* 'b': Boolean * 'b': Boolean
* @return \PhpOffice\PhpWord\DocumentProperties * @return self
*/ */
public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null) public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null)
{ {

View File

@ -59,7 +59,7 @@ abstract class AbstractElement
* *
* @var string * @var string
*/ */
private $docPart = 'section'; protected $docPart = 'section';
/** /**
* Document part Id * Document part Id
@ -70,7 +70,7 @@ abstract class AbstractElement
* *
* @var integer * @var integer
*/ */
private $docPartId = 1; protected $docPartId = 1;
/** /**
* Elements collection * Elements collection
@ -84,7 +84,7 @@ abstract class AbstractElement
* *
* @var int * @var int
*/ */
private $relationId; protected $relationId;
/** /**
* Add text element * Add text element
@ -144,8 +144,8 @@ abstract class AbstractElement
$link = new Link(String::toUTF8($linkSrc), String::toUTF8($linkName), $fontStyle, $paragraphStyle); $link = new Link(String::toUTF8($linkSrc), String::toUTF8($linkName), $fontStyle, $paragraphStyle);
$link->setDocPart($this->getDocPart(), $this->getDocPartId()); $link->setDocPart($this->getDocPart(), $this->getDocPartId());
$rID = Media::addElement($elementDocPart, 'link', $linkSrc); $rId = Media::addElement($elementDocPart, 'link', $linkSrc);
$link->setRelationId($rID); $link->setRelationId($rId);
$this->elements[] = $link; $this->elements[] = $link;
return $link; return $link;
@ -271,8 +271,8 @@ abstract class AbstractElement
$image = new Image($src, $style, $isWatermark); $image = new Image($src, $style, $isWatermark);
$image->setDocPart($this->getDocPart(), $this->getDocPartId()); $image->setDocPart($this->getDocPart(), $this->getDocPartId());
$rID = Media::addElement($elementDocPart, 'image', $src, $image); $rId = Media::addElement($elementDocPart, 'image', $src, $image);
$image->setRelationId($rID); $image->setRelationId($rId);
$this->elements[] = $image; $this->elements[] = $image;
return $image; return $image;
} }
@ -301,10 +301,10 @@ abstract class AbstractElement
$ext = substr($ext, 0, -1); $ext = substr($ext, 0, -1);
} }
$icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png"); $icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png");
$rID = Media::addElement($elementDocPart, 'object', $src); $rId = Media::addElement($elementDocPart, 'object', $src);
$object->setRelationId($rID); $object->setRelationId($rId);
$rIDimg = Media::addElement($elementDocPart, 'image', $icon, new Image($icon)); $rIdimg = Media::addElement($elementDocPart, 'image', $icon, new Image($icon));
$object->setImageRelationId($rIDimg); $object->setImageRelationId($rIdimg);
$this->elements[] = $object; $this->elements[] = $object;
return $object; return $object;
} else { } else {
@ -323,9 +323,10 @@ abstract class AbstractElement
$this->checkValidity('footnote'); $this->checkValidity('footnote');
$footnote = new FootnoteElement($paragraphStyle); $footnote = new FootnoteElement($paragraphStyle);
$refID = FootnoteCollection::addFootnoteElement($footnote); $rId = FootnoteCollection::addFootnoteElement($footnote);
$footnote->setDocPart('footnote', $this->getDocPartId()); $footnote->setDocPart('footnote', $this->getDocPartId());
$footnote->setRelationId($refID); $footnote->setRelationId($rId);
$this->elements[] = $footnote; $this->elements[] = $footnote;
return $footnote; return $footnote;

View File

@ -59,12 +59,12 @@ class Footnote extends AbstractElement
/** /**
* Set Footnote Reference ID * Set Footnote Reference ID
* *
* @param int $refId * @param int $rId
* @deprecated 0.9.2 * @deprecated 0.9.2
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */
public function setReferenceId($refId) public function setReferenceId($rId)
{ {
$this->setRelationId($refId); $this->setRelationId($rId);
} }
} }

View File

@ -80,8 +80,8 @@ class Image extends AbstractElement
* @param string $source * @param string $source
* @param mixed $style * @param mixed $style
* @param boolean $isWatermark * @param boolean $isWatermark
* @throws \PhpOffice\PhpWord\Exception\InvalidImageException * @throws InvalidImageException
* @throws \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException * @throws UnsupportedImageTypeException
*/ */
public function __construct($source, $style = null, $isWatermark = false) public function __construct($source, $style = null, $isWatermark = false)
{ {

View File

@ -9,6 +9,7 @@
namespace PhpOffice\PhpWord\Element; namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Style\ListItem as ListItemStyle; use PhpOffice\PhpWord\Style\ListItem as ListItemStyle;
/** /**
@ -26,7 +27,7 @@ class ListItem extends AbstractElement
/** /**
* Textrun * Textrun
* *
* @var \PhpOffice\PhpWord\Element\Text * @var Text
*/ */
private $textObject; private $textObject;

View File

@ -26,7 +26,7 @@ class Object extends AbstractElement
/** /**
* Image Style * Image Style
* *
* @var \PhpOffice\PhpWord\Style\Image * @var ImageStyle
*/ */
private $style; private $style;
@ -60,7 +60,7 @@ class Object extends AbstractElement
/** /**
* Get Image style * Get Image style
* *
* @return \PhpOffice\PhpWord\Style\Image * @return ImageStyle
*/ */
public function getStyle() public function getStyle()
{ {

View File

@ -10,6 +10,7 @@
namespace PhpOffice\PhpWord\Element; namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Element\Row; use PhpOffice\PhpWord\Element\Row;
use PhpOffice\PhpWord\Element\Cell;
use PhpOffice\PhpWord\Style\Table as TableStyle; use PhpOffice\PhpWord\Style\Table as TableStyle;
/** /**
@ -70,7 +71,7 @@ class Table extends AbstractElement
* *
* @param int $width * @param int $width
* @param mixed $style * @param mixed $style
* @return \PhpOffice\PhpWord\Element\Cell * @return Cell
*/ */
public function addCell($width = null, $style = null) public function addCell($width = null, $style = null)
{ {

View File

@ -13,65 +13,133 @@ use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Element\Footnote as FootnoteElement; use PhpOffice\PhpWord\Element\Footnote as FootnoteElement;
/** /**
* Footnote * Footnote collection
*/ */
class Footnote class Footnote
{ {
/** /**
* Footnote elements * Elements
* *
* @var array * @var array
*/ */
private static $elements = array(); private static $elements = array();
/**
* Add new element
*
* @param FootnoteElement $footnote
* @return integer Reference ID
* @since 0.9.2
*/
public static function addElement(FootnoteElement $footnote)
{
$rId = self::countElements() + 1;
self::$elements[$rId] = $footnote;
return $rId;
}
/**
* Set element
*
* @param integer $index
* @param FootnoteElement $footnote
* @since 0.9.2
*/
public static function setElement($index, FootnoteElement $footnote)
{
self::$elements[$index] = $footnote;
}
/**
* Get element by index
*
* @return FootnoteElement
* @since 0.9.2
*/
public static function getElement($index)
{
if (array_key_exists($index, self::$elements)) {
return self::$elements[$index];
} else {
return null;
}
}
/**
* Get elements
*
* @return array
* @since 0.9.2
*/
public static function getElements()
{
return self::$elements;
}
/**
* Get element count
*
* @return integer
* @since 0.9.2
*/
public static function countElements()
{
return count(self::$elements);
}
/**
* Reset elements
*
* @since 0.9.2
*/
public static function resetElements()
{
self::$elements = array();
}
/** /**
* Add new footnote * Add new footnote
* *
* @param FootnoteElement $footnote * @param FootnoteElement $footnote
* @return int Reference ID * @return integer Reference ID
* @deprecated 0.9.2
* @codeCoverageIgnore
*/ */
public static function addFootnoteElement(FootnoteElement $footnote) public static function addFootnoteElement(FootnoteElement $footnote)
{ {
$refID = self::countFootnoteElements() + 1; return self::addElement($footnote);
self::$elements[] = $footnote;
return $refID;
} }
/** /**
* Get Footnote Elements * Get Footnote Elements
* *
* @return array * @return array
* @deprecated 0.9.2
* @codeCoverageIgnore
*/ */
public static function getFootnoteElements() public static function getFootnoteElements()
{ {
return self::$elements; return self::getElements();
} }
/** /**
* Get Footnote Elements Count * Get Footnote Elements Count
* *
* @return int * @return integer
* @deprecated 0.9.2
* @codeCoverageIgnore
*/ */
public static function countFootnoteElements() public static function countFootnoteElements()
{ {
return count(self::$elements); return self::countElements();
}
/**
* Reset footer elements
*/
public static function reset()
{
self::$elements = array();
} }
/** /**
* Add new Footnote Link Element * Add new Footnote Link Element
* *
* @param string $linkSrc * @param string $linkSrc
* @return int Reference ID * @return integer Reference ID
* @deprecated 0.9.2 * @deprecated 0.9.2
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */

View File

@ -10,6 +10,8 @@
namespace PhpOffice\PhpWord; namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Writer\WriterInterface;
use PhpOffice\PhpWord\Reader\ReaderInterface;
/** /**
* IO factory * IO factory
@ -19,9 +21,9 @@ abstract class IOFactory
/** /**
* Create new writer * Create new writer
* *
* @param \PhpOffice\PhpWord\PhpWord $phpWord * @param PhpWord $phpWord
* @param string $name * @param string $name
* @return \PhpOffice\PhpWord\Writer\WriterInterface * @return WriterInterface
* @throws Exception * @throws Exception
*/ */
public static function createWriter(PhpWord $phpWord, $name = 'Word2007') public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
@ -38,7 +40,7 @@ abstract class IOFactory
* Create new reader * Create new reader
* *
* @param string $name * @param string $name
* @return \PhpOffice\PhpWord\Reader\ReaderInterface * @return ReaderInterface
* @throws Exception * @throws Exception
*/ */
public static function createReader($name = 'Word2007') public static function createReader($name = 'Word2007')
@ -56,7 +58,7 @@ abstract class IOFactory
* *
* @param string $filename The name of the file * @param string $filename The name of the file
* @param string $readerName * @param string $readerName
* @return \PhpOffice\PhpWord * @return PhpWord
*/ */
public static function load($filename, $readerName = 'Word2007') public static function load($filename, $readerName = 'Word2007')
{ {

View File

@ -13,7 +13,7 @@ use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Element\Image; use PhpOffice\PhpWord\Element\Image;
/** /**
* Media * Media collection
*/ */
class Media class Media
{ {
@ -39,7 +39,7 @@ class Media
// Assign unique media Id and initiate media container if none exists // Assign unique media Id and initiate media container if none exists
$mediaId = md5($container . $source); $mediaId = md5($container . $source);
if (!array_key_exists($container, self::$elements)) { if (!array_key_exists($container, self::$elements)) {
self::$elements[$container]= array(); self::$elements[$container] = array();
} }
// Add media if not exists or point to existing media // Add media if not exists or point to existing media
@ -47,7 +47,7 @@ class Media
$mediaCount = self::countElements($container); $mediaCount = self::countElements($container);
$mediaTypeCount = self::countElements($container, $mediaType); $mediaTypeCount = self::countElements($container, $mediaType);
$mediaData = array(); $mediaData = array();
$relId = ++$mediaCount; $rId = ++$mediaCount;
$target = null; $target = null;
$mediaTypeCount++; $mediaTypeCount++;
@ -57,15 +57,15 @@ class Media
throw new Exception('Image object not assigned.'); throw new Exception('Image object not assigned.');
} }
$isMemImage = $image->getIsMemImage(); $isMemImage = $image->getIsMemImage();
$ext = $image->getImageExtension(); $extension = $image->getImageExtension();
$mediaData['imageExtension'] = $ext; $mediaData['imageExtension'] = $extension;
$mediaData['imageType'] = $image->getImageType(); $mediaData['imageType'] = $image->getImageType();
if ($isMemImage) { if ($isMemImage) {
$mediaData['isMemImage'] = true; $mediaData['isMemImage'] = true;
$mediaData['createFunction'] = $image->getImageCreateFunction(); $mediaData['createFunction'] = $image->getImageCreateFunction();
$mediaData['imageFunction'] = $image->getImageFunction(); $mediaData['imageFunction'] = $image->getImageFunction();
} }
$target = "media/{$container}_image{$mediaTypeCount}.{$ext}"; $target = "media/{$container}_image{$mediaTypeCount}.{$extension}";
// Objects // Objects
} elseif ($mediaType == 'object') { } elseif ($mediaType == 'object') {
$file = "oleObject{$mediaTypeCount}.bin"; $file = "oleObject{$mediaTypeCount}.bin";
@ -78,9 +78,9 @@ class Media
$mediaData['source'] = $source; $mediaData['source'] = $source;
$mediaData['target'] = $target; $mediaData['target'] = $target;
$mediaData['type'] = $mediaType; $mediaData['type'] = $mediaType;
$mediaData['rID'] = $relId; $mediaData['rID'] = $rId;
self::$elements[$container][$mediaId] = $mediaData; self::$elements[$container][$mediaId] = $mediaData;
return $relId; return $rId;
} else { } else {
return self::$elements[$container][$mediaId]['rID']; return self::$elements[$container][$mediaId]['rID'];
} }
@ -153,7 +153,7 @@ class Media
/** /**
* Reset media elements * Reset media elements
*/ */
public static function reset() public static function resetElements()
{ {
self::$elements = array(); self::$elements = array();
} }

View File

@ -82,8 +82,8 @@ class PhpWord
/** /**
* Set document properties object * Set document properties object
* *
* @param \PhpOffice\PhpWord\DocumentProperties $documentProperties * @param DocumentProperties $documentProperties
* @return \PhpOffice\PhpWord\PhpWord * @return self
*/ */
public function setDocumentProperties(DocumentProperties $documentProperties) public function setDocumentProperties(DocumentProperties $documentProperties)
{ {
@ -217,7 +217,7 @@ class PhpWord
/** /**
* Get all sections * Get all sections
* *
* @return \PhpOffice\PhpWord\Element\Section[] * @return Section[]
*/ */
public function getSections() public function getSections()
{ {

View File

@ -47,7 +47,7 @@ abstract class AbstractReader implements ReaderInterface
* Set read data only * Set read data only
* *
* @param bool $pValue * @param bool $pValue
* @return \PhpOffice\PhpWord\Reader\ReaderInterface * @return self
*/ */
public function setReadDataOnly($pValue = true) public function setReadDataOnly($pValue = true)
{ {
@ -60,7 +60,7 @@ abstract class AbstractReader implements ReaderInterface
* *
* @param string $pFilename * @param string $pFilename
* @return resource * @return resource
* @throws \PhpOffice\PhpWord\Exception\Exception * @throws Exception
*/ */
protected function openFile($pFilename) protected function openFile($pFilename)
{ {

File diff suppressed because it is too large Load Diff

View File

@ -38,7 +38,7 @@ class XMLReader
* *
* @param string $zipFile * @param string $zipFile
* @param string $xmlFile * @param string $xmlFile
* @return \DOMDocument * @return \DOMDocument|false
*/ */
public function getDomFromZip($zipFile, $xmlFile) public function getDomFromZip($zipFile, $xmlFile)
{ {
@ -54,6 +54,7 @@ class XMLReader
} }
$contents = $zip->getFromName($xmlFile); $contents = $zip->getFromName($xmlFile);
$zip->close(); $zip->close();
if ($contents === false) { if ($contents === false) {
return false; return false;
} else { } else {
@ -69,7 +70,7 @@ class XMLReader
* @param string $path * @param string $path
* @return \DOMNodeList * @return \DOMNodeList
*/ */
public function getElements($path, \DOMNode $context = null) public function getElements($path, \DOMNode $contextNode = null)
{ {
if ($this->dom === null) { if ($this->dom === null) {
return array(); return array();
@ -78,42 +79,42 @@ class XMLReader
$this->xpath = new \DOMXpath($this->dom); $this->xpath = new \DOMXpath($this->dom);
} }
return $this->xpath->query($path, $context); return $this->xpath->query($path, $contextNode);
} }
/** /**
* Get elements * Get element
* *
* @param string $path * @param string $path
* @return \DOMNodeList * @return \DOMNode|null
*/ */
public function getElement($path, \DOMNode $context = null) public function getElement($path, \DOMNode $contextNode)
{ {
$elements = $this->getElements($path, $context); $elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) { if ($elements->length > 0) {
return $elements->item(0); return $elements->item(0);
} else { } else {
return false; return null;
} }
} }
/** /**
* Get element attribute * Get element attribute
* *
* @param string|\DOMNode $path
* @param string $attribute * @param string $attribute
* @return null|string * @param string $path
* @return string|null
*/ */
public function getAttribute($path, $attribute, \DOMNode $context = null) public function getAttribute($attribute, \DOMElement $contextNode, $path = null)
{ {
if ($path instanceof \DOMNode) { if (is_null($path)) {
$return = $path->getAttribute($attribute); $return = $contextNode->getAttribute($attribute);
} else { } else {
$elements = $this->getElements($path, $context); $elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) { if ($elements->length > 0) {
$return = $elements->item(0)->getAttribute($attribute); $return = $elements->item(0)->getAttribute($attribute);
} else { } else {
$return = ''; $return = null;
} }
} }
@ -124,29 +125,28 @@ class XMLReader
* Get element value * Get element value
* *
* @param string $path * @param string $path
* @return null|string * @return string|null
*/ */
public function getValue($path, \DOMNode $context = null) public function getValue($path, \DOMNode $contextNode)
{ {
$elements = $this->getElements($path, $context); $elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) { if ($elements->length > 0) {
$return = $elements->item(0)->nodeValue; return $elements->item(0)->nodeValue;
} else { } else {
$return = ''; return null;
} }
return ($return == '') ? null : $return;
} }
/** /**
* Count elements * Count elements
* *
* @param string $path * @param string $path
* @return \DOMNodeList * @return integer
*/ */
public function countElements($path, \DOMNode $context = null) public function countElements($path, \DOMNode $contextNode)
{ {
$elements = $this->getElements($path, $context); $elements = $this->getElements($path, $contextNode);
return $elements->length; return $elements->length;
} }
@ -156,8 +156,8 @@ class XMLReader
* @param string $path * @param string $path
* @return \DOMNodeList * @return \DOMNodeList
*/ */
public function elementExists($path, \DOMNode $context = null) public function elementExists($path, \DOMNode $contextNode)
{ {
return $this->getElements($path, $context)->length > 0; return $this->getElements($path, $contextNode)->length > 0;
} }
} }

View File

@ -31,6 +31,13 @@ class ZipArchive
const OVERWRITE = 'OVERWRITE'; const OVERWRITE = 'OVERWRITE';
const CREATE = 'CREATE'; const CREATE = 'CREATE';
/**
* Number of files (emulate ZipArchive::$numFiles)
*
* @var string
*/
public $numFiles = 0;
/** /**
* Temporary storage directory * Temporary storage directory
* *
@ -48,13 +55,14 @@ class ZipArchive
/** /**
* Open a new zip archive * Open a new zip archive
* *
* @param string $fileName Filename for the zip archive * @param string $filename Filename for the zip archive
* @return boolean * @return boolean
*/ */
public function open($fileName) public function open($filename)
{ {
$this->tempDir = sys_get_temp_dir(); $this->tempDir = sys_get_temp_dir();
$this->zip = new \PclZip($fileName); $this->zip = new \PclZip($filename);
$this->numFiles = count($this->zip->listContent());
return true; return true;
} }
@ -138,19 +146,19 @@ class ZipArchive
} }
/** /**
* Find if given fileName exist in archive (Emulate ZipArchive locateName()) * Find if given file name exist in archive (Emulate ZipArchive locateName())
* *
* @param string $fileName Filename for the file in zip archive * @param string $filename Filename for the file in zip archive
* @return boolean * @return boolean
*/ */
public function locateName($fileName) public function locateName($filename)
{ {
$list = $this->zip->listContent(); $list = $this->zip->listContent();
$listCount = count($list); $listCount = count($list);
$listIndex = -1; $listIndex = -1;
for ($i = 0; $i < $listCount; ++$i) { for ($i = 0; $i < $listCount; ++$i) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || if (strtolower($list[$i]["filename"]) == strtolower($filename) ||
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { strtolower($list[$i]["stored_filename"]) == strtolower($filename)) {
$listIndex = $i; $listIndex = $i;
break; break;
} }
@ -160,12 +168,12 @@ class ZipArchive
} }
/** /**
* Extract file from archive by given fileName (Emulate ZipArchive getFromName()) * Extract file from archive by given file name (Emulate ZipArchive getFromName())
* *
* @param string $fileName Filename for the file in zip archive * @param string $filename Filename for the file in zip archive
* @return string $contents File string contents * @return string $contents File string contents
*/ */
public function getFromName($fileName) public function getFromName($filename)
{ {
$list = $this->zip->listContent(); $list = $this->zip->listContent();
$listCount = count($list); $listCount = count($list);
@ -173,8 +181,8 @@ class ZipArchive
$contents = null; $contents = null;
for ($i = 0; $i < $listCount; ++$i) { for ($i = 0; $i < $listCount; ++$i) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || if (strtolower($list[$i]["filename"]) == strtolower($filename) ||
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { strtolower($list[$i]["stored_filename"]) == strtolower($filename)) {
$listIndex = $i; $listIndex = $i;
break; break;
} }
@ -183,11 +191,11 @@ class ZipArchive
if ($listIndex != -1) { if ($listIndex != -1) {
$extracted = $this->zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING); $extracted = $this->zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING);
} else { } else {
$fileName = substr($fileName, 1); $filename = substr($filename, 1);
$listIndex = -1; $listIndex = -1;
for ($i = 0; $i < $listCount; ++$i) { for ($i = 0; $i < $listCount; ++$i) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || if (strtolower($list[$i]["filename"]) == strtolower($filename) ||
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { strtolower($list[$i]["stored_filename"]) == strtolower($filename)) {
$listIndex = $i; $listIndex = $i;
break; break;
} }
@ -200,4 +208,21 @@ class ZipArchive
return $contents; return $contents;
} }
/**
* Returns the name of an entry using its index
*
* @param integer $index
* @return string|false
*/
public function getNameIndex($index)
{
$list = $this->zip->listContent();
$listCount = count($list);
if ($index <= $listCount) {
return $list[$index]['filename'];
} else {
return false;
}
}
} }

View File

@ -14,7 +14,7 @@ use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style\Table; use PhpOffice\PhpWord\Style\Table;
/** /**
* Style * Style collection
*/ */
class Style class Style
{ {
@ -91,7 +91,7 @@ class Style
/** /**
* Reset styles * Reset styles
*/ */
public static function reset() public static function resetStyles()
{ {
self::$styles = array(); self::$styles = array();
} }
@ -109,7 +109,7 @@ class Style
/** /**
* Get all styles * Get all styles
* *
* @return \PhpOffice\PhpWord\Style\Font[] * @return Font[]
*/ */
public static function getStyles() public static function getStyles()
{ {

View File

@ -152,9 +152,9 @@ class TOC
} }
/** /**
* Reset footnotes * Reset titles
*/ */
public static function reset() public static function resetTitles()
{ {
self::$titles = array(); self::$titles = array();
} }

View File

@ -13,6 +13,7 @@ use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Footnote; use PhpOffice\PhpWord\Footnote;
use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Writer\Word2007\ContentTypes; use PhpOffice\PhpWord\Writer\Word2007\ContentTypes;
use PhpOffice\PhpWord\Writer\Word2007\Rels; use PhpOffice\PhpWord\Writer\Word2007\Rels;
use PhpOffice\PhpWord\Writer\Word2007\DocProps; use PhpOffice\PhpWord\Writer\Word2007\DocProps;
@ -97,11 +98,11 @@ class Word2007 extends AbstractWriter implements WriterInterface
// Add header/footer contents // Add header/footer contents
$overrides = array(); $overrides = array();
$rID = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements $rId = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements
$sections = $this->phpWord->getSections(); $sections = $this->phpWord->getSections();
foreach ($sections as $section) { foreach ($sections as $section) {
$this->addHeaderFooterContent($section, $objZip, 'header', $rID); $this->addHeaderFooterContent($section, $objZip, 'header', $rId);
$this->addHeaderFooterContent($section, $objZip, 'footer', $rID); $this->addHeaderFooterContent($section, $objZip, 'footer', $rId);
} }
// Add footnotes media files, relations, and contents // Add footnotes media files, relations, and contents
@ -113,7 +114,7 @@ class Word2007 extends AbstractWriter implements WriterInterface
} }
$objZip->addFromString('word/footnotes.xml', $this->getWriterPart('footnotes')->writeFootnotes(Footnote::getFootnoteElements())); $objZip->addFromString('word/footnotes.xml', $this->getWriterPart('footnotes')->writeFootnotes(Footnote::getFootnoteElements()));
$this->cTypes['override']["/word/footnotes.xml"] = 'footnotes'; $this->cTypes['override']["/word/footnotes.xml"] = 'footnotes';
$this->docRels[] = array('target' => 'footnotes.xml', 'type' => 'footnotes', 'rID' => ++$rID); $this->docRels[] = array('target' => 'footnotes.xml', 'type' => 'footnotes', 'rID' => ++$rId);
} }
// Write dynamic files // Write dynamic files
@ -207,12 +208,11 @@ class Word2007 extends AbstractWriter implements WriterInterface
/** /**
* Add header/footer content * Add header/footer content
* *
* @param \PhpOffice\PhpWord\Element\Section $section
* @param mixed $objZip * @param mixed $objZip
* @param string $elmType * @param string $elmType
* @param integer $rID * @param integer $rId
*/ */
private function addHeaderFooterContent(&$section, $objZip, $elmType, &$rID) private function addHeaderFooterContent(Section &$section, $objZip, $elmType, &$rId)
{ {
$getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters'; $getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters';
$writeFunction = $elmType == 'header' ? 'writeHeader' : 'writeFooter'; $writeFunction = $elmType == 'header' ? 'writeHeader' : 'writeFooter';
@ -220,11 +220,11 @@ class Word2007 extends AbstractWriter implements WriterInterface
$elmObjects = $section->$getFunction(); $elmObjects = $section->$getFunction();
foreach ($elmObjects as $index => &$elmObject) { foreach ($elmObjects as $index => &$elmObject) {
$elmCount++; $elmCount++;
$elmObject->setRelationId(++$rID); $elmObject->setRelationId(++$rId);
$elmFile = "{$elmType}{$elmCount}.xml"; $elmFile = "{$elmType}{$elmCount}.xml";
$objZip->addFromString("word/$elmFile", $this->getWriterPart($elmType)->$writeFunction($elmObject)); $objZip->addFromString("word/$elmFile", $this->getWriterPart($elmType)->$writeFunction($elmObject));
$this->cTypes['override']["/word/$elmFile"] = $elmType; $this->cTypes['override']["/word/$elmFile"] = $elmType;
$this->docRels[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rID); $this->docRels[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId);
} }
} }
} }

View File

@ -93,7 +93,7 @@ class Base extends AbstractWriterPart
*/ */
protected function writeLink(XMLWriter $xmlWriter, Link $link, $withoutP = false) protected function writeLink(XMLWriter $xmlWriter, Link $link, $withoutP = false)
{ {
$rID = $link->getRelationId() + ($link->isInSection() ? 6 : 0); $rId = $link->getRelationId() + ($link->isInSection() ? 6 : 0);
$linkName = $link->getLinkName(); $linkName = $link->getLinkName();
if (is_null($linkName)) { if (is_null($linkName)) {
$linkName = $link->getLinkSrc(); $linkName = $link->getLinkSrc();
@ -106,7 +106,7 @@ class Base extends AbstractWriterPart
$this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph);
} }
$xmlWriter->startElement('w:hyperlink'); $xmlWriter->startElement('w:hyperlink');
$xmlWriter->writeAttribute('r:id', 'rId' . $rID); $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
$xmlWriter->writeAttribute('w:history', '1'); $xmlWriter->writeAttribute('w:history', '1');
$xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->writeInlineFontStyle($xmlWriter, $styleFont); $this->writeInlineFontStyle($xmlWriter, $styleFont);

View File

@ -32,7 +32,7 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, count(Footnote::getFootnoteElements())); $this->assertEquals(1, count(Footnote::getFootnoteElements()));
$this->assertEquals(1, count(Footnote::getFootnoteLinkElements())); $this->assertEquals(1, count(Footnote::getFootnoteLinkElements()));
Footnote::reset(); Footnote::resetElements();
$this->assertEquals(0, count(Footnote::getFootnoteElements())); $this->assertEquals(0, count(Footnote::getFootnoteElements()));
} }
} }

View File

@ -95,7 +95,7 @@ class MediaTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(2, Media::countElements('footer1')); $this->assertEquals(2, Media::countElements('footer1'));
Media::reset(); Media::resetElements();
$this->assertEquals(0, Media::countElements('footer1')); $this->assertEquals(0, Media::countElements('footer1'));
} }

View File

@ -44,7 +44,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase
} }
$this->assertNull(Style::getStyle('Unknown')); $this->assertNull(Style::getStyle('Unknown'));
Style::reset(); Style::resetStyles();
$this->assertEquals(0, count(Style::getStyles())); $this->assertEquals(0, count(Style::getStyles()));
} }

View File

@ -80,7 +80,7 @@ class TOCTest extends \PHPUnit_Framework_TestCase
$i++; $i++;
} }
TOC::reset(); TOC::resetTitles();
$this->assertEquals(0, count($toc->getTitles())); $this->assertEquals(0, count($toc->getTitles()));
} }