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 WorkbookProtection
|
|
|
10 |
{
|
|
|
11 |
public function __construct(
|
|
|
12 |
public ?string $password = null,
|
|
|
13 |
public bool $lockStructure = false,
|
|
|
14 |
public bool $lockWindows = false,
|
|
|
15 |
public bool $lockRevisions = false,
|
|
|
16 |
) {}
|
|
|
17 |
|
|
|
18 |
public function getXml(): string
|
|
|
19 |
{
|
|
|
20 |
return '<workbookProtection'.$this->getSheetViewAttributes().'/>';
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
private function getSheetViewAttributes(): string
|
|
|
24 |
{
|
|
|
25 |
return $this->generateAttributes([
|
|
|
26 |
'workbookPassword' => null !== $this->password ? PasswordHashHelper::make($this->password) : '',
|
|
|
27 |
'lockStructure' => $this->lockStructure,
|
|
|
28 |
'lockWindows' => $this->lockWindows,
|
|
|
29 |
'lockRevisions' => $this->lockRevisions,
|
|
|
30 |
]);
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* @param array<string, bool|string> $data with key containing the attribute name and value containing the attribute value
|
|
|
35 |
*/
|
|
|
36 |
private function generateAttributes(array $data): string
|
|
|
37 |
{
|
|
|
38 |
// Create attribute for each key
|
|
|
39 |
$attributes = array_map(static function (string $key, bool|string $value): string {
|
|
|
40 |
if (\is_bool($value)) {
|
|
|
41 |
$value = $value ? 'true' : 'false';
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
return $key.'="'.$value.'"';
|
|
|
45 |
}, array_keys($data), $data);
|
|
|
46 |
|
|
|
47 |
// Append all attributes
|
|
|
48 |
return ' '.implode(' ', $attributes);
|
|
|
49 |
}
|
|
|
50 |
}
|