Reference.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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-2009 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. /** Zend_Pdf_Element_Reference_Context */
  24. require_once 'Zend/Pdf/Element/Reference/Context.php';
  25. /** Zend_Pdf_Element_Reference_Table */
  26. require_once 'Zend/Pdf/Element/Reference/Table.php';
  27. /** Zend_Pdf_ElementFactory */
  28. require_once 'Zend/Pdf/ElementFactory.php';
  29. /**
  30. * PDF file 'reference' element implementation
  31. *
  32. * @category Zend
  33. * @package Zend_Pdf
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Pdf_Element_Reference extends Zend_Pdf_Element
  38. {
  39. /**
  40. * Object value
  41. * The reference to the object
  42. *
  43. * @var mixed
  44. */
  45. private $_ref;
  46. /**
  47. * Object number within PDF file
  48. *
  49. * @var integer
  50. */
  51. private $_objNum;
  52. /**
  53. * Generation number
  54. *
  55. * @var integer
  56. */
  57. private $_genNum;
  58. /**
  59. * Reference context
  60. *
  61. * @var Zend_Pdf_Element_Reference_Context
  62. */
  63. private $_context;
  64. /**
  65. * Reference to the factory.
  66. *
  67. * It's the same as referenced object factory, but we save it here to avoid
  68. * unnecessary dereferencing, whech can produce cascade dereferencing and parsing.
  69. * The same for duplication of getFactory() function. It can be processed by __call()
  70. * method, but we catch it here.
  71. *
  72. * @var Zend_Pdf_ElementFactory
  73. */
  74. private $_factory;
  75. /**
  76. * Object constructor:
  77. *
  78. * @param integer $objNum
  79. * @param integer $genNum
  80. * @param Zend_Pdf_Element_Reference_Context $context
  81. * @param Zend_Pdf_ElementFactory $factory
  82. * @throws Zend_Pdf_Exception
  83. */
  84. public function __construct($objNum, $genNum = 0, Zend_Pdf_Element_Reference_Context $context, Zend_Pdf_ElementFactory $factory)
  85. {
  86. if ( !(is_integer($objNum) && $objNum > 0) ) {
  87. throw new Zend_Pdf_Exception('Object number must be positive integer');
  88. }
  89. if ( !(is_integer($genNum) && $genNum >= 0) ) {
  90. throw new Zend_Pdf_Exception('Generation number must be non-negative integer');
  91. }
  92. $this->_objNum = $objNum;
  93. $this->_genNum = $genNum;
  94. $this->_ref = null;
  95. $this->_context = $context;
  96. $this->_factory = $factory;
  97. }
  98. /**
  99. * Check, that object is generated by specified factory
  100. *
  101. * @return Zend_Pdf_ElementFactory
  102. */
  103. public function getFactory()
  104. {
  105. return $this->_factory;
  106. }
  107. /**
  108. * Return type of the element.
  109. *
  110. * @return integer
  111. */
  112. public function getType()
  113. {
  114. if ($this->_ref === null) {
  115. $this->_dereference();
  116. }
  117. return $this->_ref->getType();
  118. }
  119. /**
  120. * Return reference to the object
  121. *
  122. * @param Zend_Pdf_Factory $factory
  123. * @return string
  124. */
  125. public function toString($factory = null)
  126. {
  127. if ($factory === null) {
  128. $shift = 0;
  129. } else {
  130. $shift = $factory->getEnumerationShift($this->_factory);
  131. }
  132. return $this->_objNum + $shift . ' ' . $this->_genNum . ' R';
  133. }
  134. /**
  135. * Dereference.
  136. * Take inderect object, take $value member of this object (must be Zend_Pdf_Element),
  137. * take reference to the $value member of this object and assign it to
  138. * $value member of current PDF Reference object
  139. * $obj can be null
  140. *
  141. * @throws Zend_Pdf_Exception
  142. */
  143. private function _dereference()
  144. {
  145. if (($obj = $this->_factory->fetchObject($this->_objNum . ' ' . $this->_genNum)) === null) {
  146. $obj = $this->_context->getParser()->getObject(
  147. $this->_context->getRefTable()->getOffset($this->_objNum . ' ' . $this->_genNum . ' R'),
  148. $this->_context
  149. );
  150. }
  151. if ($obj === null ) {
  152. $this->_ref = new Zend_Pdf_Element_Null();
  153. return;
  154. }
  155. if ($obj->toString() != $this->_objNum . ' ' . $this->_genNum . ' R') {
  156. throw new Zend_Pdf_Exception('Incorrect reference to the object');
  157. }
  158. $this->_ref = $obj;
  159. $this->setParentObject($obj);
  160. $this->_factory->registerObject($obj, $this->_objNum . ' ' . $this->_genNum);
  161. }
  162. /**
  163. * Mark object as modified, to include it into new PDF file segment.
  164. */
  165. public function touch()
  166. {
  167. if ($this->_ref === null) {
  168. $this->_dereference();
  169. }
  170. $this->_ref->touch();
  171. }
  172. /**
  173. * Get handler
  174. *
  175. * @param string $property
  176. * @return mixed
  177. */
  178. public function __get($property)
  179. {
  180. if ($this->_ref === null) {
  181. $this->_dereference();
  182. }
  183. return $this->_ref->$property;
  184. }
  185. /**
  186. * Set handler
  187. *
  188. * @param string $property
  189. * @param mixed $value
  190. */
  191. public function __set($property, $value)
  192. {
  193. if ($this->_ref === null) {
  194. $this->_dereference();
  195. }
  196. $this->_ref->$property = $value;
  197. }
  198. /**
  199. * Call handler
  200. *
  201. * @param string $method
  202. * @param array $args
  203. * @return mixed
  204. */
  205. public function __call($method, $args)
  206. {
  207. if ($this->_ref === null) {
  208. $this->_dereference();
  209. }
  210. return call_user_func_array(array($this->_ref, $method), $args);
  211. }
  212. /**
  213. * Clean up resources
  214. */
  215. public function cleanUp()
  216. {
  217. $this->_ref = null;
  218. }
  219. /**
  220. * Convert PDF element to PHP type.
  221. *
  222. * @return mixed
  223. */
  224. public function toPhp()
  225. {
  226. if ($this->_ref === null) {
  227. $this->_dereference();
  228. }
  229. return $this->_ref->toPhp();
  230. }
  231. }