3 namespace PhpOffice\PhpSpreadsheet\Style
;
5 use PhpOffice\PhpSpreadsheet\Exception
as PhpSpreadsheetException
;
7 class Protection
extends Supervisor
9 /** Protection styles */
10 const PROTECTION_INHERIT
= 'inherit';
11 const PROTECTION_PROTECTED
= 'protected';
12 const PROTECTION_UNPROTECTED
= 'unprotected';
29 * Create a new Protection.
31 * @param bool $isSupervisor Flag indicating if this is a supervisor or not
32 * Leave this value at default unless you understand exactly what
33 * its ramifications are
34 * @param bool $isConditional Flag indicating if this is a conditional style or not
35 * Leave this value at default unless you understand exactly what
36 * its ramifications are
38 public function __construct($isSupervisor = false, $isConditional = false)
41 parent
::__construct($isSupervisor);
44 if (!$isConditional) {
45 $this->locked
= self
::PROTECTION_INHERIT
;
46 $this->hidden
= self
::PROTECTION_INHERIT
;
51 * Get the shared style component for the currently active cell in currently active sheet.
52 * Only used for style supervisor.
56 public function getSharedComponent()
58 return $this->parent
->getSharedComponent()->getProtection();
62 * Build style array from subcomponents.
68 public function getStyleArray($array)
70 return ['protection' => $array];
74 * Apply styles from array.
77 * $spreadsheet->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray(
85 * @param array $pStyles Array containing style information
87 * @throws PhpSpreadsheetException
91 public function applyFromArray(array $pStyles)
93 if ($this->isSupervisor
) {
94 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
96 if (isset($pStyles['locked'])) {
97 $this->setLocked($pStyles['locked']);
99 if (isset($pStyles['hidden'])) {
100 $this->setHidden($pStyles['hidden']);
112 public function getLocked()
114 if ($this->isSupervisor
) {
115 return $this->getSharedComponent()->getLocked();
118 return $this->locked
;
124 * @param string $pValue see self::PROTECTION_*
128 public function setLocked($pValue)
130 if ($this->isSupervisor
) {
131 $styleArray = $this->getStyleArray(['locked' => $pValue]);
132 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
134 $this->locked
= $pValue;
145 public function getHidden()
147 if ($this->isSupervisor
) {
148 return $this->getSharedComponent()->getHidden();
151 return $this->hidden
;
157 * @param string $pValue see self::PROTECTION_*
161 public function setHidden($pValue)
163 if ($this->isSupervisor
) {
164 $styleArray = $this->getStyleArray(['hidden' => $pValue]);
165 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
167 $this->hidden
= $pValue;
176 * @return string Hash code
178 public function getHashCode()
180 if ($this->isSupervisor
) {
181 return $this->getSharedComponent()->getHashCode();