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

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