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