PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/zend/Zend/Pdf/Element/Reference.php

http://zoop.googlecode.com/
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
 22
 23/** Internally used classes */
 24require_once 'Zend/Pdf/Element/Null.php';
 25
 26
 27/** Zend_Pdf_Element */
 28require_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 */
 38class 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     * Mark object as modified, to include it into new PDF file segment.
186     */
187    public function touch()
188    {
189        if ($this->_ref === null) {
190            $this->_dereference();
191        }
192
193        $this->_ref->touch();
194    }
195
196    /**
197     * Return object, which can be used to identify object and its references identity
198     *
199     * @return Zend_Pdf_Element_Object
200     */
201    public function getObject()
202    {
203        if ($this->_ref === null) {
204            $this->_dereference();
205        }
206
207        return $this->_ref;
208    }
209
210    /**
211     * Get handler
212     *
213     * @param string $property
214     * @return mixed
215     */
216    public function __get($property)
217    {
218        if ($this->_ref === null) {
219            $this->_dereference();
220        }
221
222        return $this->_ref->$property;
223    }
224
225    /**
226     * Set handler
227     *
228     * @param string $property
229     * @param  mixed $value
230     */
231    public function __set($property, $value)
232    {
233        if ($this->_ref === null) {
234            $this->_dereference();
235        }
236
237        $this->_ref->$property = $value;
238    }
239
240    /**
241     * Call handler
242     *
243     * @param string $method
244     * @param array  $args
245     * @return mixed
246     */
247    public function __call($method, $args)
248    {
249        if ($this->_ref === null) {
250            $this->_dereference();
251        }
252
253        return call_user_func_array(array($this->_ref, $method), $args);
254    }
255
256    /**
257     * Clean up resources
258     */
259    public function cleanUp()
260    {
261        $this->_ref = null;
262    }
263
264    /**
265     * Convert PDF element to PHP type.
266     *
267     * @return mixed
268     */
269    public function toPhp()
270    {
271        if ($this->_ref === null) {
272            $this->_dereference();
273        }
274
275        return $this->_ref->toPhp();
276    }
277}