|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Pdf |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Dictionary.php 22797 2010-08-06 15:02:12Z alexander $ |
|
20 */ |
|
21 |
|
22 |
|
23 /** Internally used classes */ |
|
24 require_once 'Zend/Pdf/Element/Name.php'; |
|
25 |
|
26 |
|
27 /** Zend_Pdf_Element */ |
|
28 require_once 'Zend/Pdf/Element.php'; |
|
29 |
|
30 /** |
|
31 * PDF file 'dictionary' element implementation |
|
32 * |
|
33 * @category Zend |
|
34 * @package Zend_Pdf |
|
35 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
36 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
37 */ |
|
38 class Zend_Pdf_Element_Dictionary extends Zend_Pdf_Element |
|
39 { |
|
40 /** |
|
41 * Dictionary elements |
|
42 * Array of Zend_Pdf_Element objects ('name' => Zend_Pdf_Element) |
|
43 * |
|
44 * @var array |
|
45 */ |
|
46 private $_items = array(); |
|
47 |
|
48 |
|
49 /** |
|
50 * Object constructor |
|
51 * |
|
52 * @param array $val - array of Zend_Pdf_Element objects |
|
53 * @throws Zend_Pdf_Exception |
|
54 */ |
|
55 public function __construct($val = null) |
|
56 { |
|
57 if ($val === null) { |
|
58 return; |
|
59 } else if (!is_array($val)) { |
|
60 require_once 'Zend/Pdf/Exception.php'; |
|
61 throw new Zend_Pdf_Exception('Argument must be an array'); |
|
62 } |
|
63 |
|
64 foreach ($val as $name => $element) { |
|
65 if (!$element instanceof Zend_Pdf_Element) { |
|
66 require_once 'Zend/Pdf/Exception.php'; |
|
67 throw new Zend_Pdf_Exception('Array elements must be Zend_Pdf_Element objects'); |
|
68 } |
|
69 if (!is_string($name)) { |
|
70 require_once 'Zend/Pdf/Exception.php'; |
|
71 throw new Zend_Pdf_Exception('Array keys must be strings'); |
|
72 } |
|
73 $this->_items[$name] = $element; |
|
74 } |
|
75 } |
|
76 |
|
77 |
|
78 /** |
|
79 * Add element to an array |
|
80 * |
|
81 * @name Zend_Pdf_Element_Name $name |
|
82 * @param Zend_Pdf_Element $val - Zend_Pdf_Element object |
|
83 * @throws Zend_Pdf_Exception |
|
84 */ |
|
85 public function add(Zend_Pdf_Element_Name $name, Zend_Pdf_Element $val) |
|
86 { |
|
87 $this->_items[$name->value] = $val; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Return dictionary keys |
|
92 * |
|
93 * @return array |
|
94 */ |
|
95 public function getKeys() |
|
96 { |
|
97 return array_keys($this->_items); |
|
98 } |
|
99 |
|
100 |
|
101 /** |
|
102 * Get handler |
|
103 * |
|
104 * @param string $property |
|
105 * @return Zend_Pdf_Element | null |
|
106 */ |
|
107 public function __get($item) |
|
108 { |
|
109 $element = isset($this->_items[$item]) ? $this->_items[$item] |
|
110 : null; |
|
111 |
|
112 return $element; |
|
113 } |
|
114 |
|
115 /** |
|
116 * Set handler |
|
117 * |
|
118 * @param string $property |
|
119 * @param mixed $value |
|
120 */ |
|
121 public function __set($item, $value) |
|
122 { |
|
123 if ($value === null) { |
|
124 unset($this->_items[$item]); |
|
125 } else { |
|
126 $this->_items[$item] = $value; |
|
127 } |
|
128 } |
|
129 |
|
130 /** |
|
131 * Return type of the element. |
|
132 * |
|
133 * @return integer |
|
134 */ |
|
135 public function getType() |
|
136 { |
|
137 return Zend_Pdf_Element::TYPE_DICTIONARY; |
|
138 } |
|
139 |
|
140 |
|
141 /** |
|
142 * Return object as string |
|
143 * |
|
144 * @param Zend_Pdf_Factory $factory |
|
145 * @return string |
|
146 */ |
|
147 public function toString($factory = null) |
|
148 { |
|
149 $outStr = '<<'; |
|
150 $lastNL = 0; |
|
151 |
|
152 foreach ($this->_items as $name => $element) { |
|
153 if (!is_object($element)) { |
|
154 require_once 'Zend/Pdf/Exception.php'; |
|
155 throw new Zend_Pdf_Exception('Wrong data'); |
|
156 } |
|
157 |
|
158 if (strlen($outStr) - $lastNL > 128) { |
|
159 $outStr .= "\n"; |
|
160 $lastNL = strlen($outStr); |
|
161 } |
|
162 |
|
163 $nameObj = new Zend_Pdf_Element_Name($name); |
|
164 $outStr .= $nameObj->toString($factory) . ' ' . $element->toString($factory) . ' '; |
|
165 } |
|
166 $outStr .= '>>'; |
|
167 |
|
168 return $outStr; |
|
169 } |
|
170 |
|
171 /** |
|
172 * Detach PDF object from the factory (if applicable), clone it and attach to new factory. |
|
173 * |
|
174 * @param Zend_Pdf_ElementFactory $factory The factory to attach |
|
175 * @param array &$processed List of already processed indirect objects, used to avoid objects duplication |
|
176 * @param integer $mode Cloning mode (defines filter for objects cloning) |
|
177 * @returns Zend_Pdf_Element |
|
178 * @throws Zend_Pdf_Exception |
|
179 */ |
|
180 public function makeClone(Zend_Pdf_ElementFactory $factory, array &$processed, $mode) |
|
181 { |
|
182 if (isset($this->_items['Type'])) { |
|
183 if ($this->_items['Type']->value == 'Pages') { |
|
184 // It's a page tree node |
|
185 // skip it and its children |
|
186 return new Zend_Pdf_Element_Null(); |
|
187 } |
|
188 |
|
189 if ($this->_items['Type']->value == 'Page' && |
|
190 $mode == Zend_Pdf_Element::CLONE_MODE_SKIP_PAGES |
|
191 ) { |
|
192 // It's a page node, skip it |
|
193 return new Zend_Pdf_Element_Null(); |
|
194 } |
|
195 } |
|
196 |
|
197 $newDictionary = new self(); |
|
198 foreach ($this->_items as $key => $value) { |
|
199 $newDictionary->_items[$key] = $value->makeClone($factory, $processed, $mode); |
|
200 } |
|
201 |
|
202 return $newDictionary; |
|
203 } |
|
204 |
|
205 /** |
|
206 * Set top level parent indirect object. |
|
207 * |
|
208 * @param Zend_Pdf_Element_Object $parent |
|
209 */ |
|
210 public function setParentObject(Zend_Pdf_Element_Object $parent) |
|
211 { |
|
212 parent::setParentObject($parent); |
|
213 |
|
214 foreach ($this->_items as $item) { |
|
215 $item->setParentObject($parent); |
|
216 } |
|
217 } |
|
218 |
|
219 /** |
|
220 * Convert PDF element to PHP type. |
|
221 * |
|
222 * Dictionary is returned as an associative array |
|
223 * |
|
224 * @return mixed |
|
225 */ |
|
226 public function toPhp() |
|
227 { |
|
228 $phpArray = array(); |
|
229 |
|
230 foreach ($this->_items as $itemName => $item) { |
|
231 $phpArray[$itemName] = $item->toPhp(); |
|
232 } |
|
233 |
|
234 return $phpArray; |
|
235 } |
|
236 } |