Passed
Pull Request — master (#75)
by Arman
05:05 queued 02:36
created

transform()   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
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
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 2.8.0
13
 */
14
use Quantum\Libraries\Transformer\TransformerInterface;
15
use Quantum\Libraries\Transformer\TransformerManager;
16
use Quantum\Exceptions\StopExecutionException;
17
use Quantum\Libraries\Asset\AssetManager;
18
use Quantum\Exceptions\AppException;
19
use Quantum\Libraries\Csrf\Csrf;
20
21
/**
22
 * Generates the CSRF token
23
 * @return string|null
24
 * @throws \Quantum\Exceptions\AppException
25
 * @throws \Quantum\Exceptions\DatabaseException
26
 * @throws \Quantum\Exceptions\SessionException
27
 */
28
function csrf_token(): ?string
29
{
30
    $appKey = env('APP_KEY');
31
32
    if (!$appKey) {
33
        throw AppException::missingAppKey();
34
    }
35
36
    return Csrf::generateToken(session(), $appKey);
37
}
38
39
/**
40
 * _message
41
 * @param string $subject
42
 * @param string|array $params
43
 * @return string
44
 */
45
function _message(string $subject, $params): string
46
{
47
    if (is_array($params)) {
48
        return preg_replace_callback('/{%\d+}/', function () use (&$params) {
49
            return array_shift($params);
50
        }, $subject);
51
    } else {
52
        return preg_replace('/{%\d+}/', $params, $subject);
53
    }
54
}
55
56
/**
57
 * Validates base64 string
58
 * @param string $string
59
 * @return boolean
60
 */
61
function valid_base64(string $string): bool
62
{
63
    $decoded = base64_decode($string, true);
64
65
    // Check if there is no invalid character in string
66
    if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) {
67
        return false;
68
    }
69
70
    // Decode the string in strict mode and send the response
71
    if (!base64_decode($string, true)) {
72
        return false;
73
    }
74
75
    // Encode and compare it to original one
76
    if (base64_encode($decoded) != $string) {
77
        return false;
78
    }
79
80
    return true;
81
}
82
83
/**
84
 * Gets directory classes
85
 * @param string $path
86
 * @return array
87
 */
88
function get_directory_classes(string $path): array
89
{
90
    $class_names = [];
91
92
    if (is_dir($path)) {
93
        $allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
94
        $phpFiles = new RegexIterator($allFiles, '/\.php$/');
95
96
        foreach ($phpFiles as $file) {
97
            $class = pathinfo($file->getFilename());
98
            array_push($class_names, $class['filename']);
99
        }
100
    }
101
102
    return $class_names;
103
}
104
105
/**
106
 * Gets the caller class
107
 * @param integer $index
108
 * @return string|null
109
 */
110
function get_caller_class(int $index = 2): ?string
111
{
112
    $caller = debug_backtrace();
113
    $caller = $caller[$index];
114
115
    return $caller['class'] ?? null;
116
}
117
118
/**
119
 * Gets the caller function
120
 * @param integer $index
121
 * @return string|null
122
 */
123
function get_caller_function(int $index = 2): ?string
124
{
125
    $caller = debug_backtrace();
126
    $caller = $caller[$index];
127
128
    return $caller['function'] ?? null;
129
}
130
131
/**
132
 *
133
 * @throws \Quantum\Exceptions\StopExecutionException
134
 */
135
136
/**
137
 * Stops the execution
138
 * @param \Closure|null $closure
139
 * @throws \Quantum\Exceptions\StopExecutionException
140
 */
141
function stop(Closure $closure = null)
142
{
143
    if ($closure) {
144
        $closure();
145
    }
146
147
    throw StopExecutionException::executionTerminated();
148
}
149
150
/**
151
 * Generates random number sequence
152
 * @param int $length
153
 * @return int
154
 */
155
function random_number(int $length = 10): int
156
{
157
    $randomString = '';
158
    for ($i = 0; $i < $length; $i++) {
159
        $randomString .= rand(0, 9);
160
    }
161
    return (int) $randomString;
162
}
163
164
/**
165
 * Slugify the string
166
 * @param string $text
167
 * @return string
168
 */
169
function slugify(string $text): string
170
{
171
    $text = trim($text, ' ');
172
    $text = preg_replace('/[^\p{L}\p{N}]/u', ' ', $text);
173
    $text = preg_replace('/\s+/', '-', $text);
174
    $text = trim($text, '-');
175
    $text = mb_strtolower($text);
176
177
    if (empty($text)) {
178
        return 'n-a';
179
    }
180
    return $text;
181
}
182
183
/**
184
 * Dumps the assets
185
 * @param string $type
186
 * @throws \Quantum\Exceptions\AssetException
187
 */
188
function assets(string $type)
189
{
190
    $assetTypes = [
191
        'css' => 1,
192
        'js' => 2
193
    ];
194
195
    AssetManager::getInstance()->dump($assetTypes[$type]);
196
}
197
198
/**
199
 * Checks if the entity is closure
200
 * @param mixed $entity
201
 * @return bool
202
 */
203
function is_closure($entity): bool
204
{
205
    return $entity instanceof \Closure;
206
}
207
208
/**
209
 * Transforms the data by given transformer signature
210
 * @param array $data
211
 * @param TransformerInterface $transformer
212
 * @return array
213
 */
214
function transform(array $data, TransformerInterface $transformer): array
215
{
216
    return TransformerManager::transform($data, $transformer);
217
}
218
219