Reference.php 6.8 KB

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