class-recursive-arrayaccess.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Multidimensional ArrayAccess
  4. *
  5. * Allows ArrayAccess-like functionality with multidimensional arrays. Fully supports
  6. * both sets and unsets.
  7. *
  8. * @package WordPress
  9. * @subpackage Session
  10. * @since 3.6.0
  11. */
  12. // Exit if accessed directly
  13. if ( ! defined( 'ABSPATH' ) ) exit;
  14. /**
  15. * Recursive array class to allow multidimensional array access.
  16. *
  17. * @package WordPress
  18. * @since 3.6.0
  19. */
  20. class Recursive_ArrayAccess implements ArrayAccess {
  21. /**
  22. * Internal data collection.
  23. *
  24. * @var array
  25. */
  26. protected $container = array();
  27. /**
  28. * Flag whether or not the internal collection has been changed.
  29. *
  30. * @var bool
  31. */
  32. protected $dirty = false;
  33. /**
  34. * Default object constructor.
  35. *
  36. * @param array $data
  37. */
  38. protected function __construct( $data = array() ) {
  39. foreach ( $data as $key => $value ) {
  40. $this[ $key ] = $value;
  41. }
  42. }
  43. /**
  44. * Allow deep copies of objects
  45. */
  46. public function __clone() {
  47. foreach ( $this->container as $key => $value ) {
  48. if ( $value instanceof self ) {
  49. $this[ $key ] = clone $value;
  50. }
  51. }
  52. }
  53. /**
  54. * Output the data container as a multidimensional array.
  55. *
  56. * @return array
  57. */
  58. public function toArray() {
  59. $data = $this->container;
  60. foreach ( $data as $key => $value ) {
  61. if ( $value instanceof self ) {
  62. $data[ $key ] = $value->toArray();
  63. }
  64. }
  65. return $data;
  66. }
  67. /**
  68. * ArrayAccess Implementation
  69. **/
  70. /**
  71. * Whether a offset exists
  72. *
  73. * @link http://php.net/manual/en/arrayaccess.offsetexists.php
  74. *
  75. * @param mixed $offset An offset to check for.
  76. *
  77. * @return boolean true on success or false on failure.
  78. */
  79. public function offsetExists( $offset ) {
  80. return isset( $this->container[ $offset ]) ;
  81. }
  82. /**
  83. * Offset to retrieve
  84. *
  85. * @link http://php.net/manual/en/arrayaccess.offsetget.php
  86. *
  87. * @param mixed $offset The offset to retrieve.
  88. *
  89. * @return mixed Can return all value types.
  90. */
  91. public function offsetGet( $offset ) {
  92. return isset( $this->container[ $offset ] ) ? $this->container[ $offset ] : null;
  93. }
  94. /**
  95. * Offset to set
  96. *
  97. * @link http://php.net/manual/en/arrayaccess.offsetset.php
  98. *
  99. * @param mixed $offset The offset to assign the value to.
  100. * @param mixed $value The value to set.
  101. *
  102. * @return void
  103. */
  104. public function offsetSet( $offset, $data ) {
  105. if ( is_array( $data ) ) {
  106. $data = new self( $data );
  107. }
  108. if ( $offset === null ) { // don't forget this!
  109. $this->container[] = $data;
  110. } else {
  111. $this->container[ $offset ] = $data;
  112. }
  113. $this->dirty = true;
  114. }
  115. /**
  116. * Offset to unset
  117. *
  118. * @link http://php.net/manual/en/arrayaccess.offsetunset.php
  119. *
  120. * @param mixed $offset The offset to unset.
  121. *
  122. * @return void
  123. */
  124. public function offsetUnset( $offset ) {
  125. unset( $this->container[ $offset ] );
  126. }
  127. }