Dictionary.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** Zend_Pdf_Element */
  22. require_once 'Zend/Pdf/Element.php';
  23. /**
  24. * PDF file 'dictionary' element implementation
  25. *
  26. * @category Zend
  27. * @package Zend_Pdf
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Pdf_Element_Dictionary extends Zend_Pdf_Element
  32. {
  33. /**
  34. * Dictionary elements
  35. * Array of Zend_Pdf_Element objects ('name' => Zend_Pdf_Element)
  36. *
  37. * @var array
  38. */
  39. private $_items = array();
  40. /**
  41. * Object constructor
  42. *
  43. * @param array $val - array of Zend_Pdf_Element objects
  44. * @throws Zend_Pdf_Exception
  45. */
  46. public function __construct($val = null)
  47. {
  48. if ($val === null) {
  49. return;
  50. } else if (!is_array($val)) {
  51. throw new Zend_Pdf_Exception('Argument must be an array');
  52. }
  53. foreach ($val as $name => $element) {
  54. if (!$element instanceof Zend_Pdf_Element) {
  55. throw new Zend_Pdf_Exception('Array elements must be Zend_Pdf_Element objects');
  56. }
  57. if (!is_string($name)) {
  58. throw new Zend_Pdf_Exception('Array keys must be strings');
  59. }
  60. $this->_items[$name] = $element;
  61. }
  62. }
  63. /**
  64. * Add element to an array
  65. *
  66. * @name Zend_Pdf_Element_Name $name
  67. * @param Zend_Pdf_Element $val - Zend_Pdf_Element object
  68. * @throws Zend_Pdf_Exception
  69. */
  70. public function add(Zend_Pdf_Element_Name $name, Zend_Pdf_Element $val)
  71. {
  72. $this->_items[$name->value] = $val;
  73. }
  74. /**
  75. * Return dictionary keys
  76. *
  77. * @return array
  78. */
  79. public function getKeys()
  80. {
  81. return array_keys($this->_items);
  82. }
  83. /**
  84. * Get handler
  85. *
  86. * @param string $property
  87. * @return Zend_Pdf_Element | null
  88. */
  89. public function __get($item)
  90. {
  91. $element = isset($this->_items[$item]) ? $this->_items[$item]
  92. : null;
  93. return $element;
  94. }
  95. /**
  96. * Set handler
  97. *
  98. * @param string $property
  99. * @param mixed $value
  100. */
  101. public function __set($item, $value)
  102. {
  103. if ($value === null) {
  104. unset($this->_items[$item]);
  105. } else {
  106. $this->_items[$item] = $value;
  107. }
  108. }
  109. /**
  110. * Return type of the element.
  111. *
  112. * @return integer
  113. */
  114. public function getType()
  115. {
  116. return Zend_Pdf_Element::TYPE_DICTIONARY;
  117. }
  118. /**
  119. * Return object as string
  120. *
  121. * @param Zend_Pdf_Factory $factory
  122. * @return string
  123. */
  124. public function toString($factory = null)
  125. {
  126. $outStr = '<<';
  127. $lastNL = 0;
  128. foreach ($this->_items as $name => $element) {
  129. if (!is_object($element)) {
  130. throw new Zend_Pdf_Exception('Wrong data');
  131. }
  132. if (strlen($outStr) - $lastNL > 128) {
  133. $outStr .= "\n";
  134. $lastNL = strlen($outStr);
  135. }
  136. $nameObj = new Zend_Pdf_Element_Name($name);
  137. $outStr .= $nameObj->toString($factory) . ' ' . $element->toString($factory) . ' ';
  138. }
  139. $outStr .= '>>';
  140. return $outStr;
  141. }
  142. /**
  143. * Convert PDF element to PHP type.
  144. *
  145. * Dictionary is returned as an associative array
  146. *
  147. * @return mixed
  148. */
  149. public function toPhp()
  150. {
  151. $phpArray = array();
  152. foreach ($this->_items as $itemName => $item) {
  153. $phpArray[$itemName] = $item->toPhp();
  154. }
  155. return $phpArray;
  156. }
  157. }