PodsArray

The Pods - Custom Content Types and Fields PodsArray class.

Defined (1)

The class is defined in the following location(s).

/classes/PodsArray.php  
  1. class PodsArray implements ArrayAccess { 
  2.  
  3. /** 
  4. * @var array|mixed 
  5. */ 
  6. private $__container = array(); 
  7.  
  8. /** 
  9. * Alternative to get_object_vars to access an object as an array with simple functionality and accepts arrays to 
  10. * add additional functionality. Additional functionality includes validation and setting default data. 
  11. * @param mixed $container Object (or existing Array) 
  12. * @return \PodsArray 
  13. * @license http://www.gnu.org/licenses/gpl-2.0.html 
  14. * @since 2.0 
  15. */ 
  16. public function __construct ( $container ) { 
  17. if ( is_array( $container ) || is_object( $container ) ) 
  18. $this->__container = &$container; 
  19.  
  20. /** 
  21. * Set value from array usage $object['offset'] = 'value'; 
  22. * @param mixed $offset Used to set index of Array or Variable name on Object 
  23. * @param mixed $value Value to be set 
  24. * @return mixed|void 
  25. * @since 2.0 
  26. */ 
  27. public function offsetSet ( $offset, $value ) { 
  28. if ( is_array( $this->__container ) ) 
  29. $this->__container[ $offset ] = $value; 
  30. else 
  31. $this->__container->$offset = $value; 
  32. return $value; 
  33.  
  34. /** 
  35. * Get value from array usage $object['offset']; 
  36. * @param mixed $offset Used to get value of Array or Variable on Object 
  37. * @return mixed|null 
  38. * @since 2.0 
  39. */ 
  40. public function offsetGet ( $offset ) { 
  41. if ( is_array( $this->__container ) ) { 
  42. if ( isset( $this->__container[ $offset ] ) ) 
  43. return $this->__container[ $offset ]; 
  44. return null; 
  45. if ( isset( $this->__container->$offset ) ) 
  46. return $this->__container->$offset; 
  47. return null; 
  48.  
  49. /** 
  50. * Get value from array usage $object['offset']; 
  51. * @param mixed $offset Used to get value of Array or Variable on Object 
  52. * @return bool 
  53. * @since 2.0 
  54. */ 
  55. public function offsetExists ( $offset ) { 
  56. if ( is_array( $this->__container ) ) 
  57. return isset( $this->__container[ $offset ] ); 
  58. return isset( $this->__container->$offset ); 
  59.  
  60. /** 
  61. * Get value from array usage $object['offset']; 
  62. * @param mixed $offset Used to unset index of Array or Variable on Object 
  63. * @since 2.0 
  64. */ 
  65. public function offsetUnset ( $offset ) { 
  66. if ( is_array( $this->__container ) ) 
  67. unset( $this->__container[ $offset ] ); 
  68. else 
  69. unset( $this->__container->$offset ); 
  70.  
  71. /** 
  72. * Validate value on a specific type and set default (if empty) 
  73. * @param mixed $offset Used to get value of Array or Variable on Object 
  74. * @param mixed $default Used to set default value if it doesn't exist 
  75. * @param string $type Used to force a specific type of variable (allowed: array, object, integer, absint, boolean) 
  76. * @param mixed $extra Used in advanced types of variables 
  77. * @return array|bool|int|mixed|null|number|object 
  78. * @since 2.0 
  79. */ 
  80. public function validate ( $offset, $default = null, $type = null, $extra = null ) { 
  81. if ( !$this->offsetExists( $offset ) ) 
  82. $this->offsetSet( $offset, $default ); 
  83.  
  84. $value = $this->offsetGet( $offset ); 
  85.  
  86. if ( empty( $value ) && null !== $default && false !== $value ) 
  87. $value = $default; 
  88. if ( 'array' == $type || 'array_merge' == $type ) { 
  89. if ( !is_array( $value ) ) 
  90. $value = explode( ', ', $value ); 
  91.  
  92. if ( 'array_merge' == $type && $value !== $default ) 
  93. $value = array_merge( $default, $value ); 
  94. elseif ( 'object' == $type || 'object_merge' == $type ) { 
  95. if ( !is_object( $value ) ) { 
  96. if ( !is_array( $value ) ) 
  97. $value = explode( ', ', $value ); 
  98. $value = (object) $value; 
  99.  
  100. if ( 'object_merge' == $type && $value !== $default ) 
  101. $value = (object) array_merge( (array) $default, (array) $value ); 
  102. elseif ( 'integer' == $type || 'int' == $type || 'absint' == $type ) { 
  103. if ( !is_numeric( trim( $value ) ) ) 
  104. $value = 0; 
  105. else 
  106. $value = intval( $value ); 
  107.  
  108. if ( 'absint' == $type ) 
  109. $value = abs( $value ); 
  110. elseif ( 'boolean' == $type || 'bool' == $type ) 
  111. $value = (boolean) $value; 
  112. elseif ( 'in_array' == $type && is_array( $default ) ) { 
  113. if ( is_array( $value ) ) { 
  114. foreach ( $value as $k => $v ) { 
  115. if ( !in_array( $v, $extra ) ) 
  116. unset( $value[ $k ] ); 
  117. elseif ( !in_array( $value, $extra ) ) 
  118. $value = $default; 
  119. elseif ( 'isset' == $type && is_array( $default ) ) { 
  120. if ( is_array( $value ) ) { 
  121. foreach ( $value as $k => $v ) { 
  122. if ( !isset( $extra[ $v ] ) ) 
  123. unset( $value[ $k ] ); 
  124. elseif ( !isset( $extra[ $value ] ) ) 
  125. $value = $default; 
  126.  
  127. $this->offsetSet( $offset, $value ); 
  128.  
  129. return $value; 
  130.  
  131. /** 
  132. * Dump the PodsArray object to array 
  133. * @return array Array version of the object 
  134. * @since 2.0 
  135. */ 
  136. public function dump () { 
  137. if ( is_array( $this->__container ) ) 
  138. return $this->__container; 
  139. return get_object_vars( $this->__container ); 
  140.  
  141. /** 
  142. * Mapping >> offsetSet 
  143. * @since 2.0 
  144. */ 
  145. public function __set ( $offset, $value ) { 
  146. return $this->offsetSet( $offset, $value ); 
  147.  
  148. /** 
  149. * Mapping >> offsetGet 
  150. * @since 2.0 
  151. */ 
  152. public function __get ( $offset ) { 
  153. return $this->offsetGet( $offset ); 
  154.  
  155. /** 
  156. * Mapping >> offsetExists 
  157. * @since 2.0 
  158. */ 
  159. public function __isset ( $offset ) { 
  160. return $this->offsetExists( $offset ); 
  161.  
  162. /** 
  163. * Mapping >> offsetUnset 
  164. * @since 2.0 
  165. */ 
  166. public function __unset ( $offset ) { 
  167. $this->offsetUnset( $offset );