Array.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /** Zend_Pdf_PhpArray */
  24. require_once 'Zend/Pdf/PhpArray.php';
  25. /**
  26. * PDF file 'array' element implementation
  27. *
  28. * @category Zend
  29. * @package Zend_Pdf
  30. * @copyright Copyright (c) 2005-2009 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_Array extends Zend_Pdf_Element
  34. {
  35. /**
  36. * Object value
  37. * Array of Zend_Pdf_Element objects.
  38. * Appropriate methods must (!) be used to modify it to provide correct
  39. * work with objects and references.
  40. *
  41. * @var Zend_Pdf_PhpArray
  42. */
  43. private $_items;
  44. /**
  45. * Object constructor
  46. *
  47. * @param array $val - array of Zend_Pdf_Element objects
  48. * @throws Zend_Pdf_Exception
  49. */
  50. public function __construct($val = null)
  51. {
  52. $this->_items = new Zend_Pdf_PhpArray();
  53. if ($val !== null && is_array($val)) {
  54. foreach ($val as $element) {
  55. if (!$element instanceof Zend_Pdf_Element) {
  56. throw new Zend_Pdf_Exception('Array elements must be Zend_Pdf_Element objects');
  57. }
  58. $this->_items[] = $element;
  59. }
  60. } else if ($val !== null){
  61. throw new Zend_Pdf_Exception('Argument must be an array');
  62. }
  63. }
  64. /**
  65. * Provides access to $this->_items
  66. *
  67. * @param string $property
  68. * @return Zend_Pdf_PhpArray
  69. */
  70. public function __get($property) {
  71. if ($property=='items') {
  72. return $this->_items;
  73. }
  74. throw new Exception('Undefined property: Zend_Pdf_Element_Array::$' . $property);
  75. }
  76. /**
  77. * Provides read-only access to $this->_items;
  78. *
  79. * @param unknown_type $offset
  80. * @param unknown_type $value
  81. */
  82. public function __set($property, $value) {
  83. if ($property=='items') {
  84. throw new Exception('Array container cannot be overwritten');
  85. }
  86. throw new Exception('Undefined property: Zend_Pdf_Element_Array::$' . $property);
  87. }
  88. /**
  89. * Return type of the element.
  90. *
  91. * @return integer
  92. */
  93. public function getType()
  94. {
  95. return Zend_Pdf_Element::TYPE_ARRAY;
  96. }
  97. /**
  98. * Return object as string
  99. *
  100. * @param Zend_Pdf_Factory $factory
  101. * @return string
  102. */
  103. public function toString($factory = null)
  104. {
  105. $outStr = '[';
  106. $lastNL = 0;
  107. foreach ($this->_items as $element) {
  108. if (strlen($outStr) - $lastNL > 128) {
  109. $outStr .= "\n";
  110. $lastNL = strlen($outStr);
  111. }
  112. $outStr .= $element->toString($factory) . ' ';
  113. }
  114. $outStr .= ']';
  115. return $outStr;
  116. }
  117. /**
  118. * Convert PDF element to PHP type.
  119. *
  120. * Dictionary is returned as an associative array
  121. *
  122. * @return mixed
  123. */
  124. public function toPhp()
  125. {
  126. foreach ($this->_items as $item) {
  127. $phpArray[] = $item->toPhp();
  128. }
  129. return $phpArray;
  130. }
  131. }