You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
6.2 KiB

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