1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BaconPdf |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Bacon/BaconPdf For the canonical source repository |
6
|
|
|
* @copyright 2015 Ben 'DASPRiD' Scholzen |
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\DictionaryObject; |
15
|
|
|
use Bacon\Pdf\Object\LiteralStringObject; |
16
|
|
|
use Bacon\Pdf\Object\NameObject; |
17
|
|
|
use Bacon\Pdf\Structure\TextStringType; |
18
|
|
|
use Bacon\Pdf\Type\DateType; |
19
|
|
|
use DateTimeImmutable; |
20
|
|
|
|
21
|
|
|
final class Info |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var DictionaryObject |
25
|
|
|
*/ |
26
|
|
|
private $dictionary; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param DictionaryObject $dictionary |
30
|
|
|
*/ |
31
|
|
|
public function __construct(DictionaryObject $dictionary) |
32
|
|
|
{ |
33
|
|
|
$this->dictionary = $dictionary; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Sets an entry in the information dictionary. |
38
|
|
|
* |
39
|
|
|
* The CreationData and ModDate values are restricted for internal use, so trying to set them will trigger an |
40
|
|
|
* exception. Setting the "Trapped" value is allowed, but it must be one of the values "True", "False" or "Unknown". |
41
|
|
|
* You can set any key in here, but the following are the standard keys recognized by the PDF standard. Keep in mind |
42
|
|
|
* that the keys are case-sensitive: |
43
|
|
|
* |
44
|
|
|
* Title, Author, Subject, Keywords, Creator, Producer and Trapped. |
45
|
|
|
* |
46
|
|
|
* @param string $key |
47
|
|
|
* @param string $value |
48
|
|
|
* @throws DomainException |
49
|
|
|
*/ |
50
|
|
|
public function set($key, $value) |
51
|
|
|
{ |
52
|
|
|
if ('CreationDate' === $key || 'ModDate' === $key) { |
53
|
|
|
throw new DomainException('CreationDate and ModDate must not be set manually'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ('Trapped' === $key) { |
57
|
|
|
if (!in_array($value, ['True', 'False', 'Unknown'])) { |
58
|
|
|
throw new DomainException('Value for "Trapped" must be either "True", "False" or "Unknown"'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->dictionary->set($key, new NameObject($value)); |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->dictionary->set($key, new TextStringType($value)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Removes an entry from the information dictionary. |
70
|
|
|
* |
71
|
|
|
* @param string $key |
72
|
|
|
*/ |
73
|
|
|
public function remove($key) |
74
|
|
|
{ |
75
|
|
|
$this->dictionary->remove($key); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Checks whether an entry exists in the information dictionary. |
80
|
|
|
* |
81
|
|
|
* @param string $key |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function has($key) |
85
|
|
|
{ |
86
|
|
|
return $this->dictionary->has($key); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieves the value for a specific entry in the information dictionary. |
91
|
|
|
* |
92
|
|
|
* You may retrieve any entry from the information dictionary through this method, except for "CreationData" and |
93
|
|
|
* "ModDate". Those two entries have their own respective methods to be retrieved. |
94
|
|
|
* |
95
|
|
|
* @param string $key |
96
|
|
|
* @return string |
97
|
|
|
* @throws DomainException |
98
|
|
|
* @throws UnexpectedValueException |
99
|
|
|
*/ |
100
|
|
|
public function get($key) |
101
|
|
|
{ |
102
|
|
|
if ('CreationDate' === $key || 'ModDate' === $key) { |
103
|
|
|
throw new DomainException('CreationDate and ModDate must be retrieved through their respective methods'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$object = $this->dictionary->get($key); |
107
|
|
|
|
108
|
|
|
if ('Trapped' === $key) { |
109
|
|
|
if (!$object instanceof NameObject) { |
110
|
|
|
throw new UnexpectedValueException(sprintf( |
111
|
|
|
'Expected an object of type %s, but got %s', |
112
|
|
|
NameObject::class, |
113
|
|
|
get_class($object) |
114
|
|
|
)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $object->getName(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (!$object instanceof LiteralStringObject) { |
121
|
|
|
throw new UnexpectedValueException(sprintf( |
122
|
|
|
'Expected an object of type %s, but got %s', |
123
|
|
|
LiteralStringObject::class, |
124
|
|
|
get_class($object) |
125
|
|
|
)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $object->getValue(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return DateTimeImmutable |
133
|
|
|
*/ |
134
|
|
|
public function getCreationDate() |
135
|
|
|
{ |
136
|
|
|
return $this->retrieveDate('CreationDate'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return DateTimeImmutable |
141
|
|
|
*/ |
142
|
|
|
public function getModificationDate() |
143
|
|
|
{ |
144
|
|
|
return $this->retrieveDate('ModDate'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $key |
149
|
|
|
* @return DateTimeImmutable |
150
|
|
|
* @throws UnexpectedValueException |
151
|
|
|
*/ |
152
|
|
|
private function retrieveDate($key) |
153
|
|
|
{ |
154
|
|
|
$object = $this->dictionary->get($key); |
155
|
|
|
|
156
|
|
|
if (!$object instanceof DateType) { |
157
|
|
|
throw new UnexpectedValueException(sprintf( |
158
|
|
|
'Expected an object of type %s, but got %s', |
159
|
|
|
DateType::class, |
160
|
|
|
get_class($object) |
161
|
|
|
)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $object->getDateTime(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.