PhpArray.php 2.7 KB

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