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