Dictionary.php 4.3 KB

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