forked from b-viguier/php-emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.php
More file actions
87 lines (71 loc) · 2.33 KB
/
utils.php
File metadata and controls
87 lines (71 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
const 🍎 = '🍎';
const 🍐 = '🍐';
const 🍊 = '🍊';
const 🍋 = '🍋';
const 🍌 = '🍌';
const 😀 = '😀';
const 😎 = '😎';
const 🥶 = '🥶';
const 🤡 = '🤡';
const 🤠 = '🤠';
const 🐶 = '🐶';
const 🐭 = '🐭';
const 🐰 = '🐰';
const 🦊 = '🦊';
const 🐯 = '🐯';
const ❌ = '❌';
const ✅ = '✅';
$linesOfCode = file(__DIR__.DIRECTORY_SEPARATOR.'README.php');
function dump(callable $fn, string $title = null, string $url = null): void
{
global $linesOfCode;
$reflection = new ReflectionFunction($fn);
$start = $reflection->getStartLine();
$length = $reflection->getEndLine() - $start + ($title === null ? 0 : 1);
$lines = array_map(
fn(string $line) => substr($line, 4), // Remove indentation (4 spaces)
array_slice($linesOfCode, $start - 1, $length)
);
$lines[0] = substr($lines[0], 8); // Remove "fn() => "
$code = rtrim(trim(join('', $lines)), ',');
if ($title === null) {
$funcName = strstr($code, '(', true);
$title = '`'.$funcName.'`';
} else {
$funcName = substr($title, $start = 1 + strpos($title, '`'), strpos($title, '`', $start) - $start);
}
if(null === $url) {
$funcUrl = str_replace('_', '-', $funcName);
$url = "https://www.php.net/manual/en/function.$funcUrl.php";
}
echo "### [$title]($url)\n```php\n";
echo "$code;\n```\n```\n".dumpArray($fn())."\n```\n\n";
}
function dumpArray(array $array): string
{
if ([] === $array) {
return '[]';
}
$isStringButNotAnEmoji = fn($var) => is_string($var) && strlen($var) != 3 && strlen($var) != 4;
$parts = [];
foreach ($array as $elmt) {
if (is_array($elmt)) {
$parts[] = dumpArray($elmt);
} elseif ($isStringButNotAnEmoji($elmt)) {
$parts[] = "'$elmt'";
} elseif ($elmt === null) {
$parts[] = 'null';
} else {
$parts[] = $elmt;
}
}
$keys = array_keys($array);
if ($keys !== range(0, count($keys) - 1)) {
$parts = array_map(fn($k, $v) => $isStringButNotAnEmoji($k) ? "'$k' => $v" : "$k => $v", $keys, $parts);
}
// Assume array homogeneity + no more than 1 sub level
return is_array(reset($array)) ?
"[\n ".join(", \n ", $parts).",\n]"
: '['.join(', ', $parts).']';;
}