Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
 
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
 
7
class Border extends Supervisor
8
{
9
    // Border style
10
    const BORDER_NONE = 'none';
11
    const BORDER_DASHDOT = 'dashDot';
12
    const BORDER_DASHDOTDOT = 'dashDotDot';
13
    const BORDER_DASHED = 'dashed';
14
    const BORDER_DOTTED = 'dotted';
15
    const BORDER_DOUBLE = 'double';
16
    const BORDER_HAIR = 'hair';
17
    const BORDER_MEDIUM = 'medium';
18
    const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
19
    const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
20
    const BORDER_MEDIUMDASHED = 'mediumDashed';
21
    const BORDER_SLANTDASHDOT = 'slantDashDot';
22
    const BORDER_THICK = 'thick';
23
    const BORDER_THIN = 'thin';
24
    const BORDER_OMIT = 'omit'; // should be used only for Conditional
25
 
26
    /**
27
     * Border style.
28
     *
29
     * @var string
30
     */
31
    protected $borderStyle = self::BORDER_NONE;
32
 
33
    /**
34
     * Border color.
35
     *
36
     * @var Color
37
     */
38
    protected $color;
39
 
40
    /**
41
     * @var null|int
42
     */
43
    public $colorIndex;
44
 
45
    /**
46
     * Create a new Border.
47
     *
48
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
49
     *                                    Leave this value at default unless you understand exactly what
50
     *                                        its ramifications are
51
     */
52
    public function __construct($isSupervisor = false, bool $isConditional = false)
53
    {
54
        // Supervisor?
55
        parent::__construct($isSupervisor);
56
 
57
        // Initialise values
58
        $this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
59
 
60
        // bind parent if we are a supervisor
61
        if ($isSupervisor) {
62
            $this->color->bindParent($this, 'color');
63
        }
64
        if ($isConditional) {
65
            $this->borderStyle = self::BORDER_OMIT;
66
        }
67
    }
68
 
69
    /**
70
     * Get the shared style component for the currently active cell in currently active sheet.
71
     * Only used for style supervisor.
72
     *
73
     * @return Border
74
     */
75
    public function getSharedComponent()
76
    {
77
        /** @var Style */
78
        $parent = $this->parent;
79
 
80
        /** @var Borders $sharedComponent */
81
        $sharedComponent = $parent->getSharedComponent();
82
        switch ($this->parentPropertyName) {
83
            case 'bottom':
84
                return $sharedComponent->getBottom();
85
            case 'diagonal':
86
                return $sharedComponent->getDiagonal();
87
            case 'left':
88
                return $sharedComponent->getLeft();
89
            case 'right':
90
                return $sharedComponent->getRight();
91
            case 'top':
92
                return $sharedComponent->getTop();
93
        }
94
 
95
        throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.');
96
    }
97
 
98
    /**
99
     * Build style array from subcomponents.
100
     *
101
     * @param array $array
102
     *
103
     * @return array
104
     */
105
    public function getStyleArray($array)
106
    {
107
        /** @var Style */
108
        $parent = $this->parent;
109
 
110
        return $parent->/** @scrutinizer ignore-call */ getStyleArray([$this->parentPropertyName => $array]);
111
    }
112
 
113
    /**
114
     * Apply styles from array.
115
     *
116
     * <code>
117
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
118
     *        [
119
     *            'borderStyle' => Border::BORDER_DASHDOT,
120
     *            'color' => [
121
     *                'rgb' => '808080'
122
     *            ]
123
     *        ]
124
     * );
125
     * </code>
126
     *
127
     * @param array $styleArray Array containing style information
128
     *
129
     * @return $this
130
     */
131
    public function applyFromArray(array $styleArray)
132
    {
133
        if ($this->isSupervisor) {
134
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
135
        } else {
136
            if (isset($styleArray['borderStyle'])) {
137
                $this->setBorderStyle($styleArray['borderStyle']);
138
            }
139
            if (isset($styleArray['color'])) {
140
                $this->getColor()->applyFromArray($styleArray['color']);
141
            }
142
        }
143
 
144
        return $this;
145
    }
146
 
147
    /**
148
     * Get Border style.
149
     *
150
     * @return string
151
     */
152
    public function getBorderStyle()
153
    {
154
        if ($this->isSupervisor) {
155
            return $this->getSharedComponent()->getBorderStyle();
156
        }
157
 
158
        return $this->borderStyle;
159
    }
160
 
161
    /**
162
     * Set Border style.
163
     *
164
     * @param bool|string $style
165
     *                            When passing a boolean, FALSE equates Border::BORDER_NONE
166
     *                                and TRUE to Border::BORDER_MEDIUM
167
     *
168
     * @return $this
169
     */
170
    public function setBorderStyle($style)
171
    {
172
        if (empty($style)) {
173
            $style = self::BORDER_NONE;
174
        } elseif (is_bool($style)) {
175
            $style = self::BORDER_MEDIUM;
176
        }
177
 
178
        if ($this->isSupervisor) {
179
            $styleArray = $this->getStyleArray(['borderStyle' => $style]);
180
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
181
        } else {
182
            $this->borderStyle = $style;
183
        }
184
 
185
        return $this;
186
    }
187
 
188
    /**
189
     * Get Border Color.
190
     *
191
     * @return Color
192
     */
193
    public function getColor()
194
    {
195
        return $this->color;
196
    }
197
 
198
    /**
199
     * Set Border Color.
200
     *
201
     * @return $this
202
     */
203
    public function setColor(Color $color)
204
    {
205
        // make sure parameter is a real color and not a supervisor
206
        $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
207
 
208
        if ($this->isSupervisor) {
209
            $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
210
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
211
        } else {
212
            $this->color = $color;
213
        }
214
 
215
        return $this;
216
    }
217
 
218
    /**
219
     * Get hash code.
220
     *
221
     * @return string Hash code
222
     */
223
    public function getHashCode()
224
    {
225
        if ($this->isSupervisor) {
226
            return $this->getSharedComponent()->getHashCode();
227
        }
228
 
229
        return md5(
230
            $this->borderStyle .
231
            $this->color->getHashCode() .
232
            __CLASS__
233
        );
234
    }
235
 
236
    protected function exportArray1(): array
237
    {
238
        $exportedArray = [];
239
        $this->exportArray2($exportedArray, 'borderStyle', $this->getBorderStyle());
240
        $this->exportArray2($exportedArray, 'color', $this->getColor());
241
 
242
        return $exportedArray;
243
    }
244
}