File indexing completed on 2025-01-19 05:21:21

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Pdf
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 
0023 /** Internally used classes */
0024 // require_once 'Zend/Pdf/Element/Null.php';
0025 
0026 
0027 /** Zend_Pdf_Element */
0028 // require_once 'Zend/Pdf/Element.php';
0029 
0030 /**
0031  * PDF file 'reference' element implementation
0032  *
0033  * @category   Zend
0034  * @package    Zend_Pdf
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Pdf_Element_Reference extends Zend_Pdf_Element
0039 {
0040     /**
0041      * Object value
0042      * The reference to the object
0043      *
0044      * @var mixed
0045      */
0046     private $_ref;
0047 
0048     /**
0049      * Object number within PDF file
0050      *
0051      * @var integer
0052      */
0053     private $_objNum;
0054 
0055     /**
0056      * Generation number
0057      *
0058      * @var integer
0059      */
0060     private $_genNum;
0061 
0062     /**
0063      * Reference context
0064      *
0065      * @var Zend_Pdf_Element_Reference_Context
0066      */
0067     private $_context;
0068 
0069 
0070     /**
0071      * Reference to the factory.
0072      *
0073      * It's the same as referenced object factory, but we save it here to avoid
0074      * unnecessary dereferencing, whech can produce cascade dereferencing and parsing.
0075      * The same for duplication of getFactory() function. It can be processed by __call()
0076      * method, but we catch it here.
0077      *
0078      * @var Zend_Pdf_ElementFactory
0079      */
0080     private $_factory;
0081 
0082     /**
0083      * Object constructor:
0084      *
0085      * @param integer $objNum
0086      * @param integer $genNum
0087      * @param Zend_Pdf_Element_Reference_Context $context
0088      * @param Zend_Pdf_ElementFactory $factory
0089      * @throws Zend_Pdf_Exception
0090      */
0091     public function __construct($objNum, $genNum = 0, Zend_Pdf_Element_Reference_Context $context, Zend_Pdf_ElementFactory $factory)
0092     {
0093         if ( !(is_integer($objNum) && $objNum > 0) ) {
0094             // require_once 'Zend/Pdf/Exception.php';
0095             throw new Zend_Pdf_Exception('Object number must be positive integer');
0096         }
0097         if ( !(is_integer($genNum) && $genNum >= 0) ) {
0098             // require_once 'Zend/Pdf/Exception.php';
0099             throw new Zend_Pdf_Exception('Generation number must be non-negative integer');
0100         }
0101 
0102         $this->_objNum  = $objNum;
0103         $this->_genNum  = $genNum;
0104         $this->_ref     = null;
0105         $this->_context = $context;
0106         $this->_factory = $factory;
0107     }
0108 
0109     /**
0110      * Check, that object is generated by specified factory
0111      *
0112      * @return Zend_Pdf_ElementFactory
0113      */
0114     public function getFactory()
0115     {
0116         return $this->_factory;
0117     }
0118 
0119 
0120     /**
0121      * Return type of the element.
0122      *
0123      * @return integer
0124      */
0125     public function getType()
0126     {
0127         if ($this->_ref === null) {
0128             $this->_dereference();
0129         }
0130 
0131         return $this->_ref->getType();
0132     }
0133 
0134 
0135     /**
0136      * Return reference to the object
0137      *
0138      * @param Zend_Pdf_Factory $factory
0139      * @return string
0140      */
0141     public function toString($factory = null)
0142     {
0143         if ($factory === null) {
0144             $shift = 0;
0145         } else {
0146             $shift = $factory->getEnumerationShift($this->_factory);
0147         }
0148 
0149         return $this->_objNum + $shift . ' ' . $this->_genNum . ' R';
0150     }
0151 
0152 
0153     /**
0154      * Dereference.
0155      * Take inderect object, take $value member of this object (must be Zend_Pdf_Element),
0156      * take reference to the $value member of this object and assign it to
0157      * $value member of current PDF Reference object
0158      * $obj can be null
0159      *
0160      * @throws Zend_Pdf_Exception
0161      */
0162     private function _dereference()
0163     {
0164         if (($obj = $this->_factory->fetchObject($this->_objNum . ' ' . $this->_genNum)) === null) {
0165             $obj = $this->_context->getParser()->getObject(
0166                            $this->_context->getRefTable()->getOffset($this->_objNum . ' ' . $this->_genNum . ' R'),
0167                            $this->_context
0168                                                           );
0169         }
0170 
0171         if ($obj === null ) {
0172             $this->_ref = new Zend_Pdf_Element_Null();
0173             return;
0174         }
0175 
0176         if ($obj->toString() != $this->_objNum . ' ' . $this->_genNum . ' R') {
0177             // require_once 'Zend/Pdf/Exception.php';
0178             throw new Zend_Pdf_Exception('Incorrect reference to the object');
0179         }
0180 
0181         $this->_ref = $obj;
0182     }
0183 
0184     /**
0185      * Detach PDF object from the factory (if applicable), clone it and attach to new factory.
0186      *
0187      * @param Zend_Pdf_ElementFactory $factory  The factory to attach
0188      * @param array &$processed  List of already processed indirect objects, used to avoid objects duplication
0189      * @param integer $mode  Cloning mode (defines filter for objects cloning)
0190      * @returns Zend_Pdf_Element
0191      */
0192     public function makeClone(Zend_Pdf_ElementFactory $factory, array &$processed, $mode)
0193     {
0194         if ($this->_ref === null) {
0195             $this->_dereference();
0196         }
0197 
0198         // This code duplicates code in Zend_Pdf_Element_Object class,
0199         // but allows to avoid unnecessary method call in most cases
0200         $id = spl_object_hash($this->_ref);
0201         if (isset($processed[$id])) {
0202             // Do nothing if object is already processed
0203             // return it
0204             return $processed[$id];
0205         }
0206 
0207         return $this->_ref->makeClone($factory, $processed, $mode);
0208     }
0209 
0210     /**
0211      * Mark object as modified, to include it into new PDF file segment.
0212      */
0213     public function touch()
0214     {
0215         if ($this->_ref === null) {
0216             $this->_dereference();
0217         }
0218 
0219         $this->_ref->touch();
0220     }
0221 
0222     /**
0223      * Return object, which can be used to identify object and its references identity
0224      *
0225      * @return Zend_Pdf_Element_Object
0226      */
0227     public function getObject()
0228     {
0229         if ($this->_ref === null) {
0230             $this->_dereference();
0231         }
0232 
0233         return $this->_ref;
0234     }
0235 
0236     /**
0237      * Get handler
0238      *
0239      * @param string $property
0240      * @return mixed
0241      */
0242     public function __get($property)
0243     {
0244         if ($this->_ref === null) {
0245             $this->_dereference();
0246         }
0247 
0248         return $this->_ref->$property;
0249     }
0250 
0251     /**
0252      * Set handler
0253      *
0254      * @param string $property
0255      * @param  mixed $value
0256      */
0257     public function __set($property, $value)
0258     {
0259         if ($this->_ref === null) {
0260             $this->_dereference();
0261         }
0262 
0263         $this->_ref->$property = $value;
0264     }
0265 
0266     /**
0267      * Call handler
0268      *
0269      * @param string $method
0270      * @param array  $args
0271      * @return mixed
0272      */
0273     public function __call($method, $args)
0274     {
0275         if ($this->_ref === null) {
0276             $this->_dereference();
0277         }
0278 
0279         return call_user_func_array(array($this->_ref, $method), $args);
0280     }
0281 
0282     /**
0283      * Clean up resources
0284      */
0285     public function cleanUp()
0286     {
0287         $this->_ref = null;
0288     }
0289 
0290     /**
0291      * Convert PDF element to PHP type.
0292      *
0293      * @return mixed
0294      */
0295     public function toPhp()
0296     {
0297         if ($this->_ref === null) {
0298             $this->_dereference();
0299         }
0300 
0301         return $this->_ref->toPhp();
0302     }
0303 }