forked from laraish/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.php
56 lines (47 loc) · 1.22 KB
/
Helper.php
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
<?php
namespace Laraish;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class Helper
{
/**
* @param array $array
* @param bool $snakeCaseKey
* @return array|\stdClass
*/
public static function arrayToObject(array $array, bool $snakeCaseKey = true)
{
if (!Arr::isAssoc($array)) {
foreach ($array as &$value) {
if (is_array($value)) {
$value = static::arrayToObject($value);
}
}
return $array;
}
$obj = new \stdClass;
foreach ($array as $key => $value) {
if ($snakeCaseKey) {
$key = str_replace('-', '_', Str::snake($key));
}
if (is_array($value)) {
$obj->{$key} = static::arrayToObject($value);
} else {
$obj->{$key} = $value;
}
}
return $obj;
}
public static function isArrayOfType($value, string $type): bool
{
if (!is_array($value)) {
return false;
}
foreach ($value as $item) {
if (!($item instanceof $type)) {
return false;
}
}
return true;
}
}