DocumentInformation::retrieveDate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * BaconPdf
4
 *
5
 * @link      http://github.com/Bacon/BaconPdf For the canonical source repository
6
 * @copyright 2015 Ben Scholzen (DASPRiD)
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Bacon\Pdf;
11
12
use Bacon\Pdf\Exception\DomainException;
13
use Bacon\Pdf\Utils\StringUtils;
14
use Bacon\Pdf\Writer\ObjectWriter;
15
use DateTimeImmutable;
16
use OutOfBoundsException;
17
18
final class DocumentInformation
19
{
20
    /**
21
     * @var array
22
     */
23
    private $data = ['Producer' => 'BaconPdf'];
24
25
    /**
26
     * Sets an entry in the information dictionary.
27
     *
28
     * The CreationData and ModDate values are restricted for internal use, so trying to set them will trigger an
29
     * exception. Setting the "Trapped" value is allowed, but it must be one of the values "True", "False" or "Unknown".
30
     * You can set any key in here, but the following are the standard keys recognized by the PDF standard. Keep in mind
31
     * that the keys are case-sensitive:
32
     *
33
     * Title, Author, Subject, Keywords, Creator, Producer and Trapped.
34
     *
35
     * @param  string $key
36
     * @param  string $value
37
     * @throws DomainException
38
     */
39
    public function set($key, $value)
40
    {
41
        if ('CreationDate' === $key || 'ModDate' === $key) {
42
            throw new DomainException('CreationDate and ModDate must not be set manually');
43
        }
44
45
        if ('Trapped' === $key) {
46
            if (!in_array($value, ['True', 'False', 'Unknown'])) {
47
                throw new DomainException('Value for "Trapped" must be either "True", "False" or "Unknown"');
48
            }
49
50
            $this->data['Trapped'] = $value;
51
            return;
52
        }
53
54
        $this->data[$key] = $value;
55
    }
56
57
    /**
58
     * Removes an entry from the information dictionary.
59
     *
60
     * @param string $key
61
     */
62
    public function remove($key)
63
    {
64
        unset($this->data[$key]);
65
    }
66
67
    /**
68
     * Checks whether an entry exists in the information dictionary.
69
     *
70
     * @param  string $key
71
     * @return bool
72
     */
73
    public function has($key)
74
    {
75
        return array_key_exists($key, $this->data);
76
    }
77
78
    /**
79
     * Retrieves the value for a specific entry in the information dictionary.
80
     *
81
     * You may retrieve any entry from the information dictionary through this method, except for "CreationData" and
82
     * "ModDate". Those two entries have their own respective methods to be retrieved.
83
     *
84
     * @param  string $key
85
     * @return string
86
     * @throws DomainException
87
     * @throws OutOfBoundsException
88
     */
89
    public function get($key)
90
    {
91
        if ('CreationDate' === $key || 'ModDate' === $key) {
92
            throw new DomainException('CreationDate and ModDate must be retrieved through their respective methods');
93
        }
94
95
        if (!array_key_exists($key, $this->data)) {
96
            throw new OutOfBoundsException(sprintf('Entry for key "%s" not found', $key));
97
        }
98
99
        return $this->data[$key];
100
    }
101
102
    /**
103
     * @return DateTimeImmutable
104
     */
105
    public function getCreationDate()
106
    {
107
        return $this->retrieveDate('CreationDate');
108
    }
109
110
    /**
111
     * @return DateTimeImmutable
112
     */
113
    public function getModificationDate()
114
    {
115
        return $this->retrieveDate('ModDate');
116
    }
117
118
    /**
119
     * Writes the info dictionary.
120
     *
121
     * @param ObjectWriter $objectWriter
122
     * @internal
123
     */
124
    public function writeInfoDictionary(ObjectWriter $objectWriter)
125
    {
126
        $objectWriter->startDictionary();
127
128
        foreach ($this->data as $key => $value) {
129
            $objectWriter->writeName($key);
130
131
            switch ($key) {
132
                case 'CreationDate':
133
                case 'ModDate':
134
                    $objectWriter->writeLiteralString(StringUtils::formatDateTime($value));
135
                    break;
136
137
                case 'Trapped':
138
                    $objectWriter->writeName($value);
139
                    break;
140
141
                default:
142
                    $objectWriter->writeLiteralString(StringUtils::encodeString($value));
143
            }
144
        }
145
146
        $objectWriter->endDictionary();
147
    }
148
149
    /**
150
     * @param  string $key
151
     * @return DateTimeImmutable
152
     * @throws OutOfBoundsException
153
     */
154
    private function retrieveDate($key)
155
    {
156
        if (!array_key_exists($key, $this->data)) {
157
            throw new OutOfBoundsException(sprintf('Entry for key "%s" not found', $key));
158
        }
159
160
        return $this->data[$key];
161
    }
162
}
163