1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace OpenSpout\Writer\XLSX\Options;
|
|
|
6 |
|
|
|
7 |
use OpenSpout\Writer\XLSX\Helper\PasswordHashHelper;
|
|
|
8 |
|
|
|
9 |
final readonly class SheetProtection
|
|
|
10 |
{
|
|
|
11 |
public function __construct(
|
|
|
12 |
public ?string $password = null,
|
|
|
13 |
public bool $lockSheet = false,
|
|
|
14 |
public bool $lockColumnInsert = false,
|
|
|
15 |
public bool $lockColumnDelete = false,
|
|
|
16 |
public bool $lockColumnFormatting = false,
|
|
|
17 |
public bool $lockRowInsert = false,
|
|
|
18 |
public bool $lockRowDelete = false,
|
|
|
19 |
public bool $lockRowFormatting = false,
|
|
|
20 |
public bool $lockAutoFilter = false,
|
|
|
21 |
public bool $lockSort = false,
|
|
|
22 |
public bool $lockCellFormatting = false,
|
|
|
23 |
public bool $lockLockedCellSelection = false,
|
|
|
24 |
public bool $lockUnlockedCellsSelection = false,
|
|
|
25 |
public bool $lockObjects = false,
|
|
|
26 |
public bool $lockHyperlinkInsert = false,
|
|
|
27 |
public bool $lockPivotTables = false,
|
|
|
28 |
public bool $lockScenarios = false,
|
|
|
29 |
) {}
|
|
|
30 |
|
|
|
31 |
public function getXml(): string
|
|
|
32 |
{
|
|
|
33 |
return '<sheetProtection'.$this->getSheetViewAttributes().'/>';
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
private function getSheetViewAttributes(): string
|
|
|
37 |
{
|
|
|
38 |
return $this->generateAttributes([
|
|
|
39 |
'password' => null !== $this->password ? PasswordHashHelper::make($this->password) : '',
|
|
|
40 |
'sheet' => $this->lockSheet,
|
|
|
41 |
'objects' => $this->lockObjects,
|
|
|
42 |
'scenarios' => $this->lockScenarios,
|
|
|
43 |
'formatCells' => $this->lockCellFormatting,
|
|
|
44 |
'formatColumns' => $this->lockColumnFormatting,
|
|
|
45 |
'formatRows' => $this->lockRowFormatting,
|
|
|
46 |
'insertColumns' => $this->lockColumnInsert,
|
|
|
47 |
'insertRows' => $this->lockRowInsert,
|
|
|
48 |
'deleteColumns' => $this->lockColumnDelete,
|
|
|
49 |
'deleteRows' => $this->lockRowDelete,
|
|
|
50 |
'selectLockedCells' => $this->lockLockedCellSelection,
|
|
|
51 |
'selectUnlockedCells' => $this->lockUnlockedCellsSelection,
|
|
|
52 |
'autoFilter' => $this->lockAutoFilter,
|
|
|
53 |
'sort' => $this->lockSort,
|
|
|
54 |
'hyperlink' => $this->lockHyperlinkInsert,
|
|
|
55 |
'pivotTables' => $this->lockPivotTables,
|
|
|
56 |
]);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* @param array<string, bool|string> $data with key containing the attribute name and value containing the attribute value
|
|
|
61 |
*/
|
|
|
62 |
private function generateAttributes(array $data): string
|
|
|
63 |
{
|
|
|
64 |
// Create attribute for each key
|
|
|
65 |
$attributes = array_map(static function (string $key, bool|string $value): string {
|
|
|
66 |
if (\is_bool($value)) {
|
|
|
67 |
$value = $value ? 'true' : 'false';
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
return $key.'="'.$value.'"';
|
|
|
71 |
}, array_keys($data), $data);
|
|
|
72 |
|
|
|
73 |
// Append all attributes
|
|
|
74 |
return ' '.implode(' ', $attributes);
|
|
|
75 |
}
|
|
|
76 |
}
|