| 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: | |
| 13: | // Exit if accessed directly. |
| 14: | if ( ! defined( 'ABSPATH' ) ) { |
| 15: | exit; |
| 16: | } |
| 17: | |
| 18: | /** |
| 19: | * Recursive array class to allow multidimensional array access. |
| 20: | * |
| 21: | * @package WordPress |
| 22: | * @since 3.6.0 |
| 23: | */ |
| 24: | class Recursive_ArrayAccess implements ArrayAccess { |
| 25: | /** |
| 26: | * Internal data collection. |
| 27: | * |
| 28: | * @var array |
| 29: | */ |
| 30: | protected $container = array(); |
| 31: | |
| 32: | /** |
| 33: | * Flag whether or not the internal collection has been changed. |
| 34: | * |
| 35: | * @var bool |
| 36: | */ |
| 37: | protected $dirty = false; |
| 38: | |
| 39: | /** |
| 40: | * Default object constructor. |
| 41: | * |
| 42: | * @param array $data Array of data. |
| 43: | */ |
| 44: | protected function __construct( $data = array() ) { |
| 45: | foreach ( $data as $key => $value ) { |
| 46: | $this[ $key ] = $value; |
| 47: | } |
| 48: | } |
| 49: | |
| 50: | /** |
| 51: | * Allow deep copies of objects |
| 52: | */ |
| 53: | public function __clone() { |
| 54: | foreach ( $this->container as $key => $value ) { |
| 55: | if ( $value instanceof self ) { |
| 56: | $this[ $key ] = clone $value; |
| 57: | } |
| 58: | } |
| 59: | } |
| 60: | |
| 61: | /** |
| 62: | * Output the data container as a multidimensional array. |
| 63: | * |
| 64: | * @return array |
| 65: | */ |
| 66: | public function toArray() { |
| 67: | $data = $this->container; |
| 68: | foreach ( $data as $key => $value ) { |
| 69: | if ( $value instanceof self ) { |
| 70: | $data[ $key ] = $value->toArray(); |
| 71: | } |
| 72: | } |
| 73: | return $data; |
| 74: | } |
| 75: | |
| 76: | /** |
| 77: | * ArrayAccess Implementation |
| 78: | **/ |
| 79: | |
| 80: | /** |
| 81: | * Whether a offset exists |
| 82: | * |
| 83: | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| 84: | * |
| 85: | * @param mixed $offset An offset to check for. |
| 86: | * |
| 87: | * @return boolean true on success or false on failure. |
| 88: | */ |
| 89: | #[\ReturnTypeWillChange] // phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound, Squiz.Commenting.FunctionComment.Missing |
| 90: | public function offsetExists( $offset ) { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
| 91: | return isset( $this->container[ $offset ] ); |
| 92: | } |
| 93: | |
| 94: | /** |
| 95: | * Offset to retrieve |
| 96: | * |
| 97: | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
| 98: | * |
| 99: | * @param mixed $offset The offset to retrieve. |
| 100: | * |
| 101: | * @return mixed Can return all value types. |
| 102: | */ |
| 103: | #[\ReturnTypeWillChange] // phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound, Squiz.Commenting.FunctionComment.Missing, Squiz.Commenting.FunctionComment.Missing |
| 104: | public function offsetGet( $offset ) { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
| 105: | return isset( $this->container[ $offset ] ) ? $this->container[ $offset ] : null; |
| 106: | } |
| 107: | |
| 108: | /** |
| 109: | * Offset to set |
| 110: | * |
| 111: | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
| 112: | * |
| 113: | * @param mixed $offset The offset to assign the value to. |
| 114: | * @param mixed $data The value to set. |
| 115: | * |
| 116: | * @return void |
| 117: | */ |
| 118: | #[\ReturnTypeWillChange] // phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound, Squiz.Commenting.FunctionComment.Missing, Squiz.Commenting.FunctionComment.Missing |
| 119: | public function offsetSet( $offset, $data ) { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
| 120: | if ( is_array( $data ) ) { |
| 121: | $data = new self( $data ); |
| 122: | } |
| 123: | if ( null === $offset ) { // Don't forget this! |
| 124: | $this->container[] = $data; |
| 125: | } else { |
| 126: | $this->container[ $offset ] = $data; |
| 127: | } |
| 128: | |
| 129: | $this->dirty = true; |
| 130: | } |
| 131: | |
| 132: | /** |
| 133: | * Offset to unset |
| 134: | * |
| 135: | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| 136: | * |
| 137: | * @param mixed $offset The offset to unset. |
| 138: | * |
| 139: | * @return void |
| 140: | */ |
| 141: | #[\ReturnTypeWillChange] // phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound, Squiz.Commenting.FunctionComment.Missing, Squiz.Commenting.FunctionComment.Missing |
| 142: | public function offsetUnset( $offset ) { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
| 143: | unset( $this->container[ $offset ] ); |
| 144: | } |
| 145: | } |
| 146: |