File indexing completed on 2024-05-12 06:02:53

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  * @subpackage Actions
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Internally used classes */
0024 // require_once 'Zend/Pdf/Element.php';
0025 
0026 
0027 /**
0028  * PDF name tree representation class
0029  *
0030  * @todo implement lazy resource loading so resources will be really loaded at access time
0031  *
0032  * @package    Zend_Pdf
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  */
0036 class Zend_Pdf_NameTree implements ArrayAccess, Iterator, Countable
0037 {
0038     /**
0039      * Elements
0040      * Array of name => object tree entries
0041      *
0042      * @var array
0043      */
0044     protected $_items = array();
0045 
0046     /**
0047      * Object constructor
0048      *
0049      * @param Zend_Pdf_Element $rootDictionary root of name dictionary
0050      */
0051     public function __construct(Zend_Pdf_Element $rootDictionary)
0052     {
0053         if ($rootDictionary->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
0054             // require_once 'Zend/Pdf/Exception.php';
0055             throw new Zend_Pdf_Exception('Name tree root must be a dictionary.');
0056         }
0057 
0058         $intermediateNodes = array();
0059         $leafNodes         = array();
0060         if ($rootDictionary->Kids !== null) {
0061             $intermediateNodes[] = $rootDictionary;
0062         } else {
0063             $leafNodes[] = $rootDictionary;
0064         }
0065 
0066         while (count($intermediateNodes) != 0) {
0067             $newIntermediateNodes = array();
0068             foreach ($intermediateNodes as $node) {
0069                 foreach ($node->Kids->items as $childNode) {
0070                     if ($childNode->Kids !== null) {
0071                         $newIntermediateNodes[] = $childNode;
0072                     } else {
0073                         $leafNodes[] = $childNode;
0074                     }
0075                 }
0076             }
0077             $intermediateNodes = $newIntermediateNodes;
0078         }
0079 
0080         foreach ($leafNodes as $leafNode) {
0081             $destinationsCount = count($leafNode->Names->items)/2;
0082             for ($count = 0; $count < $destinationsCount; $count++) {
0083                 $this->_items[$leafNode->Names->items[$count*2]->value] = $leafNode->Names->items[$count*2 + 1];
0084             }
0085         }
0086     }
0087 
0088     public function current()
0089     {
0090         return current($this->_items);
0091     }
0092 
0093 
0094     public function next()
0095     {
0096         return next($this->_items);
0097     }
0098 
0099 
0100     public function key()
0101     {
0102         return key($this->_items);
0103     }
0104 
0105 
0106     public function valid() {
0107         return current($this->_items)!==false;
0108     }
0109 
0110 
0111     public function rewind()
0112     {
0113         reset($this->_items);
0114     }
0115 
0116 
0117     public function offsetExists($offset)
0118     {
0119         return array_key_exists($offset, $this->_items);
0120     }
0121 
0122 
0123     public function offsetGet($offset)
0124     {
0125         return $this->_items[$offset];
0126     }
0127 
0128 
0129     public function offsetSet($offset, $value)
0130     {
0131         if ($offset === null) {
0132             $this->_items[]        = $value;
0133         } else {
0134             $this->_items[$offset] = $value;
0135         }
0136     }
0137 
0138 
0139     public function offsetUnset($offset)
0140     {
0141         unset($this->_items[$offset]);
0142     }
0143 
0144 
0145     public function clear()
0146     {
0147         $this->_items = array();
0148     }
0149 
0150     public function count()
0151     {
0152         return count($this->_items);
0153     }
0154 }