PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / averta / core / src / Utility / Data.php

Data.php in Depicter — Popup & Slider Builder 1.2.0, at vendor/averta/core/src/Utility/Data.php

221 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Averta\Core\Utility;
3
4 class Data
5 {
6 /**
7 * Dots Walk
8 *
9 * Traverse array with dot notation.
10 *
11 * @param string|array $dots dot notation key.next.final
12 * @param array|object $array an array to traverse
13 * @param null|mixed $default
14 *
15 * @return array|mixed|null
16 */
17 public static function walk($dots, $array, $default = null)
18 {
19 $traverse = is_array($dots) ? $dots : explode('.', $dots);
20 foreach ($traverse as $i => $step) {
21 unset($traverse[$i]);
22 if($step === '*' && is_array($array)) {
23 return array_map(function($item) use ($traverse, $default) {
24 return static::walk($traverse, $item, $default);
25 }, $array);
26 } else {
27 $v = is_object($array) ? ($array->$step ?? null) : ($array[$step] ?? null);
28 }
29
30 if ( !isset($v) && ! is_string($array) ) {
31 return $default;
32 }
33 $array = $v ?? $default;
34 }
35
36 return $array;
37 }
38
39 /**
40 * @param mixed $value
41 * @param string|callable $type
42 *
43 * @return bool|float|int|mixed|string
44 */
45 public static function cast($value, $type)
46 {
47 // Integer
48 if ($type == 'int' || $type == 'integer') {
49 return is_object($value) || is_array($value) ? null : (int) $value;
50 }
51
52 // Float
53 if ($type == 'float' || $type == 'double' || $type == 'real') {
54 return is_object($value) || is_array($value) ? null : (float) $value;
55 }
56
57 // JSON
58 if ($type == 'json') {
59
60 if(is_serialized($value)) {
61 $value = unserialize($value);
62 } if(static::isJson($value)) {
63 return $value;
64 }
65
66 return json_encode($value);
67 }
68
69 // Serialize
70 if ($type == 'serialize' || $type == 'serial') {
71
72 if(static::isJson($value)) {
73 $value = json_decode((string) $value, true);
74 } if(is_serialized($value)) {
75 return $value;
76 }
77
78 return serialize($value);
79 }
80
81 // String
82 if ($type == 'str' || $type == 'string') {
83 if(is_object($value) || is_array($value)) {
84 $value = json_encode($value);
85 } else {
86 $value = (string) $value;
87 }
88
89 return $value;
90 }
91
92 // Bool
93 if ($type == 'bool' || $type == 'boolean') {
94 return (bool) $value;
95 }
96
97 // Array
98 if ($type == 'array') {
99 if(is_numeric($value)) {
100 return $value;
101 } elseif (is_string($value) && static::isJson($value)) {
102 $value = json_decode($value, true);
103 } elseif (is_string($value) && is_serialized($value)) {
104 $value = unserialize($value);
105 } elseif(!is_string($value)) {
106 $value = (array) $value;
107 }
108
109 return $value;
110 }
111
112 // Object
113 if ($type == 'object' || $type == 'obj') {
114 if(is_numeric($value)) {
115 return $value;
116 } elseif (is_string($value) && static::isJson($value)) {
117 $value = (object) json_decode($value);
118 } elseif (is_string($value) && is_serialized($value)) {
119 $value = (object) unserialize($value);
120 } elseif(!is_string($value)) {
121 $value = (object) $value;
122 } elseif (is_array($value)) {
123 $value = (object) $value;
124 }
125
126 return $value;
127 }
128
129 // Callback
130 if (is_callable($type)) {
131 return call_user_func($type, $value);
132 }
133
134 return $value;
135 }
136
137 /**
138 * Detect is JSON
139 *
140 * @param $args
141 *
142 * @return bool
143 */
144 public static function isJson(...$args)
145 {
146 if(is_array($args[0]) || is_object($args[0])) {
147 return false;
148 }
149
150 if (trim($args[0]) === '') {
151 return false;
152 }
153
154 json_decode(...$args);
155 return (json_last_error() == JSON_ERROR_NONE);
156 }
157
158 /**
159 * Checks whether the variable has true or positive value or not
160 *
161 * @param $var
162 *
163 * @return bool|string
164 */
165 public static function isTrue( $var ) {
166 if ( is_bool( $var ) ) {
167 return $var;
168 }
169
170 if ( is_string( $var ) ){
171 $var = strtolower( $var );
172 if( in_array( $var, [ 'yes', 'on', 'true', 'checked' ] ) ){
173 return true;
174 }
175 }
176
177 if ( is_numeric( $var ) ) {
178 return (bool) $var;
179 }
180
181 return false;
182 }
183
184 /**
185 * Checks whether the variable has true or positive value or not
186 *
187 * @param $var
188 *
189 * @return bool|string
190 */
191 public static function isBool( $var ) {
192 if ( is_bool( $var ) ) {
193 return true;
194 }
195
196 if ( is_string( $var ) ){
197 $var = strtolower( $var );
198 if( in_array( $var, [ 'yes', 'on', 'true', 'checked', 'no', 'off', 'false' ] ) ){
199 return true;
200 }
201 }
202
203 if ( is_numeric( $var ) ) {
204 return in_array( $var, [ 0, 1 ] );
205 }
206
207 return false;
208 }
209
210 /**
211 * Checks whether the variable is null or empty string
212 *
213 * @param $var
214 *
215 * @return bool
216 */
217 public static function isNullOrEmptyStr( $var ) {
218 return is_null( $var ) || $var === '';
219 }
220 }
221