Skip to content

Commit 097bea5

Browse files
authored
Merge pull request #4723 from oleibman/xmltodo
Eliminate TODO in Reader/Xml/Style/Border
2 parents 4d597c1 + b1b819e commit 097bea5

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

src/PhpSpreadsheet/Reader/Xml/Style/Border.php

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,58 +59,48 @@ public function parseStyle(SimpleXMLElement $styleData, array $namespaces): arra
5959
{
6060
$style = [];
6161

62-
$diagonalDirection = '';
63-
$borderPosition = '';
62+
$diagonalDirection = Borders::DIAGONAL_NONE;
6463
foreach ($styleData->Border as $borderStyle) {
6564
$borderAttributes = self::getAttributes($borderStyle, $namespaces['ss']);
65+
/** @var array{color?: array{rgb: string}, borderStyle: string} */
6666
$thisBorder = [];
6767
$styleType = (string) $borderAttributes->Weight;
6868
$styleType .= strtolower((string) $borderAttributes->LineStyle);
6969
$thisBorder['borderStyle'] = self::BORDER_MAPPINGS['borderStyle'][$styleType] ?? BorderStyle::BORDER_NONE;
7070

71-
foreach ($borderAttributes as $borderStyleKey => $borderStyleValuex) {
72-
$borderStyleValue = (string) $borderStyleValuex;
73-
switch ($borderStyleKey) {
74-
case 'Position':
75-
/** @var string $diagonalDirection */
76-
[$borderPosition, $diagonalDirection]
77-
= $this->parsePosition($borderStyleValue, $diagonalDirection);
78-
79-
break;
80-
case 'Color':
81-
$borderColour = substr($borderStyleValue, 1);
82-
$thisBorder['color']['rgb'] = $borderColour;
83-
84-
break;
85-
}
71+
$color = (string) ($borderAttributes['Color'] ?? '');
72+
if ($color !== '') {
73+
$thisBorder['color']['rgb'] = substr($color, 1);
8674
}
87-
88-
/** @var int|string $borderPosition */
89-
if ($borderPosition) {
90-
$style['borders'][$borderPosition] = $thisBorder;
91-
} elseif ($diagonalDirection) {
92-
$style['borders']['diagonalDirection'] = $diagonalDirection;
93-
$style['borders']['diagonal'] = $thisBorder;
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+
}
9484
}
9585
}
9686

9787
return $style;
9888
}
9989

100-
/** @return mixed[] */
101-
protected function parsePosition(string $borderStyleValue, string $diagonalDirection): array
90+
/** @return array{0: string, 1: int} */
91+
protected function parsePosition(string $borderStyleValue, int $diagonalDirection): array
10292
{
103-
// TODO diagonalDirection seems to return int not string
10493
$borderStyleValue = strtolower($borderStyleValue);
94+
$borderPosition = '';
10595

10696
if (in_array($borderStyleValue, self::BORDER_POSITIONS)) {
10797
$borderPosition = $borderStyleValue;
10898
} elseif ($borderStyleValue === 'diagonalleft') {
109-
$diagonalDirection = $diagonalDirection ? Borders::DIAGONAL_BOTH : Borders::DIAGONAL_DOWN;
99+
$diagonalDirection = ($diagonalDirection !== Borders::DIAGONAL_NONE) ? Borders::DIAGONAL_BOTH : Borders::DIAGONAL_DOWN;
110100
} elseif ($borderStyleValue === 'diagonalright') {
111-
$diagonalDirection = $diagonalDirection ? Borders::DIAGONAL_BOTH : Borders::DIAGONAL_UP;
101+
$diagonalDirection = ($diagonalDirection !== Borders::DIAGONAL_NONE) ? Borders::DIAGONAL_BOTH : Borders::DIAGONAL_UP;
112102
}
113103

114-
return [$borderPosition ?? null, $diagonalDirection];
104+
return [$borderPosition, $diagonalDirection];
115105
}
116106
}

0 commit comments

Comments
 (0)