rosavox/lib/alveolata/url/functions.php
2025-05-23 07:33:29 +00:00

130 lines
2.2 KiB
PHP

<?php
namespace alveolata\url;
/**
* @todo prüfen ob PHP-interne Funktion verwendbar ist
* @param ?array $data {(null|map<string,(null|string)>)}
*/
function form_encode(
?array $data
) : ?string
{
if ($data === null) {
return $data;
}
else {
$parts = [];
foreach ($data as $key => $value) {
\array_push(
$parts,
\sprintf(
'%s%s',
$key,
(
($value === null)
? ''
: ('=' . $value)
)
)
);
}
return \implode('&', $parts);
}
}
/**
* @todo prüfen ob PHP-interne Funktion verwendbar ist
* @return array {(null|map<string,(null|string)>)}
*/
function form_decode(
?string $data_encoded
) : array
{
if ($data_encoded === null) {
return null;
}
else {
throw (new \Exception('not implemented'));
}
}
/**
* @param array $parts {
* record<
* ?scheme:(null|string),
* ?host:(null|string),
* ?path:(null|list<string>),
* ?query:(null|string),
* ?hash:(null|string),
* >
* }
*/
function encode(
array $parts
) : string
{
$parts = \array_merge(
[
'scheme' => null,
'host' => null,
'path' => null,
'query' => null,
'hash' => null,
],
$parts
);
if (
($parts['scheme'] === null)
&&
($parts['host'] === null)
&&
($parts['path'] === null)
&&
($parts['query'] === null)
&&
($parts['hash'] === null)
) {
throw (new \Exception('url.encode: at least one part has to be defined'));
}
else {
if (
($parts['scheme'] !== null)
&&
($parts['host'] === null)
&&
($parts['path'] === null)
&&
($parts['query'] === null)
&&
($parts['hash'] === null)
) {
throw (new \Exception('url.encode: a sole scheme is insufficient to form a URL'));
}
else {
return \sprintf(
'%s%s%s%s%s',
(($parts['scheme'] === null) ? '' : ($parts['scheme'] . '://')),
(($parts['host'] === null) ? '' : ($parts['host'])),
(($parts['path'] === null) ? '' : ('/' . \implode('/', $parts['path']))),
(($parts['query'] === null) ? '' : ('?' . $parts['query'])),
(($parts['hash'] === null) ? '' : ('#' . $parts['hash']))
);
}
}
}
/**
*/
function decode(
string $url_encoded
) : array
{
throw (new \Exception('not implemented'));
}
?>