2
0

PhpArray.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * @package Zend_Pdf
  16. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. /**
  20. * PHP Array (OO wrapper)
  21. * Used to be returned by reference by __get() methods
  22. *
  23. * @package Zend_Pdf
  24. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. * @todo also implement Countable for PHP 5.1 but not yet to stay 5.0 compatible
  27. */
  28. class Zend_Pdf_PhpArray implements ArrayAccess, Iterator, Countable {
  29. /**
  30. * Array element
  31. * @var mixed
  32. */
  33. protected $_items = array();
  34. /**
  35. * Object constructor
  36. *
  37. * @param array $srcArray
  38. */
  39. public function __construct($srcArray = null)
  40. {
  41. if ($srcArray === null) {
  42. reset($this->_items);
  43. } else if (is_array($srcArray)) {
  44. $this->_items = $srcArray;
  45. } else {
  46. throw new Exception('Array can be initialized only by other array');
  47. }
  48. }
  49. public function current()
  50. {
  51. return current($this->_items);
  52. }
  53. public function next()
  54. {
  55. return next($this->_items);
  56. }
  57. public function key()
  58. {
  59. return key($this->_items);
  60. }
  61. public function valid() {
  62. return current($this->_items)!==false;
  63. }
  64. public function rewind()
  65. {
  66. reset($this->_items);
  67. }
  68. public function offsetExists($offset)
  69. {
  70. return array_key_exists($offset, $this->_items);
  71. }
  72. public function offsetGet($offset)
  73. {
  74. return $this->_items[$offset];
  75. }
  76. public function offsetSet($offset, $value)
  77. {
  78. if ($offset === null) {
  79. $this->_items[] = $value;
  80. } else {
  81. $this->_items[$offset] = $value;
  82. }
  83. }
  84. public function offsetUnset($offset)
  85. {
  86. unset($this->_items[$offset]);
  87. }
  88. public function clear()
  89. {
  90. $this->_items = array();
  91. }
  92. /**
  93. * Defined by Countable interface
  94. *
  95. * @return int
  96. */
  97. public function count()
  98. {
  99. return count($this->_items);
  100. }
  101. }