Passed
Push — master ( cceb32...6bc369 )
by Scrutinizer
01:52 queued 39s
created

ParserStaticMethods::isDataBeginPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KEINOS\MSTDN_TOOLS;
6
7
class ParserStaticMethods
8
{
9
    public static function extractDataFromString(string $string): string
10
    {
11
        return trim(self::getStringAfter('data: ', trim($string)));
12
    }
13
14
    public static function getStringAfter(string $needle, string $haystack): string
15
    {
16
        $pos_needle = strpos($haystack, $needle);
17
18
        if (false === $pos_needle) {
19
            return $haystack;
20
        }
21
        return substr($haystack, $pos_needle + strlen($needle));
22
    }
23
24
    public static function isBlank(string $string): bool
25
    {
26
        return empty(trim($string));
27
    }
28
29
    public static function isByteLenOfPayload(string $string): bool
30
    {
31
        $len_min = 5; // data length string given is max FFF + CR + LF = 5 bytes
32
33
        if ($len_min < strlen($string)) {
34
            return false;
35
        }
36
37
        $string = trim($string);
38
39
        if (! ctype_xdigit($string)) {
40
            return false;
41
        }
42
43
        return true;
44
    }
45
46
    public static function isData(string $haystack): bool
47
    {
48
        $needle = 'data: ';
49
        return $needle === substr(trim($haystack), 0, strlen($needle));
50
    }
51
52
    public static function isDataBeginPayload(string $haystack): bool
53
    {
54
        $needle = 'data: {';
55
        return $needle === substr(trim($haystack), 0, strlen($needle));
56
    }
57
58
    public static function isDataEndPayload(string $string): bool
59
    {
60
        $key = '}';
61
        return $key === substr(trim($string), strlen($key) * -1);
62
    }
63
64
    public static function isDataTootId(string $string): bool
65
    {
66
        return is_numeric(trim(str_replace('data:', '', $string)));
67
    }
68
69
    public static function isEvent(string $haystack): bool
70
    {
71
        $needle = 'event:';
72
        return $needle === substr(trim($haystack), 0, strlen($needle));
73
    }
74
75
    public static function isEventDelete(string $string): bool
76
    {
77
        return 'event: delete' === trim($string);
78
    }
79
80
    public static function isEventUpdate(string $string): bool
81
    {
82
        return 'event: update' === trim($string);
83
    }
84
85
    public static function isThump(string $string): bool
86
    {
87
        return ':thump' === trim($string);
88
    }
89
90
    public static function reEncodeJsonPretty(string $string): string
91
    {
92
        $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
93
94
        return json_encode(json_decode($string), $options) ?: '';
95
    }
96
}
97