Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.98% |
53 / 57 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Cell | |
92.98% |
53 / 57 |
|
33.33% |
1 / 3 |
26.23 | |
0.00% |
0 / 1 |
| write | |
97.62% |
41 / 42 |
|
0.00% |
0 / 1 |
10 | |||
| writeBorder | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| convertBorderStyle | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
19.26 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace PhpOffice\PhpWord\Writer\ODText\Style; |
| 4 | |
| 5 | use PhpOffice\PhpWord\Shared\XMLWriter; |
| 6 | use PhpOffice\PhpWord\Style\Cell as CellStyle; |
| 7 | |
| 8 | /** |
| 9 | * Table cell style writer. |
| 10 | */ |
| 11 | class Cell extends AbstractStyle |
| 12 | { |
| 13 | public function write(): void |
| 14 | { |
| 15 | $style = $this->getStyle(); |
| 16 | if (!$style instanceof CellStyle) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | $xmlWriter = $this->getXmlWriter(); |
| 21 | $xmlWriter->startElement('style:style'); |
| 22 | $xmlWriter->writeAttribute('style:name', $style->getStyleName()); |
| 23 | $xmlWriter->writeAttribute('style:family', 'table-cell'); |
| 24 | $xmlWriter->startElement('style:table-cell-properties'); |
| 25 | |
| 26 | if ($style->getBgColor() !== null) { |
| 27 | $xmlWriter->writeAttribute('fo:background-color', '#' . ltrim($style->getBgColor(), '#')); |
| 28 | } |
| 29 | |
| 30 | $this->writeBorder($xmlWriter, 'top', $style->getBorderTopSize(), $style->getBorderTopColor(), $style->getBorderTopStyle()); |
| 31 | $this->writeBorder($xmlWriter, 'bottom', $style->getBorderBottomSize(), $style->getBorderBottomColor(), $style->getBorderBottomStyle()); |
| 32 | $this->writeBorder($xmlWriter, 'left', $style->getBorderLeftSize(), $style->getBorderLeftColor(), $style->getBorderLeftStyle()); |
| 33 | $this->writeBorder($xmlWriter, 'right', $style->getBorderRightSize(), $style->getBorderRightColor(), $style->getBorderRightStyle()); |
| 34 | |
| 35 | $padding = [ |
| 36 | 'top' => $style->getPaddingTop(), |
| 37 | 'bottom' => $style->getPaddingBottom(), |
| 38 | 'left' => $style->getPaddingLeft(), |
| 39 | 'right' => $style->getPaddingRight(), |
| 40 | ]; |
| 41 | foreach ($padding as $side => $value) { |
| 42 | if ($value !== null) { |
| 43 | $xmlWriter->writeAttribute('fo:padding-' . $side, number_format($value / 1440, 3, '.', '') . 'in'); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | $verticalAlign = [ |
| 48 | 'top' => 'top', |
| 49 | 'center' => 'middle', |
| 50 | 'both' => 'automatic', |
| 51 | 'bottom' => 'bottom', |
| 52 | ]; |
| 53 | if ($style->getVAlign() !== null && isset($verticalAlign[$style->getVAlign()])) { |
| 54 | $xmlWriter->writeAttribute('style:vertical-align', $verticalAlign[$style->getVAlign()]); |
| 55 | } |
| 56 | |
| 57 | $writingMode = [ |
| 58 | CellStyle::TEXT_DIR_LRTB => 'lr-tb', |
| 59 | CellStyle::TEXT_DIR_TBRL => 'tb-rl', |
| 60 | CellStyle::TEXT_DIR_BTLR => 'bt-lr', |
| 61 | ]; |
| 62 | if ($style->getTextDirection() !== null && isset($writingMode[$style->getTextDirection()])) { |
| 63 | $xmlWriter->writeAttribute('style:writing-mode', $writingMode[$style->getTextDirection()]); |
| 64 | } |
| 65 | |
| 66 | if ($style->getNoWrap() === false) { |
| 67 | $xmlWriter->writeAttribute('fo:wrap-option', 'wrap'); |
| 68 | } |
| 69 | |
| 70 | $xmlWriter->endElement(); // style:table-cell-properties |
| 71 | $xmlWriter->endElement(); // style:style |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param null|float|int $size |
| 76 | */ |
| 77 | private function writeBorder(XMLWriter $xmlWriter, string $side, $size, ?string $color, ?string $style): void |
| 78 | { |
| 79 | if ($size === null) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | $xmlWriter->writeAttribute( |
| 84 | 'fo:border-' . $side, |
| 85 | number_format($size / 20, 3, '.', '') . 'pt ' . $this->convertBorderStyle($style) . ' #' . ltrim($color ?: CellStyle::DEFAULT_BORDER_COLOR, '#') |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | private function convertBorderStyle(?string $style): string |
| 90 | { |
| 91 | if ($style === null || $style === 'single' || $style === 'thick' || $style === 'inset' || $style === 'outset' || $style === 'wave') { |
| 92 | return 'solid'; |
| 93 | } |
| 94 | if ($style === 'double' || $style === 'triple') { |
| 95 | return 'double'; |
| 96 | } |
| 97 | if ($style === 'dotted' || strpos($style, 'dot') !== false) { |
| 98 | return 'dotted'; |
| 99 | } |
| 100 | if ($style === 'none' || $style === 'nil') { |
| 101 | return 'none'; |
| 102 | } |
| 103 | |
| 104 | return 'dashed'; |
| 105 | } |
| 106 | } |