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\Exception\UnexpectedValueException; |
14
|
|
|
use Bacon\Pdf\Object\LiteralStringObject; |
15
|
|
|
use Bacon\Pdf\Object\NameObject; |
16
|
|
|
use Bacon\Pdf\Type\DateType; |
17
|
|
|
use DateTimeImmutable; |
18
|
|
|
use OutOfBoundsException; |
19
|
|
|
|
20
|
|
|
final class DocumentInformation |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $data = ['Producer' => 'BaconPdf']; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Sets an entry in the information dictionary. |
29
|
|
|
* |
30
|
|
|
* The CreationData and ModDate values are restricted for internal use, so trying to set them will trigger an |
31
|
|
|
* exception. Setting the "Trapped" value is allowed, but it must be one of the values "True", "False" or "Unknown". |
32
|
|
|
* You can set any key in here, but the following are the standard keys recognized by the PDF standard. Keep in mind |
33
|
|
|
* that the keys are case-sensitive: |
34
|
|
|
* |
35
|
|
|
* Title, Author, Subject, Keywords, Creator, Producer and Trapped. |
36
|
|
|
* |
37
|
|
|
* @param string $key |
38
|
|
|
* @param string $value |
39
|
|
|
* @throws DomainException |
40
|
|
|
*/ |
41
|
|
|
public function set($key, $value) |
42
|
|
|
{ |
43
|
|
|
if ('CreationDate' === $key || 'ModDate' === $key) { |
44
|
|
|
throw new DomainException('CreationDate and ModDate must not be set manually'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ('Trapped' === $key) { |
48
|
|
|
if (!in_array($value, ['True', 'False', 'Unknown'])) { |
49
|
|
|
throw new DomainException('Value for "Trapped" must be either "True", "False" or "Unknown"'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->data['Trapped'] = $value; |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->data[$key] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Removes an entry from the information dictionary. |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
*/ |
64
|
|
|
public function remove($key) |
65
|
|
|
{ |
66
|
|
|
unset($this->data[$key]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Checks whether an entry exists in the information dictionary. |
71
|
|
|
* |
72
|
|
|
* @param string $key |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function has($key) |
76
|
|
|
{ |
77
|
|
|
return array_key_exists($key, $this->data); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Retrieves the value for a specific entry in the information dictionary. |
82
|
|
|
* |
83
|
|
|
* You may retrieve any entry from the information dictionary through this method, except for "CreationData" and |
84
|
|
|
* "ModDate". Those two entries have their own respective methods to be retrieved. |
85
|
|
|
* |
86
|
|
|
* @param string $key |
87
|
|
|
* @return string |
88
|
|
|
* @throws DomainException |
89
|
|
|
* @throws OutOfBoundsException |
90
|
|
|
*/ |
91
|
|
|
public function get($key) |
92
|
|
|
{ |
93
|
|
|
if ('CreationDate' === $key || 'ModDate' === $key) { |
94
|
|
|
throw new DomainException('CreationDate and ModDate must be retrieved through their respective methods'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!array_key_exists($key, $this->data)) { |
98
|
|
|
throw new OutOfBoundsException(sprintf('Entry for key "%s" not found', $key)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->data[$key]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return DateTimeImmutable |
106
|
|
|
*/ |
107
|
|
|
public function getCreationDate() |
108
|
|
|
{ |
109
|
|
|
return $this->retrieveDate('CreationDate'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return DateTimeImmutable |
114
|
|
|
*/ |
115
|
|
|
public function getModificationDate() |
116
|
|
|
{ |
117
|
|
|
return $this->retrieveDate('ModDate'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $key |
122
|
|
|
* @return DateTimeImmutable |
123
|
|
|
* @throws OutOfBoundsException |
124
|
|
|
*/ |
125
|
|
|
private function retrieveDate($key) |
126
|
|
|
{ |
127
|
|
|
if (!array_key_exists($key, $this->data)) { |
128
|
|
|
throw new OutOfBoundsException(sprintf('Entry for key "%s" not found', $key)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->data[$key]; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|