Array.php 3.7 KB

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