-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArtifactUrlResolver.php
More file actions
69 lines (58 loc) · 1.84 KB
/
ArtifactUrlResolver.php
File metadata and controls
69 lines (58 loc) · 1.84 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
<?php
declare(strict_types=1);
namespace Codewithkyrian\PlatformPackageInstaller;
final class ArtifactUrlResolver
{
/**
* @param array<string, mixed> $defaultVars
* @param array<string, mixed> $overrideVars
*
* @return array<string, string>
*/
public function mergeVars(array $defaultVars, array $overrideVars, string $version): array
{
$vars = array_merge($defaultVars, $overrideVars);
$vars['version'] = $version;
$out = [];
foreach ($vars as $key => $value) {
if (!is_string($key) || $key === '') {
continue;
}
if (is_scalar($value) || $value === null) {
$out[$key] = (string) $value;
continue;
}
$encoded = json_encode($value);
$out[$key] = $encoded === false ? '' : $encoded;
}
return $out;
}
/**
* Apply `{name}` placeholders using provided vars. Unknown placeholders are left intact.
*
* @param array<string, string> $vars
*/
public function applyTemplate(string $template, array $vars): string
{
return (string) preg_replace_callback('/\{([a-zA-Z0-9_-]+)\}/', function (array $m) use ($vars) {
$key = $m[1] ?? '';
if ($key === '' || !array_key_exists($key, $vars)) {
return $m[0];
}
return $vars[$key];
}, $template);
}
/**
* @return list<string> placeholder keys still present in the value
*/
public function unresolvedPlaceholders(string $value): array
{
if (!preg_match_all('/\{([a-zA-Z0-9_-]+)\}/', $value, $matches)) {
return [];
}
/** @var list<string> $keys */
$keys = array_values(array_unique($matches[1] ?? []));
sort($keys);
return $keys;
}
}