1<?php 2 3namespace dokuwiki\plugin\wordimport\docx; 4 5/** 6 * A table cell 7 * 8 * A table cell is not really a paragraph but we treat it as one for simplicity. However it contains paragraphs. 9 */ 10class TableCell extends AbstractParagraph 11{ 12 /** @var bool Is this a placeholder for a vertically merged cell? It has no content then */ 13 protected $vmerge = false; 14 /** @var int The horizontal span of this cell */ 15 protected $span = 1; 16 /** @var Paragraph[] All contained paragraphs */ 17 protected $paragraphs = []; 18 19 /** @inheritdoc */ 20 public function parse() 21 { 22 $this->p->asXML(); 23 24 // vertical merge 25 $vMerge = $this->p->xpath('w:tcPr/w:vMerge'); 26 $this->vmerge = $vMerge && ((string)$vMerge[0]->attributes('w', true)->val !== 'restart'); 27 28 // horizontal span 29 $span = $this->p->xpath('w:tcPr/w:gridSpan'); 30 $this->span = $span ? (int)$span[0]->attributes('w', true)->val : 1; 31 32 $paragraphs = $this->p->xpath('w:p'); 33 foreach ($paragraphs as $paragraph) { 34 // FIXME theoretically we would need to use the Document's factory again 35 // because a table might contain images, OTOH a table may not contain headings or lists 36 $p = new Paragraph($this->docx, $paragraph); 37 $p->parse(); 38 $this->paragraphs[] = $p; 39 } 40 } 41 42 /** 43 * Outputs the cell with closing pipes 44 * 45 * The opening pipe is create by the Table class for each row. 46 * 47 * @inheritdoc 48 */ 49 public function __toString(): string 50 { 51 if ($this->vmerge) { 52 $string = ":::"; 53 } else { 54 $string = implode('\\\\ ', array_map(static fn($p) => $p->__toString(), $this->paragraphs)); 55 } 56 57 if ($this->paragraphs) { 58 $string = $this->paragraphs[0]->alignmentPadding($string); 59 } 60 61 $string = " $string "; // add one space for nicer layout 62 $string .= str_repeat('|', $this->span); 63 return $string; 64 } 65} 66