Border.php 6.5 KB

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