Passed
Pull Request — master (#27)
by Stephen
02:38
created

Metadata::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sfneal\ViewExport\Pdf\Utils;
4
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
93
    {
94
        return in_array($key, $this->validMetadataKeys);
95
    }
96
}
97