/lib/vendor/Zend/Pdf/Element/Reference.php

https://github.com/frhumanes/PLM · PHP · 277 lines · 111 code · 37 blank · 129 comment · 17 complexity · d86d3d790ff653fc4864f1179d490254 MD5 · raw file

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