-
Notifications
You must be signed in to change notification settings - Fork 310
/
Utils.php
129 lines (108 loc) · 3.3 KB
/
Utils.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
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
<?php
namespace YouTube\Utils;
class Utils
{
/**
* Extract youtube video_id from any piece of text
* @param string $str
* @return string|null
*/
public static function extractVideoId(string $str): ?string
{
if (strlen($str) === 11) {
return $str;
}
if (preg_match('/(?:\/|%3D|v=|vi=)([a-z0-9_-]{11})(?:[%#?&\/]|$)/ui', $str, $matches)) {
return $matches[1];
}
return null;
}
/**
* Will parse either channel ID or username from custom URL.
* Will not parse legacy usernames as those cannot be queried via YouTube API
* https://support.google.com/youtube/answer/6180214?hl=en
* @param string $url
* @return string|null
*/
public static function extractChannel(string $url): ?string
{
if (strpos($url, 'UC') === 0 && strlen($url) == 24) {
return $url;
}
if (preg_match('/channel\/(UC[\w-]{22})/', $url, $matches)) {
return $matches[1];
}
if (preg_match('/\/c\/(\w+)/i', $url, $matches)) {
return $matches[1];
}
return null;
}
/**
* @param array $array
* @param string $key
* @param mixed $default
* @return mixed|null
*/
public static function arrayGet(array $array, string $key, $default = null)
{
foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
$array = $default;
break;
}
}
return $array;
}
public static function arrayFilterReset(array $array, callable $callback): array
{
return array_values(array_filter($array, $callback));
}
public static function jsonStringOrFail(string $json): array
{
return json_decode($json, true, 512, JSON_THROW_ON_ERROR);
}
// str_contains not available in early PHP
public static function stringContains(string $haystack, string $needle): bool
{
return strpos($haystack, $needle) !== false;
}
/**
* @param string $string
* @return array
*/
public static function parseQueryString(string $string): array
{
$result = [];
parse_str($string, $result);
return $result;
}
public static function relativeToAbsoluteUrl(string $url, string $domain): string
{
$scheme = parse_url($url, PHP_URL_SCHEME);
$scheme = $scheme ? $scheme : 'http';
// relative protocol?
if (strpos($url, '//') === 0) {
return $scheme . '://' . substr($url, 2);
} elseif (strpos($url, '/') === 0) {
// relative path?
return $domain . $url;
}
return $url;
}
public static function getInputValueByName(string $html, string $name): ?string
{
if (preg_match("/name=(['\"]){$name}\\1[^>]+value=(['\"])(.*?)\\2/is", $html, $matches)) {
return $matches[3];
}
return null;
}
public static function sendJson(array $data, int $status = 200): void
{
http_response_code($status);
header('Content-Type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);
exit;
}
}