1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace alkemann\h2l; |
4
|
|
|
|
5
|
|
|
use OutOfBoundsException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Util |
9
|
|
|
* |
10
|
|
|
* @package alkemann\h2l |
11
|
|
|
*/ |
12
|
|
|
class Util |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Look for a deeo value in a nested data array. |
17
|
|
|
* |
18
|
|
|
* Given $data = ['one' => ['two' => ['three' => 55]], 'four' => []]; |
19
|
|
|
* |
20
|
|
|
* ```php |
21
|
|
|
* getFromArrayByKey('one.two.three', $data) -> 55 |
22
|
|
|
* getFromArrayByKey('one|two', $data, '|') -> ['three' => 55] |
23
|
|
|
* getFromArrayByKey('four.five', $data) -> throws OutOfBoundsException |
24
|
|
|
* ``` |
25
|
|
|
* |
26
|
|
|
* @param string $key |
27
|
|
|
* @param array $data |
28
|
|
|
* @param string $delimiter |
29
|
|
|
* @return mixed|null |
30
|
|
|
*/ |
31
|
|
|
public static function getFromArrayByKey(string $key, array $data, string $delimiter = '.') |
32
|
|
|
{ |
33
|
|
|
$keys = explode($delimiter, $key); |
34
|
|
|
try { |
35
|
|
|
return self::getArrayValueByKeys($keys, $data); |
36
|
|
|
} catch (\OutOfBoundsException $e) { |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Look for a deep value in a data array. |
43
|
|
|
* |
44
|
|
|
* Given $data = ['one' => ['two' => ['three' => 55]], 'four' => []]; |
45
|
|
|
* |
46
|
|
|
* ```php |
47
|
|
|
* getArrayValueByKeys(['one','two','three'], $data) will return 55 |
48
|
|
|
* getArrayValueByKeys(['four','five'], $data) will throw OutOfBoundsException |
49
|
|
|
* ``` |
50
|
|
|
* |
51
|
|
|
* @param mixed $keys |
52
|
|
|
* @param mixed $data passed by reference |
53
|
|
|
* @return mixed |
54
|
|
|
* @throws OutOfBoundsException if the key does not exist in data |
55
|
|
|
*/ |
56
|
|
|
public static function getArrayValueByKeys(array $keys, &$data) |
57
|
|
|
{ |
58
|
|
|
$key = array_shift($keys); |
59
|
|
|
if (!is_array($data) || empty($key)) { |
60
|
|
|
return $data; |
61
|
|
|
} |
62
|
|
|
if (array_key_exists($key, $data) === false) { |
63
|
|
|
throw new OutOfBoundsException("Key [" . join('.', $keys) . ".$key] not set in " . print_r($data, 1)); |
64
|
|
|
} |
65
|
|
|
if (empty($keys)) { |
66
|
|
|
return $data[$key]; |
67
|
|
|
} else { |
68
|
|
|
return self::getArrayValueByKeys($keys, $data[$key]); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function getRequestHeadersFromServerArray(array $server_array) |
73
|
|
|
{ |
74
|
|
|
$out = []; |
75
|
|
|
foreach ($server_array as $name => $value) { |
76
|
|
|
if (substr($name, 0, 5) == "HTTP_") { |
77
|
|
|
$name = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($name, 5))))); |
78
|
|
|
$out[$name] = $value; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
if (array_key_exists("CONTENT_TYPE", $server_array)) { |
82
|
|
|
$out["Content-Type"] = $server_array['CONTENT_TYPE']; |
83
|
|
|
} |
84
|
|
|
if (array_key_exists("CONTENT_LENGTH", $server_array)) { |
85
|
|
|
$out["Content-Length"] = $server_array['CONTENT_LENGTH']; |
86
|
|
|
} |
87
|
|
|
return $out; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|