1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace KEINOS\MSTDN_TOOLS\Parser; |
6
|
|
|
|
7
|
|
|
class ParserStaticMethods |
8
|
|
|
{ |
9
|
|
|
public static function convertEOL(string $string, string $to = "\n"): string |
10
|
|
|
{ |
11
|
|
|
return strtr($string, array_fill_keys(array("\r\n", "\r", "\n"), $to)); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* extractDataFromString |
16
|
|
|
* |
17
|
|
|
* @param string $string |
18
|
|
|
* @return bool|string |
19
|
|
|
*/ |
20
|
|
|
public static function extractDataFromString(string $string) |
21
|
|
|
{ |
22
|
|
|
$result = self::trimEOL(self::getStringAfter('data: ', $string)); |
23
|
|
|
|
24
|
|
|
return ($result === trim($string)) ? false : $result; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function getStringAfter(string $needle, string $haystack): string |
28
|
|
|
{ |
29
|
|
|
$pos_needle = strpos($haystack, $needle); |
30
|
|
|
|
31
|
|
|
if (false === $pos_needle) { |
32
|
|
|
return $haystack; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return substr($haystack, $pos_needle + strlen($needle)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function isBlank(string $string): bool |
39
|
|
|
{ |
40
|
|
|
return empty(trim($string)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function isByteLenOfPayload(string $string): bool |
44
|
|
|
{ |
45
|
|
|
// data length string given is greater than FFF + CR + LF = 5 bytes |
46
|
|
|
$len_min = 5; |
47
|
|
|
|
48
|
|
|
if ($len_min < strlen($string)) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$string = trim($string); |
53
|
|
|
|
54
|
|
|
if (! ctype_xdigit($string)) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function isDataBeginPayload(string $haystack): bool |
62
|
|
|
{ |
63
|
|
|
$needle = 'data: {'; |
64
|
|
|
|
65
|
|
|
return $needle === substr(trim($haystack), 0, strlen($needle)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function isDataEndPayload(string $haystack): bool |
69
|
|
|
{ |
70
|
|
|
$needle = '}'; |
71
|
|
|
|
72
|
|
|
return $needle === substr(trim($haystack), strlen($needle) * -1); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function isDataTootId(string $string): bool |
76
|
|
|
{ |
77
|
|
|
return is_numeric(trim(str_replace('data:', '', $string))); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function isEvent(string $haystack): bool |
81
|
|
|
{ |
82
|
|
|
$needle = 'event:'; |
83
|
|
|
|
84
|
|
|
return $needle === substr(trim($haystack), 0, strlen($needle)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function isEventDelete(string $string): bool |
88
|
|
|
{ |
89
|
|
|
return 'event: delete' === trim($string); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function isEventUpdate(string $string): bool |
93
|
|
|
{ |
94
|
|
|
return 'event: update' === trim($string); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public static function isThump(string $string): bool |
98
|
|
|
{ |
99
|
|
|
return ':thump' === trim($string); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public static function trimEOL(string $string): string |
103
|
|
|
{ |
104
|
|
|
$string = self::convertEOL($string); |
105
|
|
|
|
106
|
|
|
return rtrim($string, "\n"); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|