/library/Zend/Pdf/NameTree.php
PHP | 154 lines | 86 code | 27 blank | 41 comment | 11 complexity | e777f67f422e0fe54e399bad63527f85 MD5 | raw file
Possible License(s): AGPL-1.0
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 * @subpackage Actions
18 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: NameTree.php 24594 2012-01-05 21:27:01Z matthew $
21 */
22
23/** Internally used classes */
24require_once 'Zend/Pdf/Element.php';
25
26
27/**
28 * PDF name tree representation class
29 *
30 * @todo implement lazy resource loading so resources will be really loaded at access time
31 *
32 * @package Zend_Pdf
33 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
35 */
36class Zend_Pdf_NameTree implements ArrayAccess, Iterator, Countable
37{
38 /**
39 * Elements
40 * Array of name => object tree entries
41 *
42 * @var array
43 */
44 protected $_items = array();
45
46 /**
47 * Object constructor
48 *
49 * @param Zend_Pdf_Element $rootDictionary root of name dictionary
50 */
51 public function __construct(Zend_Pdf_Element $rootDictionary)
52 {
53 if ($rootDictionary->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
54 require_once 'Zend/Pdf/Exception.php';
55 throw new Zend_Pdf_Exception('Name tree root must be a dictionary.');
56 }
57
58 $intermediateNodes = array();
59 $leafNodes = array();
60 if ($rootDictionary->Kids !== null) {
61 $intermediateNodes[] = $rootDictionary;
62 } else {
63 $leafNodes[] = $rootDictionary;
64 }
65
66 while (count($intermediateNodes) != 0) {
67 $newIntermediateNodes = array();
68 foreach ($intermediateNodes as $node) {
69 foreach ($node->Kids->items as $childNode) {
70 if ($childNode->Kids !== null) {
71 $newIntermediateNodes[] = $childNode;
72 } else {
73 $leafNodes[] = $childNode;
74 }
75 }
76 }
77 $intermediateNodes = $newIntermediateNodes;
78 }
79
80 foreach ($leafNodes as $leafNode) {
81 $destinationsCount = count($leafNode->Names->items)/2;
82 for ($count = 0; $count < $destinationsCount; $count++) {
83 $this->_items[$leafNode->Names->items[$count*2]->value] = $leafNode->Names->items[$count*2 + 1];
84 }
85 }
86 }
87
88 public function current()
89 {
90 return current($this->_items);
91 }
92
93
94 public function next()
95 {
96 return next($this->_items);
97 }
98
99
100 public function key()
101 {
102 return key($this->_items);
103 }
104
105
106 public function valid() {
107 return current($this->_items)!==false;
108 }
109
110
111 public function rewind()
112 {
113 reset($this->_items);
114 }
115
116
117 public function offsetExists($offset)
118 {
119 return array_key_exists($offset, $this->_items);
120 }
121
122
123 public function offsetGet($offset)
124 {
125 return $this->_items[$offset];
126 }
127
128
129 public function offsetSet($offset, $value)
130 {
131 if ($offset === null) {
132 $this->_items[] = $value;
133 } else {
134 $this->_items[$offset] = $value;
135 }
136 }
137
138
139 public function offsetUnset($offset)
140 {
141 unset($this->_items[$offset]);
142 }
143
144
145 public function clear()
146 {
147 $this->_items = array();
148 }
149
150 public function count()
151 {
152 return count($this->_items);
153 }
154}