TemplateProcessor
in package
Table of Contents
Constants
Properties
- $macroClosingChars : mixed
- $macroOpeningChars : mixed
- $tempDocumentContentTypes : string
- Document content types (in XML format) of the temporary document.
- $tempDocumentFilename : string
- $tempDocumentFooters : array<string|int, string>
- Content of footers (in XML format) of the temporary document.
- $tempDocumentHeaders : array<string|int, string>
- Content of headers (in XML format) of the temporary document.
- $tempDocumentMainPart : string
- Content of main document part (in XML format) of the temporary document.
- $tempDocumentNewImages : array<string|int, string>
- new inserted images list.
- $tempDocumentRelations : array<string|int, string>
- Document relations (in XML format) of the temporary document.
- $tempDocumentSettingsPart : string
- Content of settings part (in XML format) of the temporary document.
- $zipClass : mixed
- ZipArchive object.
Methods
- __construct() : mixed
- __destruct() : mixed
- applyXslStyleSheet() : void
- Applies XSL style sheet to template's parts.
- cloneBlock() : null|string
- Clone a block.
- cloneRow() : void
- Clone a table row in a template document.
- cloneRowAndSetValues() : void
- Clones a table row and populates it's values from a two-dimensional array in a template document.
- deleteBlock() : void
- Delete a block of text.
- deleteRow() : void
- Delete a table row in a template document.
- getTempDocumentFilename() : string
- getVariableCount() : array<string|int, mixed>
- Returns count of all variables in template.
- getVariables() : array<string|int, string>
- Returns array of all variables in template.
- replaceBlock() : void
- Replace a block.
- replaceCarriageReturns() : string
- Replace carriage returns with xml.
- replaceXmlBlock() : TemplateProcessor
- Replace an XML block surrounding a macro with a new block.
- save() : string
- Saves the result document.
- saveAs() : void
- Saves the result document to the user defined file.
- setChart() : void
- setCheckbox() : void
- setComplexBlock() : void
- setComplexValue() : void
- setImageValue() : void
- setMacroChars() : void
- setMacroClosingChars() : void
- setMacroOpeningChars() : void
- setUpdateFields() : void
- Automatically Recalculate Fields on Open.
- setValue() : void
- setValues() : void
- Set values from a one-dimensional array of "variable => value"-pairs.
- zip() : ZipArchive
- Expose zip class.
- ensureMacroCompleted() : string
- ensureUtf8Encoded() : string
- findContainingXmlBlockForMacro() : bool|array<string|int, int>
- Find start and end of XML block containing the given macro e.g. <w:p>...${macro}...</w:p>.
- findMacro() : int
- Find the position of (the start of) a macro.
- findRowEnd() : int
- Find the end position of the nearest table row after $offset.
- findRowStart() : int
- Find the start position of the nearest table row before $offset.
- findXmlBlockEnd() : int
- Find the nearest block end position after $offset.
- findXmlBlockStart() : int
- Find the start position of the nearest XML block start before $offset.
- fixBrokenMacros() : string
- Finds parts of broken macros and sticks them together.
- getDocumentContentTypesName() : string
- getFooterName() : string
- Get the name of the footer file for $index.
- getHeaderName() : string
- Get the name of the header file for $index.
- getMainPartName() : string
- Usually, the name of main part document will be 'document.xml'. However, some .docx files (possibly those from Office 365, experienced also on documents from Word Online created from blank templates) have file 'document22.xml' in their zip archive instead of 'document.xml'. This method searches content types file to correctly determine the file name.
- getNextRelationsIndex() : mixed
- getRelationsName() : string
- Get the name of the relations file for document part.
- getSettingsPartName() : string
- The name of the file containing the Settings part.
- getSlice() : string
- Get a slice of a string.
- getVariablesForPart() : array<string|int, string>
- Find all variables in $documentPartXML.
- indexClonedVariables() : string
- Replaces variable names in cloned rows/blocks with indexed names.
- readPartWithRels() : string
- replaceClonedVariables() : array<string|int, string>
- Replaces variables with values from array, array keys are the variable names.
- savePartWithRels() : void
- setValueForPart() : string
- Find and replace macros in the given XML section.
- splitTextIntoTexts() : string
- Splits a w:r/w:t into a list of w:r where each ${macro} is in a separate w:r.
- textNeedsSplitting() : bool
- Returns true if string contains a macro that is not in it's own w:r.
- transformSingleXml() : string
- transformXml() : mixed
- addImageToRelations() : void
- chooseImageDimension() : mixed
- findTableEnd() : int
- Find the end position of the nearest table row after $offset.
- findTableStart() : int
- Find the start position of the nearest table before $offset.
- fixImageWidthHeightRatio() : void
- getImageArgs() : mixed
- prepareImageAttrs() : mixed
Constants
MAXIMUM_REPLACEMENTS_DEFAULT
public
mixed
MAXIMUM_REPLACEMENTS_DEFAULT
= -1
Properties
$macroClosingChars
protected
static mixed
$macroClosingChars
= '}'
$macroOpeningChars
protected
static mixed
$macroOpeningChars
= '${'
$tempDocumentContentTypes
Document content types (in XML format) of the temporary document.
protected
string
$tempDocumentContentTypes
= ''
$tempDocumentFilename
protected
string
$tempDocumentFilename
Temporary document filename (with path)
$tempDocumentFooters
Content of footers (in XML format) of the temporary document.
protected
array<string|int, string>
$tempDocumentFooters
= []
$tempDocumentHeaders
Content of headers (in XML format) of the temporary document.
protected
array<string|int, string>
$tempDocumentHeaders
= []
$tempDocumentMainPart
Content of main document part (in XML format) of the temporary document.
protected
string
$tempDocumentMainPart
$tempDocumentNewImages
new inserted images list.
protected
array<string|int, string>
$tempDocumentNewImages
= []
$tempDocumentRelations
Document relations (in XML format) of the temporary document.
protected
array<string|int, string>
$tempDocumentRelations
= []
$tempDocumentSettingsPart
Content of settings part (in XML format) of the temporary document.
protected
string
$tempDocumentSettingsPart
$zipClass
ZipArchive object.
protected
mixed
$zipClass
Methods
__construct()
public
__construct(string $documentTemplate) : mixed
Parameters
- $documentTemplate : string
-
The fully qualified template filename
Tags
__destruct()
public
__destruct() : mixed
applyXslStyleSheet()
Applies XSL style sheet to template's parts.
public
applyXslStyleSheet(DOMDocument $xslDomDocument[, array<string|int, mixed> $xslOptions = [] ][, string $xslOptionsUri = '' ]) : void
Note: since the method doesn't make any guess on logic of the provided XSL style sheet, make sure that output is correctly escaped. Otherwise you may get broken document.
Parameters
- $xslDomDocument : DOMDocument
- $xslOptions : array<string|int, mixed> = []
- $xslOptionsUri : string = ''
cloneBlock()
Clone a block.
public
cloneBlock(string $blockname[, int $clones = 1 ][, bool $replace = true ][, bool $indexVariables = false ][, array<string|int, mixed> $variableReplacements = null ]) : null|string
Parameters
- $blockname : string
- $clones : int = 1
-
How many time the block should be cloned
- $replace : bool = true
- $indexVariables : bool = false
-
If true, any variables inside the block will be indexed (postfixed with #1, #2, ...)
- $variableReplacements : array<string|int, mixed> = null
-
Array containing replacements for macros found inside the block to clone
Return values
null|stringcloneRow()
Clone a table row in a template document.
public
cloneRow(string $search, int $numberOfClones) : void
Parameters
- $search : string
- $numberOfClones : int
cloneRowAndSetValues()
Clones a table row and populates it's values from a two-dimensional array in a template document.
public
cloneRowAndSetValues(string $search, array<string|int, mixed> $values) : void
Parameters
- $search : string
- $values : array<string|int, mixed>
deleteBlock()
Delete a block of text.
public
deleteBlock(string $blockname) : void
Parameters
- $blockname : string
deleteRow()
Delete a table row in a template document.
public
deleteRow(string $search) : void
Parameters
- $search : string
getTempDocumentFilename()
public
getTempDocumentFilename() : string
Return values
stringgetVariableCount()
Returns count of all variables in template.
public
getVariableCount() : array<string|int, mixed>
Return values
array<string|int, mixed>getVariables()
Returns array of all variables in template.
public
getVariables() : array<string|int, string>
Return values
array<string|int, string>replaceBlock()
Replace a block.
public
replaceBlock(string $blockname, string $replacement) : void
Parameters
- $blockname : string
- $replacement : string
replaceCarriageReturns()
Replace carriage returns with xml.
public
replaceCarriageReturns(string $string) : string
Parameters
- $string : string
Return values
stringreplaceXmlBlock()
Replace an XML block surrounding a macro with a new block.
public
replaceXmlBlock(string $macro, string $block[, string $blockType = 'w:p' ]) : TemplateProcessor
Parameters
- $macro : string
-
Name of macro
- $block : string
-
New block content
- $blockType : string = 'w:p'
-
XML tag type of block
Return values
TemplateProcessor —Fluent interface
save()
Saves the result document.
public
save() : string
Return values
stringsaveAs()
Saves the result document to the user defined file.
public
saveAs(string $fileName) : void
Parameters
- $fileName : string
Tags
setChart()
public
setChart(string $search, AbstractElement $chart) : void
Parameters
- $search : string
- $chart : AbstractElement
setCheckbox()
public
setCheckbox(string $search, bool $checked) : void
Parameters
- $search : string
- $checked : bool
setComplexBlock()
public
setComplexBlock(string $search, AbstractElement $complexType) : void
Parameters
- $search : string
- $complexType : AbstractElement
setComplexValue()
public
setComplexValue(string $search, AbstractElement $complexType) : void
Parameters
- $search : string
- $complexType : AbstractElement
setImageValue()
public
setImageValue(mixed $search, mixed $replace[, int $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT ]) : void
Parameters
- $search : mixed
- $replace : mixed
-
Path to image, or array("path" => xx, "width" => yy, "height" => zz)
- $limit : int = self::MAXIMUM_REPLACEMENTS_DEFAULT
setMacroChars()
public
setMacroChars(string $macroOpeningChars, string $macroClosingChars) : void
Parameters
- $macroOpeningChars : string
- $macroClosingChars : string
setMacroClosingChars()
public
setMacroClosingChars(string $macroClosingChars) : void
Parameters
- $macroClosingChars : string
setMacroOpeningChars()
public
setMacroOpeningChars(string $macroOpeningChars) : void
Parameters
- $macroOpeningChars : string
setUpdateFields()
Automatically Recalculate Fields on Open.
public
setUpdateFields([bool $update = true ]) : void
Parameters
- $update : bool = true
setValue()
public
setValue(mixed $search, mixed $replace[, int $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT ]) : void
Parameters
- $search : mixed
- $replace : mixed
- $limit : int = self::MAXIMUM_REPLACEMENTS_DEFAULT
setValues()
Set values from a one-dimensional array of "variable => value"-pairs.
public
setValues(array<string|int, mixed> $values) : void
Parameters
- $values : array<string|int, mixed>
zip()
Expose zip class.
public
zip() : ZipArchive
To replace an image: $templateProcessor->zip()->AddFromString("word/media/image1.jpg", file_get_contents($file));
To read a file: $templateProcessor->zip()->getFromName("word/media/image1.jpg");
Return values
ZipArchiveensureMacroCompleted()
protected
static ensureMacroCompleted(string $macro) : string
Parameters
- $macro : string
Return values
stringensureUtf8Encoded()
protected
static ensureUtf8Encoded(string|null $subject) : string
Parameters
- $subject : string|null
Return values
stringfindContainingXmlBlockForMacro()
Find start and end of XML block containing the given macro e.g. <w:p>...${macro}...</w:p>.
protected
findContainingXmlBlockForMacro(string $macro[, string $blockType = 'w:p' ]) : bool|array<string|int, int>
Note that only the first instance of the macro will be found
Parameters
- $macro : string
-
Name of macro
- $blockType : string = 'w:p'
-
XML tag for block
Return values
bool|array<string|int, int> —FALSE if not found, otherwise array with start and end
findMacro()
Find the position of (the start of) a macro.
protected
findMacro(string $search[, int $offset = 0 ]) : int
Returns -1 if not found, otherwise position of opening $
Note that only the first instance of the macro will be found
Parameters
- $search : string
-
Macro name
- $offset : int = 0
-
Offset from which to start searching
Return values
int —-1 if macro not found
findRowEnd()
Find the end position of the nearest table row after $offset.
protected
findRowEnd(int $offset) : int
Parameters
- $offset : int
Return values
intfindRowStart()
Find the start position of the nearest table row before $offset.
protected
findRowStart(int $offset) : int
Parameters
- $offset : int
Return values
intfindXmlBlockEnd()
Find the nearest block end position after $offset.
protected
findXmlBlockEnd(int $offset, string $blockType) : int
Parameters
- $offset : int
-
Search position
- $blockType : string
-
XML Block tag
Return values
int —-1 if block end not found
findXmlBlockStart()
Find the start position of the nearest XML block start before $offset.
protected
findXmlBlockStart(int $offset, string $blockType) : int
Parameters
- $offset : int
-
Search position
- $blockType : string
-
XML Block tag
Return values
int —-1 if block start not found
fixBrokenMacros()
Finds parts of broken macros and sticks them together.
protected
fixBrokenMacros(string $documentPart) : string
Macros, while being edited, could be implicitly broken by some of the word processors.
Parameters
- $documentPart : string
-
The document part in XML representation
Return values
stringgetDocumentContentTypesName()
protected
getDocumentContentTypesName() : string
Return values
stringgetFooterName()
Get the name of the footer file for $index.
protected
getFooterName(int $index) : string
Parameters
- $index : int
Return values
stringgetHeaderName()
Get the name of the header file for $index.
protected
getHeaderName(int $index) : string
Parameters
- $index : int
Return values
stringgetMainPartName()
Usually, the name of main part document will be 'document.xml'. However, some .docx files (possibly those from Office 365, experienced also on documents from Word Online created from blank templates) have file 'document22.xml' in their zip archive instead of 'document.xml'. This method searches content types file to correctly determine the file name.
protected
getMainPartName() : string
Return values
stringgetNextRelationsIndex()
protected
getNextRelationsIndex(mixed $documentPartName) : mixed
Parameters
- $documentPartName : mixed
getRelationsName()
Get the name of the relations file for document part.
protected
getRelationsName(string $documentPartName) : string
Parameters
- $documentPartName : string
Return values
stringgetSettingsPartName()
The name of the file containing the Settings part.
protected
getSettingsPartName() : string
Return values
stringgetSlice()
Get a slice of a string.
protected
getSlice(int $startPosition[, int $endPosition = 0 ]) : string
Parameters
- $startPosition : int
- $endPosition : int = 0
Return values
stringgetVariablesForPart()
Find all variables in $documentPartXML.
protected
getVariablesForPart(string $documentPartXML) : array<string|int, string>
Parameters
- $documentPartXML : string
Return values
array<string|int, string>indexClonedVariables()
Replaces variable names in cloned rows/blocks with indexed names.
protected
indexClonedVariables(int $count, string $xmlBlock) : string
Parameters
- $count : int
- $xmlBlock : string
Return values
stringreadPartWithRels()
protected
readPartWithRels(string $fileName) : string
Parameters
- $fileName : string
Return values
stringreplaceClonedVariables()
Replaces variables with values from array, array keys are the variable names.
protected
replaceClonedVariables(array<string|int, mixed> $variableReplacements, string $xmlBlock) : array<string|int, string>
Parameters
- $variableReplacements : array<string|int, mixed>
- $xmlBlock : string
Return values
array<string|int, string>savePartWithRels()
protected
savePartWithRels(string $fileName, string $xml) : void
Parameters
- $fileName : string
- $xml : string
setValueForPart()
Find and replace macros in the given XML section.
protected
setValueForPart(mixed $search, mixed $replace, array<int, string>|string $documentPartXML, int $limit) : string
Parameters
- $search : mixed
- $replace : mixed
- $documentPartXML : array<int, string>|string
- $limit : int
Return values
stringsplitTextIntoTexts()
Splits a w:r/w:t into a list of w:r where each ${macro} is in a separate w:r.
protected
splitTextIntoTexts(string $text) : string
Parameters
- $text : string
Return values
stringtextNeedsSplitting()
Returns true if string contains a macro that is not in it's own w:r.
protected
textNeedsSplitting(string $text) : bool
Parameters
- $text : string
Return values
booltransformSingleXml()
protected
transformSingleXml(string $xml, XSLTProcessor $xsltProcessor) : string
Parameters
- $xml : string
- $xsltProcessor : XSLTProcessor
Return values
stringtransformXml()
protected
transformXml(mixed $xml, XSLTProcessor $xsltProcessor) : mixed
Parameters
- $xml : mixed
- $xsltProcessor : XSLTProcessor
addImageToRelations()
private
addImageToRelations(mixed $partFileName, mixed $rid, mixed $imgPath, mixed $imageMimeType) : void
Parameters
- $partFileName : mixed
- $rid : mixed
- $imgPath : mixed
- $imageMimeType : mixed
chooseImageDimension()
private
chooseImageDimension(mixed $baseValue, mixed $inlineValue, mixed $defaultValue) : mixed
Parameters
- $baseValue : mixed
- $inlineValue : mixed
- $defaultValue : mixed
findTableEnd()
Find the end position of the nearest table row after $offset.
private
findTableEnd(int $offset) : int
Parameters
- $offset : int
Return values
intfindTableStart()
Find the start position of the nearest table before $offset.
private
findTableStart(int $offset) : int
Parameters
- $offset : int
Return values
intfixImageWidthHeightRatio()
private
fixImageWidthHeightRatio(mixed &$width, mixed &$height, mixed $actualWidth, mixed $actualHeight) : void
Parameters
- $width : mixed
- $height : mixed
- $actualWidth : mixed
- $actualHeight : mixed
getImageArgs()
private
getImageArgs(mixed $varNameWithArgs) : mixed
Parameters
- $varNameWithArgs : mixed
prepareImageAttrs()
private
prepareImageAttrs(mixed $replaceImage, mixed $varInlineArgs) : mixed
Parameters
- $replaceImage : mixed
- $varInlineArgs : mixed