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