Protection.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Style;
  3. class Protection extends Supervisor
  4. {
  5. /** Protection styles */
  6. const PROTECTION_INHERIT = 'inherit';
  7. const PROTECTION_PROTECTED = 'protected';
  8. const PROTECTION_UNPROTECTED = 'unprotected';
  9. /**
  10. * Locked.
  11. *
  12. * @var string
  13. */
  14. protected $locked;
  15. /**
  16. * Hidden.
  17. *
  18. * @var string
  19. */
  20. protected $hidden;
  21. /**
  22. * Create a new Protection.
  23. *
  24. * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  25. * Leave this value at default unless you understand exactly what
  26. * its ramifications are
  27. * @param bool $isConditional Flag indicating if this is a conditional style or not
  28. * Leave this value at default unless you understand exactly what
  29. * its ramifications are
  30. */
  31. public function __construct($isSupervisor = false, $isConditional = false)
  32. {
  33. // Supervisor?
  34. parent::__construct($isSupervisor);
  35. // Initialise values
  36. if (!$isConditional) {
  37. $this->locked = self::PROTECTION_INHERIT;
  38. $this->hidden = self::PROTECTION_INHERIT;
  39. }
  40. }
  41. /**
  42. * Get the shared style component for the currently active cell in currently active sheet.
  43. * Only used for style supervisor.
  44. *
  45. * @return Protection
  46. */
  47. public function getSharedComponent()
  48. {
  49. /** @var Style */
  50. $parent = $this->parent;
  51. return $parent->getSharedComponent()->getProtection();
  52. }
  53. /**
  54. * Build style array from subcomponents.
  55. *
  56. * @param array $array
  57. *
  58. * @return array
  59. */
  60. public function getStyleArray($array)
  61. {
  62. return ['protection' => $array];
  63. }
  64. /**
  65. * Apply styles from array.
  66. *
  67. * <code>
  68. * $spreadsheet->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray(
  69. * [
  70. * 'locked' => TRUE,
  71. * 'hidden' => FALSE
  72. * ]
  73. * );
  74. * </code>
  75. *
  76. * @param array $styleArray Array containing style information
  77. *
  78. * @return $this
  79. */
  80. public function applyFromArray(array $styleArray)
  81. {
  82. if ($this->isSupervisor) {
  83. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
  84. } else {
  85. if (isset($styleArray['locked'])) {
  86. $this->setLocked($styleArray['locked']);
  87. }
  88. if (isset($styleArray['hidden'])) {
  89. $this->setHidden($styleArray['hidden']);
  90. }
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Get locked.
  96. *
  97. * @return string
  98. */
  99. public function getLocked()
  100. {
  101. if ($this->isSupervisor) {
  102. return $this->getSharedComponent()->getLocked();
  103. }
  104. return $this->locked;
  105. }
  106. /**
  107. * Set locked.
  108. *
  109. * @param string $lockType see self::PROTECTION_*
  110. *
  111. * @return $this
  112. */
  113. public function setLocked($lockType)
  114. {
  115. if ($this->isSupervisor) {
  116. $styleArray = $this->getStyleArray(['locked' => $lockType]);
  117. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  118. } else {
  119. $this->locked = $lockType;
  120. }
  121. return $this;
  122. }
  123. /**
  124. * Get hidden.
  125. *
  126. * @return string
  127. */
  128. public function getHidden()
  129. {
  130. if ($this->isSupervisor) {
  131. return $this->getSharedComponent()->getHidden();
  132. }
  133. return $this->hidden;
  134. }
  135. /**
  136. * Set hidden.
  137. *
  138. * @param string $hiddenType see self::PROTECTION_*
  139. *
  140. * @return $this
  141. */
  142. public function setHidden($hiddenType)
  143. {
  144. if ($this->isSupervisor) {
  145. $styleArray = $this->getStyleArray(['hidden' => $hiddenType]);
  146. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  147. } else {
  148. $this->hidden = $hiddenType;
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Get hash code.
  154. *
  155. * @return string Hash code
  156. */
  157. public function getHashCode()
  158. {
  159. if ($this->isSupervisor) {
  160. return $this->getSharedComponent()->getHashCode();
  161. }
  162. return md5(
  163. $this->locked .
  164. $this->hidden .
  165. __CLASS__
  166. );
  167. }
  168. protected function exportArray1(): array
  169. {
  170. $exportedArray = [];
  171. $this->exportArray2($exportedArray, 'locked', $this->getLocked());
  172. $this->exportArray2($exportedArray, 'hidden', $this->getHidden());
  173. return $exportedArray;
  174. }
  175. }