web/lib/Zend/Pdf/Element/Reference.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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 22797 2010-08-06 15:02:12Z alexander $
       
    20  */
       
    21 
       
    22 
       
    23 /** Internally used classes */
       
    24 require_once 'Zend/Pdf/Element/Null.php';
       
    25 
       
    26 
       
    27 /** Zend_Pdf_Element */
       
    28 require_once 'Zend/Pdf/Element.php';
       
    29 
       
    30 /**
       
    31  * PDF file 'reference' element implementation
       
    32  *
       
    33  * @category   Zend
       
    34  * @package    Zend_Pdf
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_Pdf_Element_Reference extends Zend_Pdf_Element
       
    39 {
       
    40     /**
       
    41      * Object value
       
    42      * The reference to the object
       
    43      *
       
    44      * @var mixed
       
    45      */
       
    46     private $_ref;
       
    47 
       
    48     /**
       
    49      * Object number within PDF file
       
    50      *
       
    51      * @var integer
       
    52      */
       
    53     private $_objNum;
       
    54 
       
    55     /**
       
    56      * Generation number
       
    57      *
       
    58      * @var integer
       
    59      */
       
    60     private $_genNum;
       
    61 
       
    62     /**
       
    63      * Reference context
       
    64      *
       
    65      * @var Zend_Pdf_Element_Reference_Context
       
    66      */
       
    67     private $_context;
       
    68 
       
    69 
       
    70     /**
       
    71      * Reference to the factory.
       
    72      *
       
    73      * It's the same as referenced object factory, but we save it here to avoid
       
    74      * unnecessary dereferencing, whech can produce cascade dereferencing and parsing.
       
    75      * The same for duplication of getFactory() function. It can be processed by __call()
       
    76      * method, but we catch it here.
       
    77      *
       
    78      * @var Zend_Pdf_ElementFactory
       
    79      */
       
    80     private $_factory;
       
    81 
       
    82     /**
       
    83      * Object constructor:
       
    84      *
       
    85      * @param integer $objNum
       
    86      * @param integer $genNum
       
    87      * @param Zend_Pdf_Element_Reference_Context $context
       
    88      * @param Zend_Pdf_ElementFactory $factory
       
    89      * @throws Zend_Pdf_Exception
       
    90      */
       
    91     public function __construct($objNum, $genNum = 0, Zend_Pdf_Element_Reference_Context $context, Zend_Pdf_ElementFactory $factory)
       
    92     {
       
    93         if ( !(is_integer($objNum) && $objNum > 0) ) {
       
    94             require_once 'Zend/Pdf/Exception.php';
       
    95             throw new Zend_Pdf_Exception('Object number must be positive integer');
       
    96         }
       
    97         if ( !(is_integer($genNum) && $genNum >= 0) ) {
       
    98             require_once 'Zend/Pdf/Exception.php';
       
    99             throw new Zend_Pdf_Exception('Generation number must be non-negative integer');
       
   100         }
       
   101 
       
   102         $this->_objNum  = $objNum;
       
   103         $this->_genNum  = $genNum;
       
   104         $this->_ref     = null;
       
   105         $this->_context = $context;
       
   106         $this->_factory = $factory;
       
   107     }
       
   108 
       
   109     /**
       
   110      * Check, that object is generated by specified factory
       
   111      *
       
   112      * @return Zend_Pdf_ElementFactory
       
   113      */
       
   114     public function getFactory()
       
   115     {
       
   116         return $this->_factory;
       
   117     }
       
   118 
       
   119 
       
   120     /**
       
   121      * Return type of the element.
       
   122      *
       
   123      * @return integer
       
   124      */
       
   125     public function getType()
       
   126     {
       
   127         if ($this->_ref === null) {
       
   128             $this->_dereference();
       
   129         }
       
   130 
       
   131         return $this->_ref->getType();
       
   132     }
       
   133 
       
   134 
       
   135     /**
       
   136      * Return reference to the object
       
   137      *
       
   138      * @param Zend_Pdf_Factory $factory
       
   139      * @return string
       
   140      */
       
   141     public function toString($factory = null)
       
   142     {
       
   143         if ($factory === null) {
       
   144             $shift = 0;
       
   145         } else {
       
   146             $shift = $factory->getEnumerationShift($this->_factory);
       
   147         }
       
   148 
       
   149         return $this->_objNum + $shift . ' ' . $this->_genNum . ' R';
       
   150     }
       
   151 
       
   152 
       
   153     /**
       
   154      * Dereference.
       
   155      * Take inderect object, take $value member of this object (must be Zend_Pdf_Element),
       
   156      * take reference to the $value member of this object and assign it to
       
   157      * $value member of current PDF Reference object
       
   158      * $obj can be null
       
   159      *
       
   160      * @throws Zend_Pdf_Exception
       
   161      */
       
   162     private function _dereference()
       
   163     {
       
   164         if (($obj = $this->_factory->fetchObject($this->_objNum . ' ' . $this->_genNum)) === null) {
       
   165             $obj = $this->_context->getParser()->getObject(
       
   166                            $this->_context->getRefTable()->getOffset($this->_objNum . ' ' . $this->_genNum . ' R'),
       
   167                            $this->_context
       
   168                                                           );
       
   169         }
       
   170 
       
   171         if ($obj === null ) {
       
   172             $this->_ref = new Zend_Pdf_Element_Null();
       
   173             return;
       
   174         }
       
   175 
       
   176         if ($obj->toString() != $this->_objNum . ' ' . $this->_genNum . ' R') {
       
   177             require_once 'Zend/Pdf/Exception.php';
       
   178             throw new Zend_Pdf_Exception('Incorrect reference to the object');
       
   179         }
       
   180 
       
   181         $this->_ref = $obj;
       
   182     }
       
   183 
       
   184     /**
       
   185      * Detach PDF object from the factory (if applicable), clone it and attach to new factory.
       
   186      *
       
   187      * @param Zend_Pdf_ElementFactory $factory  The factory to attach
       
   188      * @param array &$processed  List of already processed indirect objects, used to avoid objects duplication
       
   189      * @param integer $mode  Cloning mode (defines filter for objects cloning)
       
   190      * @returns Zend_Pdf_Element
       
   191      */
       
   192     public function makeClone(Zend_Pdf_ElementFactory $factory, array &$processed, $mode)
       
   193     {
       
   194         if ($this->_ref === null) {
       
   195             $this->_dereference();
       
   196         }
       
   197 
       
   198         // This code duplicates code in Zend_Pdf_Element_Object class,
       
   199         // but allows to avoid unnecessary method call in most cases
       
   200         $id = spl_object_hash($this->_ref);
       
   201         if (isset($processed[$id])) {
       
   202             // Do nothing if object is already processed
       
   203             // return it
       
   204             return $processed[$id];
       
   205         }
       
   206 
       
   207         return $this->_ref->makeClone($factory, $processed, $mode);
       
   208     }
       
   209 
       
   210     /**
       
   211      * Mark object as modified, to include it into new PDF file segment.
       
   212      */
       
   213     public function touch()
       
   214     {
       
   215         if ($this->_ref === null) {
       
   216             $this->_dereference();
       
   217         }
       
   218 
       
   219         $this->_ref->touch();
       
   220     }
       
   221 
       
   222     /**
       
   223      * Return object, which can be used to identify object and its references identity
       
   224      *
       
   225      * @return Zend_Pdf_Element_Object
       
   226      */
       
   227     public function getObject()
       
   228     {
       
   229         if ($this->_ref === null) {
       
   230             $this->_dereference();
       
   231         }
       
   232 
       
   233         return $this->_ref;
       
   234     }
       
   235 
       
   236     /**
       
   237      * Get handler
       
   238      *
       
   239      * @param string $property
       
   240      * @return mixed
       
   241      */
       
   242     public function __get($property)
       
   243     {
       
   244         if ($this->_ref === null) {
       
   245             $this->_dereference();
       
   246         }
       
   247 
       
   248         return $this->_ref->$property;
       
   249     }
       
   250 
       
   251     /**
       
   252      * Set handler
       
   253      *
       
   254      * @param string $property
       
   255      * @param  mixed $value
       
   256      */
       
   257     public function __set($property, $value)
       
   258     {
       
   259         if ($this->_ref === null) {
       
   260             $this->_dereference();
       
   261         }
       
   262 
       
   263         $this->_ref->$property = $value;
       
   264     }
       
   265 
       
   266     /**
       
   267      * Call handler
       
   268      *
       
   269      * @param string $method
       
   270      * @param array  $args
       
   271      * @return mixed
       
   272      */
       
   273     public function __call($method, $args)
       
   274     {
       
   275         if ($this->_ref === null) {
       
   276             $this->_dereference();
       
   277         }
       
   278 
       
   279         return call_user_func_array(array($this->_ref, $method), $args);
       
   280     }
       
   281 
       
   282     /**
       
   283      * Clean up resources
       
   284      */
       
   285     public function cleanUp()
       
   286     {
       
   287         $this->_ref = null;
       
   288     }
       
   289 
       
   290     /**
       
   291      * Convert PDF element to PHP type.
       
   292      *
       
   293      * @return mixed
       
   294      */
       
   295     public function toPhp()
       
   296     {
       
   297         if ($this->_ref === null) {
       
   298             $this->_dereference();
       
   299         }
       
   300 
       
   301         return $this->_ref->toPhp();
       
   302     }
       
   303 }