Protection.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * <code>
  66. * $spreadsheet->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray(
  67. * array(
  68. * 'locked' => TRUE,
  69. * 'hidden' => FALSE
  70. * )
  71. * );
  72. * </code>.
  73. *
  74. * @param array $pStyles Array containing style information
  75. *
  76. * @throws PhpSpreadsheetException
  77. *
  78. * @return Protection
  79. */
  80. public function applyFromArray(array $pStyles)
  81. {
  82. if ($this->isSupervisor) {
  83. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  84. } else {
  85. if (isset($pStyles['locked'])) {
  86. $this->setLocked($pStyles['locked']);
  87. }
  88. if (isset($pStyles['hidden'])) {
  89. $this->setHidden($pStyles['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 $pValue see self::PROTECTION_*
  110. *
  111. * @return Protection
  112. */
  113. public function setLocked($pValue)
  114. {
  115. if ($this->isSupervisor) {
  116. $styleArray = $this->getStyleArray(['locked' => $pValue]);
  117. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  118. } else {
  119. $this->locked = $pValue;
  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 $pValue see self::PROTECTION_*
  139. *
  140. * @return Protection
  141. */
  142. public function setHidden($pValue)
  143. {
  144. if ($this->isSupervisor) {
  145. $styleArray = $this->getStyleArray(['hidden' => $pValue]);
  146. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  147. } else {
  148. $this->hidden = $pValue;
  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. }