-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.php
114 lines (97 loc) · 3.72 KB
/
generate.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
const DOTTY_TYPES = [
[
'name' => 'bool',
'check' => 'filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null',
'cast' => 'filter_var($value, FILTER_VALIDATE_BOOLEAN)',
],
['name' => 'int', 'check' => 'filter_var($value, FILTER_VALIDATE_INT)', 'cast' => '(int)$value'],
['name' => 'float', 'check' => 'filter_var($value, FILTER_VALIDATE_FLOAT)', 'cast' => '(float)$value'],
['name' => 'string', 'check' => 'is_string($value)', 'cast' => '$value'],
['name' => 'array', 'check' => 'is_array($value)', 'cast' => '$value'],
];
const DOTTY_HEADER = <<<'EOF'
<?php
declare(strict_types=1);
namespace SnapServ\Dotty\Internal;
use Illuminate\Support\Arr;
use SnapServ\Dotty\DottyException;
trait DottyTypes
{
EOF;
const DOTTY_FOOTER = '}';
const DOTTY_TYPE_HELPER_FN = <<<'EOF'
/**
* @param array-key $key
* @param {type}|\Closure():{type}|null $default
* @param bool $required
* @return {type}|null
* @psalm-template TDefault as {type}|\Closure():{type}|null
* @psalm-template TRequired as bool
* @psalm-param TDefault $default
* @psalm-param TRequired $required
* @psalm-return (TRequired is true ? {type} : (TDefault is null ? {type}|null : {type}))
* @throws DottyException
*/
public function {type}(string|int $key, {type}|\Closure|null $default = null, bool $required = true): ?{type}
{
return $required
? $this->require{typeTitle}($key, $default)
: $this->get{typeTitle}($key, $default);
}
EOF;
const DOTTY_TYPE_GET_FN = <<<'EOF'
/**
* @param array-key $key
* @param {type}|\Closure():{type}|null $default
* @return {type}|null
* @psalm-template TDefault as {type}|\Closure():{type}|null
* @psalm-param TDefault $default
* @psalm-return (TDefault is null ? {type}|null : {type})
*/
public function get{typeTitle}(string|int $key, {type}|\Closure|null $default = null): ?{type}
{
/** @var mixed $value */
$value = Arr::get($this->data, $key);
/** @var {type}|null */
return {check}
? {cast}
: value($default);
}
EOF;
const DOTTY_TYPE_REQUIRE_FN = <<<'EOF'
/**
* @param array-key $key
* @param {type}|\Closure():{type}|null $default
* @return {type}
* @throws DottyException
*/
public function require{typeTitle}(string|int $key, {type}|\Closure|null $default = null): {type}
{
return $this->get{typeTitle}($key, $default) ?? throw DottyException::missingKey($this, $key);
}
EOF;
function processTemplate(string $template, ?array $type = null, int $indentDepth = 0): string
{
if ($type) {
$template = str_replace('{type}', $type['name'], $template);
$template = str_replace('{typeTitle}', ucwords($type['name']), $template);
$template = str_replace('{check}', $type['check'], $template);
$template = str_replace('{cast}', $type['cast'], $template);
}
$indent = str_repeat(' ', $indentDepth);
$template = $indent . str_replace("\n", "\n{$indent}", $template);
return preg_replace('/[ \t]*$/m', '', $template);
}
$output = processTemplate(DOTTY_HEADER) . "\n";
foreach (DOTTY_TYPES as $key => $type) {
$output .= processTemplate(DOTTY_TYPE_HELPER_FN, $type, 4) . "\n\n";
$output .= processTemplate(DOTTY_TYPE_GET_FN, $type, 4) . "\n\n";
$output .= processTemplate(DOTTY_TYPE_REQUIRE_FN, $type, 4) . "\n";
if ($key !== array_key_last(DOTTY_TYPES)) {
$output .= "\n";
}
}
$output .= processTemplate(DOTTY_FOOTER) . "\n";
file_put_contents('src/Internal/DottyTypes.php', $output);