Protection.php 4.5 KB

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