From 6951890a1d0138a1e54610c83106dbba51980427 Mon Sep 17 00:00:00 2001 From: Artem Vasilev Date: Tue, 6 Dec 2022 19:04:18 +0300 Subject: [PATCH] Read cell borders and margins --- src/PhpWord/Reader/Word2007/AbstractPart.php | 39 ++++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index df3cfed709..ca04857dce 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -364,9 +364,8 @@ protected function readTable(XMLReader $xmlReader, DOMElement $domNode, $parent, } elseif ('w:tc' == $rowNode->nodeName) { // Cell $cellWidth = $xmlReader->getAttribute('w:w', $rowNode, 'w:tcPr/w:tcW'); $cellStyle = null; - $cellStyleNode = $xmlReader->getElement('w:tcPr', $rowNode); - if (null !== $cellStyleNode) { - $cellStyle = $this->readCellStyle($xmlReader, $cellStyleNode); + if ($xmlReader->elementExists('w:tcPr', $rowNode)) { + $cellStyle = $this->readCellStyle($xmlReader, $rowNode); } $cell = $row->addCell($cellWidth, $cellStyle); @@ -554,15 +553,33 @@ private function readTableIndent(XMLReader $xmlReader, DOMElement $domNode) */ private function readCellStyle(XMLReader $xmlReader, DOMElement $domNode) { - $styleDefs = [ - 'valign' => [self::READ_VALUE, 'w:vAlign'], - 'textDirection' => [self::READ_VALUE, 'w:textDirection'], - 'gridSpan' => [self::READ_VALUE, 'w:gridSpan'], - 'vMerge' => [self::READ_VALUE, 'w:vMerge', null, null, 'continue'], - 'bgColor' => [self::READ_VALUE, 'w:shd', 'w:fill'], - ]; + $style = null; + $margins = ['top', 'left', 'bottom', 'right']; + $borders = array_merge($margins, ['insideH', 'insideV']); - return $this->readStyleDefs($xmlReader, $domNode, $styleDefs); + if ($xmlReader->elementExists('w:tcPr', $domNode)) { + $styleNode = $xmlReader->getElement('w:tcPr', $domNode); + $styleDefs = []; + foreach ($margins as $side) { + $ucfSide = ucfirst($side); + $styleDefs["cellMargin$ucfSide"] = [self::READ_VALUE, "w:tcMar/w:$side", 'w:w']; + } + foreach ($borders as $side) { + $ucfSide = ucfirst($side); + $styleDefs["border{$ucfSide}Size"] = [self::READ_VALUE, "w:tcBorders/w:$side", 'w:sz']; + $styleDefs["border{$ucfSide}Color"] = [self::READ_VALUE, "w:tcBorders/w:$side", 'w:color']; + $styleDefs["border{$ucfSide}Style"] = [self::READ_VALUE, "w:tcBorders/w:$side", 'w:val']; + } + $styleDefs['valign'] = [self::READ_VALUE, 'w:vAlign']; + $styleDefs['textDirection'] = [self::READ_VALUE, 'w:textDirection']; + $styleDefs['gridSpan'] = [self::READ_VALUE, 'w:gridSpan']; + $styleDefs['vMerge'] = [self::READ_VALUE, 'w:vMerge', null, null, 'continue']; + $styleDefs['bgColor'] = [self::READ_VALUE, 'w:shd', 'w:fill']; + + $style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs); + } + + return $style; } /**