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