Passed
Push — master ( a0ae72...f853f9 )
by Shahrad
02:06
created

Common::urlDecode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 19
rs 9.9332
c 1
b 0
f 1
1
<?php
2
3
namespace TelegramBot\Util;
4
5
/**
6
 * Class Common
7
 *
8
 * @link    https://github.com/telegram-bot-php/core
9
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
10
 * @license https://github.com/telegram-bot-php/core/blob/master/LICENSE (MIT License)
11
 */
12
class Common
13
{
14
15
    /**
16
     * Validate the given string is JSON or not
17
     *
18
     * @param ?string $string
19
     * @return bool
20
     */
21
    public static function isJson(?string $string): bool
22
    {
23
        if (!is_string($string)) return false;
24
        json_decode($string);
25
        return json_last_error() === JSON_ERROR_NONE;
26
    }
27
28
    /**
29
     * Debug mode
30
     *
31
     * @param ?int $admin_id Fill this or use setAdmin()
32
     * @return void
33
     */
34
    public static function setDebugMode(?int $admin_id = null): void
35
    {
36
        error_reporting(E_ALL);
37
        ini_set('display_errors', 1);
38
        defined('DEBUG_MODE') or define('DEBUG_MODE', true);
39
        if ($admin_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $admin_id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
40
            defined('TG_ADMIN_ID') or define('TG_ADMIN_ID', $admin_id);
41
        }
42
    }
43
44
    /**
45
     * Arrest with exception
46
     *
47
     * @param mixed $class The class
48
     * @param string $callback The method
49
     * @param mixed ...$args The arguments to pass to the callback
50
     * @return mixed
51
     */
52
    public static function arrest(mixed $class, string $callback, ...$args): mixed
53
    {
54
        try {
55
            return call_user_func_array([$class, $callback], $args);
56
        } catch (\Throwable|\TypeError|\Exception $e) {
57
            if (defined('DEBUG_MODE') && DEBUG_MODE) {
58
                echo '<b>TelegramError:</b> ' . $e->getMessage() . PHP_EOL;
59
            }
60
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
61
        }
62
    }
63
64
    /**
65
     * Check string is a url encoded string or not
66
     *
67
     * @param string $string
68
     * @return bool
69
     */
70
    public static function isUrlEncode(string $string): bool
71
    {
72
        return preg_match('/%[0-9A-F]{2}/i', $string);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('/%[0-9A-F]{2}/i', $string) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
73
    }
74
75
    /**
76
     * Convert url encoded string to array
77
     *
78
     * @param string $string
79
     * @return array
80
     */
81
    public static function urlDecode(string $string): array
82
    {
83
        $raw_data = explode('&', urldecode($string));
84
        $data = [];
85
86
        foreach ($raw_data as $row) {
87
            [$key, $value] = explode('=', $row);
88
89
            if (self::isUrlEncode($value)) {
90
                $value = urldecode($value);
91
                if (self::isJson($value)) {
92
                    $value = json_decode($value, true);
93
                }
94
            }
95
96
            $data[$key] = $value;
97
        }
98
99
        return $data;
100
    }
101
102
}