Passed
Pull Request — master (#391)
by Arman
03:15
created

is_closure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 3.0.0
13
 */
14
15
use Symfony\Component\VarExporter\Exception\ExceptionInterface;
16
use Symfony\Component\VarExporter\VarExporter;
17
18
/**
19
 * Compose a message
20
 * @param string $subject
21
 * @param string|array $params
22
 * @return string
23
 */
24
function _message(string $subject, $params): string
25
{
26
    if (is_array($params)) {
27
        return preg_replace_callback('/{%\d+}/', function () use (&$params) {
28
            return array_shift($params);
29
        }, $subject);
30
    } else {
31
        return preg_replace('/{%\d+}/', $params, $subject);
32
    }
33
}
34
35
/**
36
 * Validates base64 string
37
 * @param string $string
38
 * @return boolean
39
 */
40
function valid_base64(string $string): bool
41
{
42
    if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) {
43
        return false;
44
    }
45
46
    $decoded = base64_decode($string, true);
47
48
    return $decoded !== false && base64_encode($decoded) === $string;
49
}
50
51
/**
52
 * Generates random number sequence
53
 * @param int $length
54
 * @return int
55
 */
56
function random_number(int $length = 10): int
57
{
58
    $randomString = '';
59
    for ($i = 0; $i < $length; $i++) {
60
        $randomString .= random_int(0, 9);
61
    }
62
    return (int)$randomString;
63
}
64
65
/**
66
 * Slugify the string
67
 * @param string $text
68
 * @return string
69
 */
70
function slugify(string $text): string
71
{
72
    $text = trim($text, ' ');
73
    $text = preg_replace('/[^\p{L}\p{N}]/u', ' ', $text);
74
    $text = preg_replace('/\s+/', '-', $text);
75
    $text = trim($text, '-');
76
    $text = mb_strtolower($text);
77
78
    if ($text === '' || $text === '0') {
79
        return 'n-a';
80
    }
81
82
    return $text;
83
}
84
85
/**
86
 * Checks the app debug mode
87
 * @return bool
88
 */
89
function is_debug_mode(): bool
90
{
91
    return filter_var(config()->get('app.debug'), FILTER_VALIDATE_BOOLEAN);
92
}
93
94
/**
95
 * Gets the caller class
96
 * @param integer $index
97
 * @return string|null
98
 */
99
function get_caller_class(int $index = 2): ?string
100
{
101
    $caller = debug_backtrace();
102
    $caller = $caller[$index];
103
104
    return $caller['class'] ?? null;
105
}
106
107
/**
108
 * Gets the caller function
109
 * @param integer $index
110
 * @return string|null
111
 */
112
function get_caller_function(int $index = 2): ?string
113
{
114
    $trace = debug_backtrace();
115
116
    if (!isset($trace[$index])) {
117
        return null;
118
    }
119
120
    return $trace[$index]['function'];
121
}
122
123
/**
124
 * Exports the variable
125
 * @param $var
126
 * @return string
127
 * @throws ExceptionInterface
128
 */
129
function export($var): string
130
{
131
    return VarExporter::export($var);
132
}