Refactor PhpWord to reduce dependencies
This commit is contained in:
parent
3197524343
commit
e952fdefb5
|
|
@ -25,7 +25,7 @@ namespace PhpOffice\PhpWord\Element;
|
||||||
* @method Link addLink(string $target, string $text = null, mixed $fStyle = null, mixed $pStyle = null)
|
* @method Link addLink(string $target, string $text = null, mixed $fStyle = null, mixed $pStyle = null)
|
||||||
* @method PreserveText addPreserveText(string $text, mixed $fStyle = null, mixed $pStyle = null)
|
* @method PreserveText addPreserveText(string $text, mixed $fStyle = null, mixed $pStyle = null)
|
||||||
* @method void addTextBreak(int $count = 1, mixed $fStyle = null, mixed $pStyle = null)
|
* @method void addTextBreak(int $count = 1, mixed $fStyle = null, mixed $pStyle = null)
|
||||||
* @method ListItem addListItem(string $text, int $depth = 0, mixed $fStyle = null, mixed $listStyle = null, mixed $pStyle = null)
|
* @method ListItem addListItem(string $txt, int $depth = 0, mixed $font = null, mixed $list = null, mixed $para = null)
|
||||||
* @method ListItemRun addListItemRun(int $depth = 0, mixed $listStyle = null, mixed $pStyle = null)
|
* @method ListItemRun addListItemRun(int $depth = 0, mixed $listStyle = null, mixed $pStyle = null)
|
||||||
* @method Footnote addFootnote(mixed $pStyle = null)
|
* @method Footnote addFootnote(mixed $pStyle = null)
|
||||||
* @method Endnote addEndnote(mixed $pStyle = null)
|
* @method Endnote addEndnote(mixed $pStyle = null)
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,25 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord;
|
namespace PhpOffice\PhpWord;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Collection\Endnotes;
|
|
||||||
use PhpOffice\PhpWord\Collection\Footnotes;
|
|
||||||
use PhpOffice\PhpWord\Collection\Titles;
|
|
||||||
use PhpOffice\PhpWord\Element\Section;
|
use PhpOffice\PhpWord\Element\Section;
|
||||||
use PhpOffice\PhpWord\Exception\Exception;
|
use PhpOffice\PhpWord\Exception\Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PHPWord main class
|
* PHPWord main class
|
||||||
|
*
|
||||||
|
* @method Collection\Titles getTitles()
|
||||||
|
* @method Collection\Footnotes getFootnotes()
|
||||||
|
* @method Collection\Endnotes getEndnotes()
|
||||||
|
* @method int addTitle(Element\Title $title)
|
||||||
|
* @method int addFootnote(Element\Footnote $footnote)
|
||||||
|
* @method int addEndnote(Element\Endnote $endnote)
|
||||||
|
*
|
||||||
|
* @method Style\Paragraph addParagraphStyle(string $styleName, array $styles)
|
||||||
|
* @method Style\Font addFontStyle(string $styleName, mixed $fontStyle, mixed $paragraphStyle = null)
|
||||||
|
* @method Style\Font addLinkStyle(string $styleName, mixed $styles)
|
||||||
|
* @method Style\Font addTitleStyle(int $depth, mixed $fontStyle, mixed $paragraphStyle = null)
|
||||||
|
* @method Style\Table addTableStyle(string $styleName, mixed $styleTable, mixed $styleFirstRow = null)
|
||||||
|
* @method Style\Numbering addNumberingStyle(string $styleName, mixed $styles)
|
||||||
*/
|
*/
|
||||||
class PhpWord
|
class PhpWord
|
||||||
{
|
{
|
||||||
|
|
@ -54,35 +65,84 @@ class PhpWord
|
||||||
private $sections = array();
|
private $sections = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collection of titles
|
* Collections
|
||||||
*
|
*
|
||||||
* @var \PhpOffice\PhpWord\Collection\Titles
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $titles;
|
private $collections = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collection of footnotes
|
* Create new instance
|
||||||
*
|
*
|
||||||
* @var \PhpOffice\PhpWord\Collection\Footnotes
|
* Collections are created dynamically
|
||||||
*/
|
|
||||||
private $footnotes;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Collection of endnotes
|
|
||||||
*
|
|
||||||
* @var \PhpOffice\PhpWord\Collection\Endnotes
|
|
||||||
*/
|
|
||||||
private $endnotes;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create new
|
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->documentProperties = new DocumentProperties();
|
$this->documentProperties = new DocumentProperties();
|
||||||
$this->titles = new Titles();
|
|
||||||
$this->footnotes = new Footnotes();
|
$collections = array('Titles', 'Footnotes', 'Endnotes');
|
||||||
$this->endnotes = new Endnotes();
|
foreach ($collections as $collection) {
|
||||||
|
$class = 'PhpOffice\\PhpWord\\Collection\\' . $collection;
|
||||||
|
$this->collections[$collection] = new $class();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic function call to reduce static dependency
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* - Getting and adding collections (Titles, Footnotes, and Endnotes)
|
||||||
|
* - Adding style
|
||||||
|
*
|
||||||
|
* @param mixed $function
|
||||||
|
* @param mixed $args
|
||||||
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @since 0.12.0
|
||||||
|
*/
|
||||||
|
public function __call($function, $args)
|
||||||
|
{
|
||||||
|
$function = strtolower($function);
|
||||||
|
|
||||||
|
$getCollection = array();
|
||||||
|
$addCollection = array();
|
||||||
|
$addStyle = array();
|
||||||
|
|
||||||
|
$collections = array('Title', 'Footnote', 'Endnote');
|
||||||
|
foreach ($collections as $collection) {
|
||||||
|
$getCollection[] = strtolower("get{$collection}s");
|
||||||
|
$addCollection[] = strtolower("add{$collection}");
|
||||||
|
}
|
||||||
|
|
||||||
|
$styles = array('Paragraph', 'Font', 'Table', 'Numbering', 'Link', 'Title');
|
||||||
|
foreach ($styles as $style) {
|
||||||
|
$addStyle[] = strtolower("add{$style}style");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run get collection method
|
||||||
|
if (in_array($function, $getCollection)) {
|
||||||
|
$key = ucfirst(str_replace('get', '', $function));
|
||||||
|
|
||||||
|
return $this->collections[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run add collection item method
|
||||||
|
if (in_array($function, $addCollection)) {
|
||||||
|
$key = ucfirst(str_replace('add', '', $function) . 's');
|
||||||
|
|
||||||
|
/** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collectionObject */
|
||||||
|
$collectionObject = $this->collections[$key];
|
||||||
|
|
||||||
|
return $collectionObject->addItem($args[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run add style method
|
||||||
|
if (in_array($function, $addStyle)) {
|
||||||
|
return forward_static_call_array(array('PhpOffice\\PhpWord\\Style', $function), $args);
|
||||||
|
}
|
||||||
|
|
||||||
|
// All other methods
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -133,69 +193,6 @@ class PhpWord
|
||||||
return $section;
|
return $section;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get titles
|
|
||||||
*
|
|
||||||
* @return \PhpOffice\PhpWord\Collection\Titles
|
|
||||||
*/
|
|
||||||
public function getTitles()
|
|
||||||
{
|
|
||||||
return $this->titles;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add new title
|
|
||||||
*
|
|
||||||
* @param \PhpOffice\PhpWord\Element\Title $title
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function addTitle($title)
|
|
||||||
{
|
|
||||||
return $this->titles->addItem($title);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get footnotes
|
|
||||||
*
|
|
||||||
* @return \PhpOffice\PhpWord\Collection\Footnotes
|
|
||||||
*/
|
|
||||||
public function getFootnotes()
|
|
||||||
{
|
|
||||||
return $this->footnotes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add new footnote
|
|
||||||
*
|
|
||||||
* @param \PhpOffice\PhpWord\Element\Footnote $footnote
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function addFootnote($footnote)
|
|
||||||
{
|
|
||||||
return $this->footnotes->addItem($footnote);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get endnotes
|
|
||||||
*
|
|
||||||
* @return \PhpOffice\PhpWord\Collection\Endnotes
|
|
||||||
*/
|
|
||||||
public function getEndnotes()
|
|
||||||
{
|
|
||||||
return $this->endnotes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add new endnote
|
|
||||||
*
|
|
||||||
* @param \PhpOffice\PhpWord\Element\Endnote $endnote
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function addEndnote($endnote)
|
|
||||||
{
|
|
||||||
return $this->endnotes->addItem($endnote);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get default font name
|
* Get default font name
|
||||||
*
|
*
|
||||||
|
|
@ -247,81 +244,6 @@ class PhpWord
|
||||||
return Style::setDefaultParagraphStyle($styles);
|
return Style::setDefaultParagraphStyle($styles);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a paragraph style definition to styles.xml
|
|
||||||
*
|
|
||||||
* @param string $styleName
|
|
||||||
* @param array $styles
|
|
||||||
* @return \PhpOffice\PhpWord\Style\Paragraph
|
|
||||||
*/
|
|
||||||
public function addParagraphStyle($styleName, $styles)
|
|
||||||
{
|
|
||||||
return Style::addParagraphStyle($styleName, $styles);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a font style definition to styles.xml
|
|
||||||
*
|
|
||||||
* @param string $styleName
|
|
||||||
* @param mixed $fontStyle
|
|
||||||
* @param mixed $paragraphStyle
|
|
||||||
* @return \PhpOffice\PhpWord\Style\Font
|
|
||||||
*/
|
|
||||||
public function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
|
|
||||||
{
|
|
||||||
return Style::addFontStyle($styleName, $fontStyle, $paragraphStyle);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a table style definition to styles.xml
|
|
||||||
*
|
|
||||||
* @param string $styleName
|
|
||||||
* @param mixed $styleTable
|
|
||||||
* @param mixed $styleFirstRow
|
|
||||||
* @return \PhpOffice\PhpWord\Style\Table
|
|
||||||
*/
|
|
||||||
public function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
|
|
||||||
{
|
|
||||||
return Style::addTableStyle($styleName, $styleTable, $styleFirstRow);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a numbering style
|
|
||||||
*
|
|
||||||
* @param string $styleName
|
|
||||||
* @param mixed $styles
|
|
||||||
* @return \PhpOffice\PhpWord\Style\Numbering
|
|
||||||
*/
|
|
||||||
public function addNumberingStyle($styleName, $styles)
|
|
||||||
{
|
|
||||||
return Style::addNumberingStyle($styleName, $styles);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a hyperlink style to styles.xml
|
|
||||||
*
|
|
||||||
* @param string $styleName
|
|
||||||
* @param mixed $styles
|
|
||||||
* @return \PhpOffice\PhpWord\Style\Font
|
|
||||||
*/
|
|
||||||
public function addLinkStyle($styleName, $styles)
|
|
||||||
{
|
|
||||||
return Style::addLinkStyle($styleName, $styles);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a heading style definition to styles.xml
|
|
||||||
*
|
|
||||||
* @param int $depth
|
|
||||||
* @param mixed $fontStyle
|
|
||||||
* @param mixed $paragraphStyle
|
|
||||||
* @return \PhpOffice\PhpWord\Style\Font
|
|
||||||
*/
|
|
||||||
public function addTitleStyle($depth, $fontStyle, $paragraphStyle = null)
|
|
||||||
{
|
|
||||||
return Style::addTitleStyle($depth, $fontStyle, $paragraphStyle);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load template by filename
|
* Load template by filename
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ class Content extends AbstractPart
|
||||||
$listItems = $xmlReader->getElements('text:list-item/text:p', $node);
|
$listItems = $xmlReader->getElements('text:list-item/text:p', $node);
|
||||||
foreach ($listItems as $listItem) {
|
foreach ($listItems as $listItem) {
|
||||||
// $listStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
|
// $listStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
|
||||||
$section->addListItem($listItem->nodeValue);
|
$section->addListItem($listItem->nodeValue, 0);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue