|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\ViewExport\Pdf\Utils; |
|
4
|
|
|
|
|
5
|
|
|
class Metadata |
|
6
|
|
|
{ |
|
7
|
|
|
// todo: add tests |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Array of valid metadata keys. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string[] |
|
13
|
|
|
*/ |
|
14
|
|
|
private $validMetadataKeys = [ |
|
15
|
|
|
'Title', |
|
16
|
|
|
'Author', |
|
17
|
|
|
'Subject', |
|
18
|
|
|
'Keywords', |
|
19
|
|
|
'Creator', |
|
20
|
|
|
'Producer', |
|
21
|
|
|
'CreationDate', |
|
22
|
|
|
'ModDate', |
|
23
|
|
|
'Trapped', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Metadata info to add to the PDF. |
|
28
|
|
|
* |
|
29
|
|
|
* - keys are validated from $validMetadata property |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $metadata = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Metadata constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array|null $metadata |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(array $metadata = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if (! empty($metadata)) { |
|
43
|
|
|
$this->set($metadata ?? $this->getConfigMetadata()); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Retrieve an array of metadata. |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function get(): array |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->metadata; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Set the entire $metadata array. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $metadata |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function set(array $metadata): self |
|
64
|
|
|
{ |
|
65
|
|
|
$this->metadata = array_filter($metadata, function ($key) { |
|
66
|
|
|
return $this->validateMetadata($key); |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add a $key, $value pair to the metadata array. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $key |
|
76
|
|
|
* @param $value |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function add(string $key, $value): self |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->validateMetadata($key)) { |
|
82
|
|
|
$this->metadata[$key] = $value; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Confirm a potential metadata key is valid. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $key |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
private function validateMetadata(string $key): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return in_array($key, $this->validMetadataKeys); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Retrieve an array of metadata set in the config with null & empty values removed. |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
private function getConfigMetadata(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return array_filter(config('view-export.pdf.metadata'), function ($value) { |
|
107
|
|
|
return ! is_null($value) && ! empty($value); |
|
108
|
|
|
}); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|