| 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_object($value) && !is_array($value)) { |
| 106 |
$value = json_decode( json_encode( $value ), true ); // convert to array recursively |
| 107 |
} elseif(!is_string($value)) { |
| 108 |
$value = (array) $value; |
| 109 |
} |
| 110 |
|
| 111 |
return $value; |
| 112 |
} |
| 113 |
|
| 114 |
// Object |
| 115 |
if ($type == 'object' || $type == 'obj') { |
| 116 |
if(is_numeric($value)) { |
| 117 |
return $value; |
| 118 |
} elseif (is_string($value) && static::isJson($value)) { |
| 119 |
$value = (object) json_decode($value); |
| 120 |
} elseif (is_string($value) && is_serialized($value)) { |
| 121 |
$value = (object) unserialize($value); |
| 122 |
} elseif(!is_string($value)) { |
| 123 |
$value = (object) $value; |
| 124 |
} elseif (is_array($value)) { |
| 125 |
$value = (object) $value; |
| 126 |
} |
| 127 |
|
| 128 |
return $value; |
| 129 |
} |
| 130 |
|
| 131 |
// Callback |
| 132 |
if (is_callable($type)) { |
| 133 |
return call_user_func($type, $value); |
| 134 |
} |
| 135 |
|
| 136 |
return $value; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Detect is JSON |
| 141 |
* |
| 142 |
* @param $args |
| 143 |
* |
| 144 |
* @return bool |
| 145 |
*/ |
| 146 |
public static function isJson(...$args) |
| 147 |
{ |
| 148 |
if(is_array($args[0]) || is_object($args[0])) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
if (trim($args[0]) === '') { |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
json_decode(...$args); |
| 157 |
return (json_last_error() == JSON_ERROR_NONE); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Checks whether the variable has true or positive value or not |
| 162 |
* |
| 163 |
* @param $var |
| 164 |
* |
| 165 |
* @return bool|string |
| 166 |
*/ |
| 167 |
public static function isTrue( $var ) { |
| 168 |
if ( is_bool( $var ) ) { |
| 169 |
return $var; |
| 170 |
} |
| 171 |
|
| 172 |
if ( is_string( $var ) ){ |
| 173 |
$var = strtolower( $var ); |
| 174 |
if( in_array( $var, [ 'yes', 'on', 'true', 'checked' ] ) ){ |
| 175 |
return true; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if ( is_numeric( $var ) ) { |
| 180 |
return (bool) $var; |
| 181 |
} |
| 182 |
|
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Checks whether the variable has true or positive value or not |
| 188 |
* |
| 189 |
* @param $var |
| 190 |
* |
| 191 |
* @return bool|string |
| 192 |
*/ |
| 193 |
public static function isBool( $var ) { |
| 194 |
if ( is_bool( $var ) ) { |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
if ( is_string( $var ) ){ |
| 199 |
$var = strtolower( $var ); |
| 200 |
if( in_array( $var, [ 'yes', 'on', 'true', 'checked', 'no', 'off', 'false' ] ) ){ |
| 201 |
return true; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if ( is_numeric( $var ) ) { |
| 206 |
return in_array( $var, [ 0, 1 ] ); |
| 207 |
} |
| 208 |
|
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Checks whether the variable is null or empty string |
| 214 |
* |
| 215 |
* @param $var |
| 216 |
* |
| 217 |
* @return bool |
| 218 |
*/ |
| 219 |
public static function isNullOrEmptyStr( $var ) { |
| 220 |
return is_null( $var ) || $var === ''; |
| 221 |
} |
| 222 |
} |
| 223 |
|