Passed
Push — master ( 8906b4...4a87c7 )
by Scrutinizer
01:55
created

ParserStaticMethods::reEncodeJsonPretty()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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