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.

284 lines
6.9 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. /** Zend_Pdf_Element */
  22. // require_once 'Zend/Pdf/Element.php';
  23. /**
  24. * PDF file 'indirect object' element implementation
  25. *
  26. * @category Zend
  27. * @package Zend_Pdf
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Pdf_Element_Object extends Zend_Pdf_Element
  32. {
  33. /**
  34. * Object value
  35. *
  36. * @var Zend_Pdf_Element
  37. */
  38. protected $_value;
  39. /**
  40. * Object number within PDF file
  41. *
  42. * @var integer
  43. */
  44. protected $_objNum;
  45. /**
  46. * Generation number
  47. *
  48. * @var integer
  49. */
  50. protected $_genNum;
  51. /**
  52. * Reference to the factory.
  53. *
  54. * @var Zend_Pdf_ElementFactory
  55. */
  56. protected $_factory;
  57. /**
  58. * Object constructor
  59. *
  60. * @param Zend_Pdf_Element $val
  61. * @param integer $objNum
  62. * @param integer $genNum
  63. * @param Zend_Pdf_ElementFactory $factory
  64. * @throws Zend_Pdf_Exception
  65. */
  66. public function __construct(Zend_Pdf_Element $val, $objNum, $genNum, Zend_Pdf_ElementFactory $factory)
  67. {
  68. if ($val instanceof self) {
  69. // require_once 'Zend/Pdf/Exception.php';
  70. throw new Zend_Pdf_Exception('Object number must not be an instance of Zend_Pdf_Element_Object.');
  71. }
  72. if ( !(is_integer($objNum) && $objNum > 0) ) {
  73. // require_once 'Zend/Pdf/Exception.php';
  74. throw new Zend_Pdf_Exception('Object number must be positive integer.');
  75. }
  76. if ( !(is_integer($genNum) && $genNum >= 0) ) {
  77. // require_once 'Zend/Pdf/Exception.php';
  78. throw new Zend_Pdf_Exception('Generation number must be non-negative integer.');
  79. }
  80. $this->_value = $val;
  81. $this->_objNum = $objNum;
  82. $this->_genNum = $genNum;
  83. $this->_factory = $factory;
  84. $this->setParentObject($this);
  85. $factory->registerObject($this, $objNum . ' ' . $genNum);
  86. }
  87. /**
  88. * Check, that object is generated by specified factory
  89. *
  90. * @return Zend_Pdf_ElementFactory
  91. */
  92. public function getFactory()
  93. {
  94. return $this->_factory;
  95. }
  96. /**
  97. * Return type of the element.
  98. *
  99. * @return integer
  100. */
  101. public function getType()
  102. {
  103. return $this->_value->getType();
  104. }
  105. /**
  106. * Get object number
  107. *
  108. * @return integer
  109. */
  110. public function getObjNum()
  111. {
  112. return $this->_objNum;
  113. }
  114. /**
  115. * Get generation number
  116. *
  117. * @return integer
  118. */
  119. public function getGenNum()
  120. {
  121. return $this->_genNum;
  122. }
  123. /**
  124. * Return reference to the object
  125. *
  126. * @param Zend_Pdf_Factory $factory
  127. * @return string
  128. */
  129. public function toString($factory = null)
  130. {
  131. if ($factory === null) {
  132. $shift = 0;
  133. } else {
  134. $shift = $factory->getEnumerationShift($this->_factory);
  135. }
  136. return $this->_objNum + $shift . ' ' . $this->_genNum . ' R';
  137. }
  138. /**
  139. * Dump object to a string to save within PDF file.
  140. *
  141. * $factory parameter defines operation context.
  142. *
  143. * @param Zend_Pdf_ElementFactory $factory
  144. * @return string
  145. */
  146. public function dump(Zend_Pdf_ElementFactory $factory)
  147. {
  148. $shift = $factory->getEnumerationShift($this->_factory);
  149. return $this->_objNum + $shift . " " . $this->_genNum . " obj \n"
  150. . $this->_value->toString($factory) . "\n"
  151. . "endobj\n";
  152. }
  153. /**
  154. * Get handler
  155. *
  156. * @param string $property
  157. * @return mixed
  158. */
  159. public function __get($property)
  160. {
  161. return $this->_value->$property;
  162. }
  163. /**
  164. * Set handler
  165. *
  166. * @param string $property
  167. * @param mixed $value
  168. */
  169. public function __set($property, $value)
  170. {
  171. $this->_value->$property = $value;
  172. }
  173. /**
  174. * Call handler
  175. *
  176. * @param string $method
  177. * @param array $args
  178. * @return mixed
  179. */
  180. public function __call($method, $args)
  181. {
  182. return call_user_func_array(array($this->_value, $method), $args);
  183. }
  184. /**
  185. * Detach PDF object from the factory (if applicable), clone it and attach to new factory.
  186. *
  187. * @param Zend_Pdf_ElementFactory $factory The factory to attach
  188. * @param array &$processed List of already processed indirect objects, used to avoid objects duplication
  189. * @param integer $mode Cloning mode (defines filter for objects cloning)
  190. * @returns Zend_Pdf_Element
  191. */
  192. public function makeClone(Zend_Pdf_ElementFactory $factory, array &$processed, $mode)
  193. {
  194. $id = spl_object_hash($this);
  195. if (isset($processed[$id])) {
  196. // Do nothing if object is already processed
  197. // return it
  198. return $processed[$id];
  199. }
  200. // Create obect with null value and register it in $processed container
  201. $processed[$id] = $clonedObject = $factory->newObject(new Zend_Pdf_Element_Null());
  202. // Pecursively process actual data
  203. $clonedObject->_value = $this->_value->makeClone($factory, $processed, $mode);
  204. if ($clonedObject->_value instanceof Zend_Pdf_Element_Null) {
  205. // Do not store null objects within $processed container since it may be filtered
  206. // by $mode parameter but used in some future pass
  207. unset($processed[$id]);
  208. // Return direct null object
  209. return $clonedObject->_value;
  210. }
  211. return $clonedObject;
  212. }
  213. /**
  214. * Mark object as modified, to include it into new PDF file segment
  215. */
  216. public function touch()
  217. {
  218. $this->_factory->markAsModified($this);
  219. }
  220. /**
  221. * Return object, which can be used to identify object and its references identity
  222. *
  223. * @return Zend_Pdf_Element_Object
  224. */
  225. public function getObject()
  226. {
  227. return $this;
  228. }
  229. /**
  230. * Clean up resources, used by object
  231. */
  232. public function cleanUp()
  233. {
  234. $this->_value = null;
  235. }
  236. /**
  237. * Convert PDF element to PHP type.
  238. *
  239. * @return mixed
  240. */
  241. public function toPhp()
  242. {
  243. return $this->_value->toPhp();
  244. }
  245. }