Replaced `array_key_exists` with `isset` for better performance.
This commit is contained in:
parent
9d73173dce
commit
4445fd3258
|
|
@ -92,7 +92,7 @@ abstract class AbstractContainer extends AbstractElement
|
|||
|
||||
// Run valid `add` command
|
||||
$function = strtolower($function);
|
||||
if (array_key_exists($function, $functions)) {
|
||||
if (isset($functions[$function])) {
|
||||
$element = $functions[$function];
|
||||
|
||||
// Special case for TextBreak
|
||||
|
|
@ -183,23 +183,22 @@ abstract class AbstractContainer extends AbstractElement
|
|||
*/
|
||||
private function checkValidity($method)
|
||||
{
|
||||
// Valid containers for each element
|
||||
$allContainers = array(
|
||||
'Section', 'Header', 'Footer', 'Footnote', 'Endnote',
|
||||
'Cell', 'TextRun', 'TextBox', 'ListItemRun',
|
||||
$generalContainers = array(
|
||||
'Section', 'Header', 'Footer', 'Footnote', 'Endnote', 'Cell', 'TextRun', 'TextBox', 'ListItemRun',
|
||||
);
|
||||
|
||||
$validContainers = array(
|
||||
'Text' => $allContainers,
|
||||
'Bookmark' => $allContainers,
|
||||
'Link' => $allContainers,
|
||||
'TextBreak' => $allContainers,
|
||||
'Image' => $allContainers,
|
||||
'Object' => $allContainers,
|
||||
'Field' => $allContainers,
|
||||
'Line' => $allContainers,
|
||||
'Shape' => $allContainers,
|
||||
'FormField' => $allContainers,
|
||||
'SDT' => $allContainers,
|
||||
'Text' => $generalContainers,
|
||||
'Bookmark' => $generalContainers,
|
||||
'Link' => $generalContainers,
|
||||
'TextBreak' => $generalContainers,
|
||||
'Image' => $generalContainers,
|
||||
'Object' => $generalContainers,
|
||||
'Field' => $generalContainers,
|
||||
'Line' => $generalContainers,
|
||||
'Shape' => $generalContainers,
|
||||
'FormField' => $generalContainers,
|
||||
'SDT' => $generalContainers,
|
||||
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
|
||||
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
|
||||
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
|
||||
|
|
@ -214,6 +213,7 @@ abstract class AbstractContainer extends AbstractElement
|
|||
'PageBreak' => array('Section'),
|
||||
'Chart' => array('Section'),
|
||||
);
|
||||
|
||||
// Special condition, e.g. preservetext can only exists in cell when
|
||||
// the cell is located in header or footer
|
||||
$validSubcontainers = array(
|
||||
|
|
@ -223,19 +223,20 @@ abstract class AbstractContainer extends AbstractElement
|
|||
);
|
||||
|
||||
// Check if a method is valid for current container
|
||||
if (array_key_exists($method, $validContainers)) {
|
||||
if (isset($validContainers[$method])) {
|
||||
if (!in_array($this->container, $validContainers[$method])) {
|
||||
throw new \BadMethodCallException("Cannot add $method in $this->container.");
|
||||
throw new \BadMethodCallException("Cannot add {$method} in {$this->container}.");
|
||||
}
|
||||
}
|
||||
|
||||
// Check if a method is valid for current container, located in other container
|
||||
if (array_key_exists($method, $validSubcontainers)) {
|
||||
if (isset($validSubcontainers[$method])) {
|
||||
$rules = $validSubcontainers[$method];
|
||||
$containers = $rules[0];
|
||||
$allowedDocParts = $rules[1];
|
||||
foreach ($containers as $container) {
|
||||
if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) {
|
||||
throw new \BadMethodCallException("Cannot add $method in $this->container.");
|
||||
throw new \BadMethodCallException("Cannot add {$method} in {$this->container}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ class Field extends AbstractElement
|
|||
public function setType($type = null)
|
||||
{
|
||||
if (isset($type)) {
|
||||
if (array_key_exists($type, $this->fieldsArray)) {
|
||||
if (isset($this->fieldsArray[$type])) {
|
||||
$this->type = $type;
|
||||
} else {
|
||||
throw new \InvalidArgumentException("Invalid type");
|
||||
|
|
@ -130,7 +130,7 @@ class Field extends AbstractElement
|
|||
{
|
||||
if (is_array($properties)) {
|
||||
foreach (array_keys($properties) as $propkey) {
|
||||
if (!(array_key_exists($propkey, $this->fieldsArray[$this->type]['properties']))) {
|
||||
if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) {
|
||||
throw new \InvalidArgumentException("Invalid property");
|
||||
}
|
||||
}
|
||||
|
|
@ -160,7 +160,7 @@ class Field extends AbstractElement
|
|||
{
|
||||
if (is_array($options)) {
|
||||
foreach (array_keys($options) as $optionkey) {
|
||||
if (!(array_key_exists($optionkey, $this->fieldsArray[$this->type]['options']))) {
|
||||
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey]))) {
|
||||
throw new \InvalidArgumentException("Invalid option");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,11 +61,10 @@ class Title extends AbstractElement
|
|||
*/
|
||||
public function __construct($text, $depth = 1)
|
||||
{
|
||||
|
||||
$this->text = String::toUTF8($text);
|
||||
$this->depth = $depth;
|
||||
if (array_key_exists('Heading_' . $this->depth, Style::getStyles())) {
|
||||
$this->style = 'Heading' . $this->depth;
|
||||
if (array_key_exists("Heading_{$this->depth}", Style::getStyles())) {
|
||||
$this->style = "Heading{$this->depth}";
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
|
|
|||
|
|
@ -48,12 +48,12 @@ class Media
|
|||
{
|
||||
// Assign unique media Id and initiate media container if none exists
|
||||
$mediaId = md5($container . $source);
|
||||
if (!array_key_exists($container, self::$elements)) {
|
||||
if (!isset(self::$elements[$container])) {
|
||||
self::$elements[$container] = array();
|
||||
}
|
||||
|
||||
// Add media if not exists or point to existing media
|
||||
if (!array_key_exists($mediaId, self::$elements[$container])) {
|
||||
if (!isset(self::$elements[$container][$mediaId])) {
|
||||
$mediaCount = self::countElements($container);
|
||||
$mediaTypeCount = self::countElements($container, $mediaType);
|
||||
$mediaTypeCount++;
|
||||
|
|
@ -120,7 +120,7 @@ class Media
|
|||
{
|
||||
$mediaCount = 0;
|
||||
|
||||
if (array_key_exists($container, self::$elements)) {
|
||||
if (isset(self::$elements[$container])) {
|
||||
foreach (self::$elements[$container] as $mediaData) {
|
||||
if (!is_null($mediaType)) {
|
||||
if ($mediaType == $mediaData['type']) {
|
||||
|
|
@ -156,7 +156,7 @@ class Media
|
|||
}
|
||||
return $elements;
|
||||
} else {
|
||||
if (!array_key_exists($container, self::$elements)) {
|
||||
if (!isset(self::$elements[$container])) {
|
||||
return $elements;
|
||||
}
|
||||
return self::getElementsByType($container, $type);
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ class PhpWord
|
|||
/** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collectionObject */
|
||||
$collectionObject = $this->collections[$key];
|
||||
|
||||
return $collectionObject->addItem(array_key_exists(0, $args) ? $args[0] : null);
|
||||
return $collectionObject->addItem(isset($args[0]) ? $args[0] : null);
|
||||
}
|
||||
|
||||
// Run add style method
|
||||
|
|
|
|||
|
|
@ -155,7 +155,7 @@ class Document
|
|||
$char = $this->rtf[$this->offset];
|
||||
$ascii = ord($char);
|
||||
|
||||
if (array_key_exists($ascii, $markers)) { // Marker found: {, }, \, LF, or CR
|
||||
if (isset($markers[$ascii])) { // Marker found: {, }, \, LF, or CR
|
||||
$markerFunction = $markers[$ascii];
|
||||
$this->$markerFunction();
|
||||
} else {
|
||||
|
|
@ -351,7 +351,7 @@ class Document
|
|||
'fldinst' => array(self::SKIP, 'link', null),
|
||||
);
|
||||
|
||||
if (array_key_exists($control, $controls)) {
|
||||
if (isset($controls[$control])) {
|
||||
list($function) = $controls[$control];
|
||||
if (method_exists($this, $function)) {
|
||||
$directives = $controls[$control];
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ class Word2007 extends AbstractReader implements ReaderInterface
|
|||
$stepItems = $step['stepItems'];
|
||||
foreach ($relationships[$stepPart] as $relItem) {
|
||||
$relType = $relItem['type'];
|
||||
if (array_key_exists($relType, $stepItems)) {
|
||||
if (isset($stepItems[$relType])) {
|
||||
$partName = $stepItems[$relType];
|
||||
$xmlFile = $relItem['target'];
|
||||
$this->readPart($phpWord, $relationships, $partName, $docFile, $xmlFile);
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ abstract class AbstractPart
|
|||
$headingMatches = array();
|
||||
if ($xmlReader->elementExists('w:pPr', $domNode)) {
|
||||
$paragraphStyle = $this->readParagraphStyle($xmlReader, $domNode);
|
||||
if (is_array($paragraphStyle) && array_key_exists('styleName', $paragraphStyle)) {
|
||||
if (is_array($paragraphStyle) && isset($paragraphStyle['styleName'])) {
|
||||
preg_match('/Heading(\d)/', $paragraphStyle['styleName'], $headingMatches);
|
||||
}
|
||||
}
|
||||
|
|
@ -505,10 +505,9 @@ abstract class AbstractPart
|
|||
private function getMediaTarget($docPart, $rId)
|
||||
{
|
||||
$target = null;
|
||||
if (array_key_exists($docPart, $this->rels)) {
|
||||
if (array_key_exists($rId, $this->rels[$docPart])) {
|
||||
$target = $this->rels[$docPart][$rId]['target'];
|
||||
}
|
||||
|
||||
if (isset($this->rels[$docPart]) && isset($this->rels[$docPart][$rId])) {
|
||||
$target = $this->rels[$docPart][$rId]['target'];
|
||||
}
|
||||
|
||||
return $target;
|
||||
|
|
|
|||
|
|
@ -67,12 +67,12 @@ class DocPropsCore extends AbstractPart
|
|||
$nodes = $xmlReader->getElements('*');
|
||||
if ($nodes->length > 0) {
|
||||
foreach ($nodes as $node) {
|
||||
if (!array_key_exists($node->nodeName, $this->mapping)) {
|
||||
if (!isset($this->mapping[$node->nodeName])) {
|
||||
continue;
|
||||
}
|
||||
$method = $this->mapping[$node->nodeName];
|
||||
$value = $node->nodeValue == '' ? null : $node->nodeValue;
|
||||
if (array_key_exists($node->nodeName, $this->callbacks)) {
|
||||
if (isset($this->callbacks[$node->nodeName])) {
|
||||
$value = $this->callbacks[$node->nodeName]($value);
|
||||
}
|
||||
if (method_exists($docProps, $method)) {
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ class Document extends AbstractPart
|
|||
if ($nodes->length > 0) {
|
||||
$section = $this->phpWord->addSection();
|
||||
foreach ($nodes as $node) {
|
||||
if (array_key_exists($node->nodeName, $readMethods)) {
|
||||
if (isset($readMethods[$node->nodeName])) {
|
||||
$readMethod = $readMethods[$node->nodeName];
|
||||
$this->$readMethod($xmlReader, $node, $section);
|
||||
}
|
||||
|
|
@ -72,9 +72,9 @@ class Document extends AbstractPart
|
|||
{
|
||||
$readMethods = array('w:p' => 'readParagraph', 'w:tbl' => 'readTable');
|
||||
|
||||
if (is_array($settings) && array_key_exists('hf', $settings)) {
|
||||
if (is_array($settings) && isset($settings['hf'])) {
|
||||
foreach ($settings['hf'] as $rId => $hfSetting) {
|
||||
if (array_key_exists($rId, $this->rels['document'])) {
|
||||
if (isset($this->rels['document'][$rId])) {
|
||||
list($hfType, $xmlFile, $docPart) = array_values($this->rels['document'][$rId]);
|
||||
$addMethod = "add{$hfType}";
|
||||
$hfObject = $section->$addMethod($hfSetting['type']);
|
||||
|
|
@ -85,7 +85,7 @@ class Document extends AbstractPart
|
|||
$nodes = $xmlReader->getElements('*');
|
||||
if ($nodes->length > 0) {
|
||||
foreach ($nodes as $node) {
|
||||
if (array_key_exists($node->nodeName, $readMethods)) {
|
||||
if (isset($readMethods[$node->nodeName])) {
|
||||
$readMethod = $readMethods[$node->nodeName];
|
||||
$this->$readMethod($xmlReader, $node, $hfObject, $docPart);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ class Footnotes extends AbstractPart
|
|||
|
||||
// Avoid w:type "separator" and "continuationSeparator"
|
||||
// Only look for <footnote> or <endnote> without w:type attribute
|
||||
if (is_null($type) && array_key_exists($id, $collection)) {
|
||||
if (is_null($type) && isset($collection[$id])) {
|
||||
$element = $collection[$id];
|
||||
$pNodes = $xmlReader->getElements('w:p/*', $node);
|
||||
foreach ($pNodes as $pNode) {
|
||||
|
|
|
|||
|
|
@ -133,8 +133,7 @@ class Html
|
|||
$newElement = null;
|
||||
$keys = array('node', 'element', 'styles', 'data', 'argument1', 'argument2');
|
||||
|
||||
if (array_key_exists($node->nodeName, $nodes)) {
|
||||
|
||||
if (isset($nodes[$node->nodeName])) {
|
||||
// Execute method based on node mapping table and return $newElement or null
|
||||
// Arguments are passed by reference
|
||||
$arguments = array();
|
||||
|
|
|
|||
|
|
@ -163,7 +163,7 @@ class Style
|
|||
*/
|
||||
public static function getStyle($styleName)
|
||||
{
|
||||
if (array_key_exists($styleName, self::$styles)) {
|
||||
if (isset(self::$styles[$styleName])) {
|
||||
return self::$styles[$styleName];
|
||||
} else {
|
||||
return null;
|
||||
|
|
@ -182,7 +182,7 @@ class Style
|
|||
*/
|
||||
private static function setStyleValues($name, $style, $value = null)
|
||||
{
|
||||
if (!array_key_exists($name, self::$styles)) {
|
||||
if (!isset(self::$styles[$name])) {
|
||||
if ($value !== null) {
|
||||
if (is_array($value)) {
|
||||
$style->setStyleByArray($value);
|
||||
|
|
|
|||
|
|
@ -343,7 +343,7 @@ abstract class AbstractWriter implements WriterInterface
|
|||
$type = $element['type']; // image|object|link
|
||||
|
||||
// Skip nonregistered types and set target
|
||||
if (!array_key_exists($type, $this->mediaPaths)) {
|
||||
if (!isset($this->mediaPaths[$type])) {
|
||||
continue;
|
||||
}
|
||||
$target = $this->mediaPaths[$type] . $element['target'];
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ class Body extends AbstractPart
|
|||
$method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
|
||||
$collection = $phpWord->$method()->getItems();
|
||||
|
||||
if (array_key_exists($noteTypeId, $collection)) {
|
||||
if (isset($collection[$noteTypeId])) {
|
||||
$element = $collection[$noteTypeId];
|
||||
$noteAnchor = "<a name=\"note-{$noteId}\" />";
|
||||
$noteAnchor .= "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ class Document extends AbstractPart
|
|||
$content .= '{';
|
||||
$content .= '\info';
|
||||
foreach ($properties as $property) {
|
||||
$method = 'get' . (array_key_exists($property, $mapping) ? $mapping[$property] : $property);
|
||||
$method = 'get' . (isset($mapping[$property]) ? $mapping[$property] : $property);
|
||||
$value = $docProps->$method();
|
||||
$value = in_array($property, $dateFields) ? $this->getDateValue($value) : $value;
|
||||
$content .= "{\\{$property} {$value}}";
|
||||
|
|
|
|||
|
|
@ -302,11 +302,11 @@ class Word2007 extends AbstractWriter implements WriterInterface
|
|||
$mediumType = $medium['type'];
|
||||
if ($mediumType == 'image') {
|
||||
$extension = $medium['imageExtension'];
|
||||
if (!array_key_exists($extension, $this->contentTypes['default'])) {
|
||||
if (!isset($this->contentTypes['default'][$extension])) {
|
||||
$this->contentTypes['default'][$extension] = $medium['imageType'];
|
||||
}
|
||||
} elseif ($mediumType == 'object') {
|
||||
if (!array_key_exists('bin', $this->contentTypes['default'])) {
|
||||
if (!isset($this->contentTypes['default']['bin'])) {
|
||||
$this->contentTypes['default']['bin'] = 'application/vnd.openxmlformats-officedocument.oleObject';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,8 +89,8 @@ class Rels extends AbstractPart
|
|||
$targetMapping = array('image' => 'media/', 'object' => 'embeddings/');
|
||||
|
||||
$mediaType = $mediaRel['type'];
|
||||
$type = array_key_exists($mediaType, $typeMapping) ? $typeMapping[$mediaType] : $mediaType;
|
||||
$targetPrefix = array_key_exists($mediaType, $targetMapping) ? $targetMapping[$mediaType] : '';
|
||||
$type = isset($typeMapping[$mediaType]) ? $typeMapping[$mediaType] : $mediaType;
|
||||
$targetPrefix = isset($targetMapping[$mediaType]) ? $targetMapping[$mediaType] : '';
|
||||
$target = $mediaRel['target'];
|
||||
$targetMode = ($type == 'hyperlink') ? 'External' : '';
|
||||
|
||||
|
|
|
|||
|
|
@ -19,9 +19,9 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part;
|
|||
|
||||
use PhpOffice\PhpWord\Settings as PhpWordSettings;
|
||||
use PhpOffice\PhpWord\Shared\XMLWriter;
|
||||
use PhpOffice\PhpWord\Style;
|
||||
use PhpOffice\PhpWord\Style\Font as FontStyle;
|
||||
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
|
||||
use PhpOffice\PhpWord\Style;
|
||||
use PhpOffice\PhpWord\Style\Table as TableStyle;
|
||||
use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
|
||||
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
|
||||
|
|
@ -114,14 +114,14 @@ class Styles extends AbstractPart
|
|||
$xmlWriter->startElement('w:name');
|
||||
$xmlWriter->writeAttribute('w:val', 'Normal');
|
||||
$xmlWriter->endElement(); // w:name
|
||||
if (array_key_exists('Normal', $styles)) {
|
||||
if (isset($styles['Normal'])) {
|
||||
$styleWriter = new ParagraphStyleWriter($xmlWriter, $styles['Normal']);
|
||||
$styleWriter->write();
|
||||
}
|
||||
$xmlWriter->endElement(); // w:style
|
||||
|
||||
// FootnoteReference style
|
||||
if (!array_key_exists('FootnoteReference', $styles)) {
|
||||
if (!isset($styles['FootnoteReference'])) {
|
||||
$xmlWriter->startElement('w:style');
|
||||
$xmlWriter->writeAttribute('w:type', 'character');
|
||||
$xmlWriter->writeAttribute('w:styleId', 'FootnoteReference');
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ class Frame extends AbstractStyle
|
|||
|
||||
// zIndex for infront & behind wrap
|
||||
$wrap = $style->getWrap();
|
||||
if ($wrap !== null && array_key_exists($wrap, $zIndices)) {
|
||||
if ($wrap !== null && isset($zIndices[$wrap])) {
|
||||
$styles['z-index'] = $zIndices[$wrap];
|
||||
$wrap = null;
|
||||
}
|
||||
|
|
@ -124,10 +124,10 @@ class Frame extends AbstractStyle
|
|||
$xmlWriter->writeAttribute('anchorx', "page");
|
||||
$xmlWriter->writeAttribute('anchory', "page");
|
||||
} elseif ($pos == FrameStyle::POS_RELATIVE) {
|
||||
if (array_key_exists($hPos, $relativePositions)) {
|
||||
if (isset($relativePositions[$hPos])) {
|
||||
$xmlWriter->writeAttribute('anchorx', $relativePositions[$hPos]);
|
||||
}
|
||||
if (array_key_exists($vPos, $relativePositions)) {
|
||||
if (isset($relativePositions[$vPos])) {
|
||||
$xmlWriter->writeAttribute('anchory', $relativePositions[$vPos]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ class Line extends Frame
|
|||
$xmlWriter->writeAttributeIf($style->getEndArrow() !== null, 'endarrow', $style->getEndArrow());
|
||||
|
||||
if ($dash !== null) {
|
||||
if (array_key_exists($dash, $dashStyles)) {
|
||||
if (isset($dashStyles[$dash])) {
|
||||
$xmlWriter->writeAttribute('dashstyle', $dashStyles[$dash]);
|
||||
}
|
||||
if ($dash == LineStyle::DASH_STYLE_ROUND_DOT) {
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ class MarginBorder extends AbstractStyle
|
|||
$xmlWriter->startElement('w:' . $side);
|
||||
if (!empty($this->colors)) {
|
||||
if ($color === null && !empty($this->attributes)) {
|
||||
if (array_key_exists('defaultColor', $this->attributes)) {
|
||||
if (isset($this->attributes['defaultColor'])) {
|
||||
$color = $this->attributes['defaultColor'];
|
||||
}
|
||||
}
|
||||
|
|
@ -91,7 +91,7 @@ class MarginBorder extends AbstractStyle
|
|||
$xmlWriter->writeAttribute('w:sz', $width);
|
||||
$xmlWriter->writeAttribute('w:color', $color);
|
||||
if (!empty($this->attributes)) {
|
||||
if (array_key_exists('space', $this->attributes)) {
|
||||
if (isset($this->attributes['space'])) {
|
||||
$xmlWriter->writeAttribute('w:space', $this->attributes['space']);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue