3 namespace PhpOffice\PhpSpreadsheet\Style
;
5 use PhpOffice\PhpSpreadsheet\Exception
as PhpSpreadsheetException
;
7 class Borders
extends Supervisor
10 const DIAGONAL_NONE
= 0;
11 const DIAGONAL_UP
= 1;
12 const DIAGONAL_DOWN
= 2;
13 const DIAGONAL_BOTH
= 3;
55 protected $diagonalDirection;
58 * All borders pseudo-border. Only applies to supervisor.
62 protected $allBorders;
65 * Outline pseudo-border. Only applies to supervisor.
72 * Inside pseudo-border. Only applies to supervisor.
79 * Vertical pseudo-border. Only applies to supervisor.
86 * Horizontal pseudo-border. Only applies to supervisor.
90 protected $horizontal;
93 * Create a new Borders.
95 * @param bool $isSupervisor Flag indicating if this is a supervisor or not
96 * Leave this value at default unless you understand exactly what
97 * its ramifications are
98 * @param bool $isConditional Flag indicating if this is a conditional style or not
99 * Leave this value at default unless you understand exactly what
100 * its ramifications are
102 public function __construct($isSupervisor = false, $isConditional = false)
105 parent
::__construct($isSupervisor);
108 $this->left
= new Border($isSupervisor, $isConditional);
109 $this->right
= new Border($isSupervisor, $isConditional);
110 $this->top
= new Border($isSupervisor, $isConditional);
111 $this->bottom
= new Border($isSupervisor, $isConditional);
112 $this->diagonal
= new Border($isSupervisor, $isConditional);
113 $this->diagonalDirection
= self
::DIAGONAL_NONE
;
115 // Specially for supervisor
117 // Initialize pseudo-borders
118 $this->allBorders
= new Border(true);
119 $this->outline
= new Border(true);
120 $this->inside
= new Border(true);
121 $this->vertical
= new Border(true);
122 $this->horizontal
= new Border(true);
124 // bind parent if we are a supervisor
125 $this->left
->bindParent($this, 'left');
126 $this->right
->bindParent($this, 'right');
127 $this->top
->bindParent($this, 'top');
128 $this->bottom
->bindParent($this, 'bottom');
129 $this->diagonal
->bindParent($this, 'diagonal');
130 $this->allBorders
->bindParent($this, 'allBorders');
131 $this->outline
->bindParent($this, 'outline');
132 $this->inside
->bindParent($this, 'inside');
133 $this->vertical
->bindParent($this, 'vertical');
134 $this->horizontal
->bindParent($this, 'horizontal');
139 * Get the shared style component for the currently active cell in currently active sheet.
140 * Only used for style supervisor.
144 public function getSharedComponent()
146 return $this->parent
->getSharedComponent()->getBorders();
150 * Build style array from subcomponents.
152 * @param array $array
156 public function getStyleArray($array)
158 return ['borders' => $array];
162 * Apply styles from array.
165 * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
168 * 'borderStyle' => Border::BORDER_DASHDOT,
174 * 'borderStyle' => Border::BORDER_DASHDOT,
184 * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
187 * 'borderStyle' => Border::BORDER_DASHDOT,
196 * @param array $pStyles Array containing style information
198 * @throws PhpSpreadsheetException
202 public function applyFromArray(array $pStyles)
204 if ($this->isSupervisor
) {
205 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
207 if (isset($pStyles['left'])) {
208 $this->getLeft()->applyFromArray($pStyles['left']);
210 if (isset($pStyles['right'])) {
211 $this->getRight()->applyFromArray($pStyles['right']);
213 if (isset($pStyles['top'])) {
214 $this->getTop()->applyFromArray($pStyles['top']);
216 if (isset($pStyles['bottom'])) {
217 $this->getBottom()->applyFromArray($pStyles['bottom']);
219 if (isset($pStyles['diagonal'])) {
220 $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
222 if (isset($pStyles['diagonalDirection'])) {
223 $this->setDiagonalDirection($pStyles['diagonalDirection']);
225 if (isset($pStyles['allBorders'])) {
226 $this->getLeft()->applyFromArray($pStyles['allBorders']);
227 $this->getRight()->applyFromArray($pStyles['allBorders']);
228 $this->getTop()->applyFromArray($pStyles['allBorders']);
229 $this->getBottom()->applyFromArray($pStyles['allBorders']);
241 public function getLeft()
251 public function getRight()
261 public function getTop()
271 public function getBottom()
273 return $this->bottom
;
281 public function getDiagonal()
283 return $this->diagonal
;
287 * Get AllBorders (pseudo-border). Only applies to supervisor.
289 * @throws PhpSpreadsheetException
293 public function getAllBorders()
295 if (!$this->isSupervisor
) {
296 throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
299 return $this->allBorders
;
303 * Get Outline (pseudo-border). Only applies to supervisor.
305 * @throws PhpSpreadsheetException
309 public function getOutline()
311 if (!$this->isSupervisor
) {
312 throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
315 return $this->outline
;
319 * Get Inside (pseudo-border). Only applies to supervisor.
321 * @throws PhpSpreadsheetException
325 public function getInside()
327 if (!$this->isSupervisor
) {
328 throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
331 return $this->inside
;
335 * Get Vertical (pseudo-border). Only applies to supervisor.
337 * @throws PhpSpreadsheetException
341 public function getVertical()
343 if (!$this->isSupervisor
) {
344 throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
347 return $this->vertical
;
351 * Get Horizontal (pseudo-border). Only applies to supervisor.
353 * @throws PhpSpreadsheetException
357 public function getHorizontal()
359 if (!$this->isSupervisor
) {
360 throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
363 return $this->horizontal
;
367 * Get DiagonalDirection.
371 public function getDiagonalDirection()
373 if ($this->isSupervisor
) {
374 return $this->getSharedComponent()->getDiagonalDirection();
377 return $this->diagonalDirection
;
381 * Set DiagonalDirection.
383 * @param int $pValue see self::DIAGONAL_*
387 public function setDiagonalDirection($pValue)
390 $pValue = self
::DIAGONAL_NONE
;
392 if ($this->isSupervisor
) {
393 $styleArray = $this->getStyleArray(['diagonalDirection' => $pValue]);
394 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
396 $this->diagonalDirection
= $pValue;
405 * @return string Hash code
407 public function getHashCode()
409 if ($this->isSupervisor
) {
410 return $this->getSharedComponent()->getHashcode();
414 $this->getLeft()->getHashCode() .
415 $this->getRight()->getHashCode() .
416 $this->getTop()->getHashCode() .
417 $this->getBottom()->getHashCode() .
418 $this->getDiagonal()->getHashCode() .
419 $this->getDiagonalDirection() .