| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Xml\Style; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Style\Border as BorderStyle; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Style\Borders; |
| 7 |
use SimpleXMLElement; |
| 8 |
|
| 9 |
class Border extends StyleBase |
| 10 |
{ |
| 11 |
protected const BORDER_POSITIONS = [ |
| 12 |
'top', |
| 13 |
'left', |
| 14 |
'bottom', |
| 15 |
'right', |
| 16 |
]; |
| 17 |
|
| 18 |
public const BORDER_MAPPINGS = [ |
| 19 |
'borderStyle' => [ |
| 20 |
'continuous' => BorderStyle::BORDER_HAIR, |
| 21 |
'dash' => BorderStyle::BORDER_DASHED, |
| 22 |
'dashdot' => BorderStyle::BORDER_DASHDOT, |
| 23 |
'dashdotdot' => BorderStyle::BORDER_DASHDOTDOT, |
| 24 |
'dot' => BorderStyle::BORDER_DOTTED, |
| 25 |
'double' => BorderStyle::BORDER_DOUBLE, |
| 26 |
'0continuous' => BorderStyle::BORDER_HAIR, |
| 27 |
'0dash' => BorderStyle::BORDER_DASHED, |
| 28 |
'0dashdot' => BorderStyle::BORDER_DASHDOT, |
| 29 |
'0dashdotdot' => BorderStyle::BORDER_DASHDOTDOT, |
| 30 |
'0dot' => BorderStyle::BORDER_DOTTED, |
| 31 |
'0double' => BorderStyle::BORDER_DOUBLE, |
| 32 |
'1continuous' => BorderStyle::BORDER_THIN, |
| 33 |
'1dash' => BorderStyle::BORDER_DASHED, |
| 34 |
'1dashdot' => BorderStyle::BORDER_DASHDOT, |
| 35 |
'1dashdotdot' => BorderStyle::BORDER_DASHDOTDOT, |
| 36 |
'1dot' => BorderStyle::BORDER_DOTTED, |
| 37 |
'1double' => BorderStyle::BORDER_DOUBLE, |
| 38 |
'2continuous' => BorderStyle::BORDER_MEDIUM, |
| 39 |
'2dash' => BorderStyle::BORDER_MEDIUMDASHED, |
| 40 |
'2dashdot' => BorderStyle::BORDER_MEDIUMDASHDOT, |
| 41 |
'2dashdotdot' => BorderStyle::BORDER_MEDIUMDASHDOTDOT, |
| 42 |
'2dot' => BorderStyle::BORDER_DOTTED, |
| 43 |
'2double' => BorderStyle::BORDER_DOUBLE, |
| 44 |
'3continuous' => BorderStyle::BORDER_THICK, |
| 45 |
'3dash' => BorderStyle::BORDER_MEDIUMDASHED, |
| 46 |
'3dashdot' => BorderStyle::BORDER_MEDIUMDASHDOT, |
| 47 |
'3dashdotdot' => BorderStyle::BORDER_MEDIUMDASHDOTDOT, |
| 48 |
'3dot' => BorderStyle::BORDER_DOTTED, |
| 49 |
'3double' => BorderStyle::BORDER_DOUBLE, |
| 50 |
], |
| 51 |
]; |
| 52 |
|
| 53 |
/** |
| 54 |
* @param string[] $namespaces |
| 55 |
* |
| 56 |
* @return mixed[] |
| 57 |
*/ |
| 58 |
public function parseStyle(SimpleXMLElement $styleData, array $namespaces): array |
| 59 |
{ |
| 60 |
$style = []; |
| 61 |
|
| 62 |
$diagonalDirection = Borders::DIAGONAL_NONE; |
| 63 |
foreach ($styleData->Border as $borderStyle) { |
| 64 |
$borderAttributes = self::getAttributes($borderStyle, $namespaces['ss']); |
| 65 |
/** @var array{color?: array{rgb: string}, borderStyle: string} */ |
| 66 |
$thisBorder = []; |
| 67 |
$styleType = (string) $borderAttributes->Weight; |
| 68 |
$styleType .= strtolower((string) $borderAttributes->LineStyle); |
| 69 |
$thisBorder['borderStyle'] = self::BORDER_MAPPINGS['borderStyle'][$styleType] ?? BorderStyle::BORDER_NONE; |
| 70 |
|
| 71 |
$color = (string) ($borderAttributes['Color'] ?? ''); |
| 72 |
if ($color !== '') { |
| 73 |
$thisBorder['color']['rgb'] = (string) substr($color, 1); |
| 74 |
} |
| 75 |
$position = (string) ($borderAttributes['Position'] ?? ''); |
| 76 |
if ($position !== '') { |
| 77 |
[$borderPosition, $diagonalDirection] = $this->parsePosition($position, $diagonalDirection); |
| 78 |
if ($borderPosition) { |
| 79 |
$style['borders'][$borderPosition] = $thisBorder; |
| 80 |
} elseif ($diagonalDirection !== Borders::DIAGONAL_NONE) { |
| 81 |
$style['borders']['diagonalDirection'] = $diagonalDirection; |
| 82 |
$style['borders']['diagonal'] = $thisBorder; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return $style; |
| 88 |
} |
| 89 |
|
| 90 |
/** @return array{0: string, 1: int} */ |
| 91 |
protected function parsePosition(string $borderStyleValue, int $diagonalDirection): array |
| 92 |
{ |
| 93 |
$borderStyleValue = strtolower($borderStyleValue); |
| 94 |
$borderPosition = ''; |
| 95 |
|
| 96 |
if (in_array($borderStyleValue, self::BORDER_POSITIONS)) { |
| 97 |
$borderPosition = $borderStyleValue; |
| 98 |
} elseif ($borderStyleValue === 'diagonalleft') { |
| 99 |
$diagonalDirection = ($diagonalDirection !== Borders::DIAGONAL_NONE) ? Borders::DIAGONAL_BOTH : Borders::DIAGONAL_DOWN; |
| 100 |
} elseif ($borderStyleValue === 'diagonalright') { |
| 101 |
$diagonalDirection = ($diagonalDirection !== Borders::DIAGONAL_NONE) ? Borders::DIAGONAL_BOTH : Borders::DIAGONAL_UP; |
| 102 |
} |
| 103 |
|
| 104 |
return [$borderPosition, $diagonalDirection]; |
| 105 |
} |
| 106 |
} |
| 107 |
|