Object.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_ElementFactory */
  24. require_once 'Zend/Pdf/ElementFactory.php';
  25. /**
  26. * PDF file 'indirect object' element implementation
  27. *
  28. * @category Zend
  29. * @package Zend_Pdf
  30. * @copyright Copyright (c) 2005-2009 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_Object extends Zend_Pdf_Element
  34. {
  35. /**
  36. * Object value
  37. *
  38. * @var Zend_Pdf_Element
  39. */
  40. protected $_value;
  41. /**
  42. * Object number within PDF file
  43. *
  44. * @var integer
  45. */
  46. protected $_objNum;
  47. /**
  48. * Generation number
  49. *
  50. * @var integer
  51. */
  52. protected $_genNum;
  53. /**
  54. * Reference to the factory.
  55. *
  56. * @var Zend_Pdf_ElementFactory
  57. */
  58. protected $_factory;
  59. /**
  60. * Object constructor
  61. *
  62. * @param Zend_Pdf_Element $val
  63. * @param integer $objNum
  64. * @param integer $genNum
  65. * @param Zend_Pdf_ElementFactory $factory
  66. * @throws Zend_Pdf_Exception
  67. */
  68. public function __construct(Zend_Pdf_Element $val, $objNum, $genNum, Zend_Pdf_ElementFactory $factory)
  69. {
  70. if ($val instanceof self) {
  71. throw new Zend_Pdf_Exception('Object number must not be instance of Zend_Pdf_Element_Object.');
  72. }
  73. if ( !(is_integer($objNum) && $objNum > 0) ) {
  74. throw new Zend_Pdf_Exception('Object number must be positive integer.');
  75. }
  76. if ( !(is_integer($genNum) && $genNum >= 0) ) {
  77. throw new Zend_Pdf_Exception('Generation number must be non-negative integer.');
  78. }
  79. $this->_value = $val;
  80. $this->_objNum = $objNum;
  81. $this->_genNum = $genNum;
  82. $this->_factory = $factory;
  83. $factory->registerObject($this);
  84. }
  85. /**
  86. * Check, that object is generated by specified factory
  87. *
  88. * @return Zend_Pdf_ElementFactory
  89. */
  90. public function getFactory()
  91. {
  92. return $this->_factory;
  93. }
  94. /**
  95. * Return type of the element.
  96. *
  97. * @return integer
  98. */
  99. public function getType()
  100. {
  101. return $this->_value->getType();
  102. }
  103. /**
  104. * Get object number
  105. *
  106. * @return integer
  107. */
  108. public function getObjNum()
  109. {
  110. return $this->_objNum;
  111. }
  112. /**
  113. * Get generation number
  114. *
  115. * @return integer
  116. */
  117. public function getGenNum()
  118. {
  119. return $this->_genNum;
  120. }
  121. /**
  122. * Return reference to the object
  123. *
  124. * @param Zend_Pdf_Factory $factory
  125. * @return string
  126. */
  127. public function toString($factory = null)
  128. {
  129. if ($factory === null) {
  130. $shift = 0;
  131. } else {
  132. $shift = $factory->getEnumerationShift($this->_factory);
  133. }
  134. return $this->_objNum + $shift . ' ' . $this->_genNum . ' R';
  135. }
  136. /**
  137. * Dump object to a string to save within PDF file.
  138. *
  139. * $factory parameter defines operation context.
  140. *
  141. * @param Zend_Pdf_ElementFactory $factory
  142. * @return string
  143. */
  144. public function dump(Zend_Pdf_ElementFactory $factory)
  145. {
  146. $shift = $factory->getEnumerationShift($this->_factory);
  147. return $this->_objNum + $shift . " " . $this->_genNum . " obj \n"
  148. . $this->_value->toString($factory) . "\n"
  149. . "endobj\n";
  150. }
  151. /**
  152. * Get handler
  153. *
  154. * @param string $property
  155. * @return mixed
  156. */
  157. public function __get($property)
  158. {
  159. return $this->_value->$property;
  160. }
  161. /**
  162. * Set handler
  163. *
  164. * @param string $property
  165. * @param mixed $value
  166. */
  167. public function __set($property, $value)
  168. {
  169. $this->_value->$property = $value;
  170. }
  171. /**
  172. * Call handler
  173. *
  174. * @param string $method
  175. * @param array $args
  176. * @return mixed
  177. */
  178. public function __call($method, $args)
  179. {
  180. switch (count($args)) {
  181. case 0:
  182. return $this->_value->$method();
  183. case 1:
  184. return $this->_value->$method($args[0]);
  185. case 2:
  186. return $this->_value->$method($args[0], $args[1]);
  187. case 3:
  188. return $this->_value->$method($args[0], $args[1], $args[2]);
  189. case 4:
  190. return $this->_value->$method($args[0], $args[1], $args[2], $args[3]);
  191. default:
  192. throw new Zend_Pdf_Exception('Unsupported number of arguments');
  193. }
  194. }
  195. /**
  196. * Mark object as modified, to include it into new PDF file segment
  197. */
  198. public function touch()
  199. {
  200. $this->_factory->markAsModified($this);
  201. }
  202. /**
  203. * Clean up resources, used by object
  204. */
  205. public function cleanUp()
  206. {
  207. $this->_value = null;
  208. }
  209. /**
  210. * Convert PDF element to PHP type.
  211. *
  212. * @return mixed
  213. */
  214. public function toPhp()
  215. {
  216. return $this->_value->toPhp();
  217. }
  218. }