-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmas.php
More file actions
83 lines (71 loc) · 2.55 KB
/
Copy pathmas.php
File metadata and controls
83 lines (71 loc) · 2.55 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
<?php
/**
* Model Address Standard (MAS) — decode/encode MAS URIs.
*
* Usage:
* $params = mas_decode("https://api.example.com#m=gpt-4o&k=sk-xyz");
* $frag = mas_encode(["m" => "gpt-4o"]);
*/
if (!function_exists('mas_decode')) {
/**
* Extract MAS parameters (m, k) from an HTTP/HTTPS URI.
*
* @param string $uri A valid MAS address.
* @return array{0: string, 1?: string} [m, k?]
* @throws \InvalidArgumentException If the fragment is missing,
* m is absent, or m is empty.
*/
function mas_decode(string $uri): array {
$hash = strpos($uri, '#');
if ($hash === false) {
throw new \InvalidArgumentException('MAS: fragment is required');
}
$fragment = substr($uri, $hash + 1);
if ($fragment === '' || $fragment === false) {
throw new \InvalidArgumentException('MAS: fragment is required');
}
$m = null;
$k = null;
foreach (explode('&', $fragment) as $part) {
$eq = strpos($part, '=');
$key = $eq !== false ? substr($part, 0, $eq) : $part;
$val = $eq !== false ? substr($part, $eq + 1) : '';
$key = rawurldecode($key);
$val = rawurldecode($val);
if ($key === 'm') {
if ($val === '') {
throw new \InvalidArgumentException('MAS: m must not be empty');
}
$m = $val;
} elseif ($key === 'k' && $val !== '') {
$k = $val;
}
}
if ($m === null) {
throw new \InvalidArgumentException('MAS: m is required');
}
return $k !== null ? [$m, $k] : [$m];
}
}
if (!function_exists('mas_encode')) {
/**
* Build a MAS fragment string from parameters.
*
* @param array{0: string, 1?: string}|array{m: string, k?: string} $params
* Must contain a non-empty "m" key or first element.
* @return string URI fragment of the form "#m=...&k=...".
* @throws \InvalidArgumentException If m is missing or empty.
*/
function mas_encode(array $params): string {
$m = $params['m'] ?? $params[0] ?? null;
if ($m === null || $m === '') {
throw new \InvalidArgumentException('MAS: m is required and must be non-empty');
}
$parts = ['m=' . rawurlencode($m)];
$k = $params['k'] ?? $params[1] ?? null;
if ($k !== null && $k !== '') {
$parts[] = 'k=' . rawurlencode($k);
}
return '#' . implode('&', $parts);
}
}