- Added a new json.html output

- Fixed a bug where post() was defined twice in request. Added server and request too
This commit is contained in:
Dave M. 2020-03-30 12:20:28 -04:00
parent 404d1f4359
commit dcb22fe086
3 changed files with 38 additions and 8 deletions

View File

@ -16,12 +16,17 @@ class JsonExtension implements Extension {
}
}
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token)
{
$flag = $this->flags;
if ( $token === "json.pretty" ) {
$flag = \JSON_PRETTY_PRINT;
switch ($token) {
case "json.pretty":
$flag = \JSON_PRETTY_PRINT;
break;
case "json.html":
return "<?php echo htmlentities(json_encode($arguments, {$this->flags}), ENT_QUOTES, 'UTF-8') ?>";
}
return "<?php echo json_encode($arguments, $flag) ?>";

View File

@ -162,11 +162,26 @@ class UrlExtension implements Extension {
$search = [];
foreach($matches as $item) {
if ( ! array_key_exists($item[1], $arguments) ) {
throw new \InvalidArgumentException("Argument `{$item[1]}` is missing within required route parameter(s)");
if (strpos($item[1], "=") !== false) {
list($variable, $default) = explode('=', $item[1]);
}
else {
$variable = $item[1];
}
if ( array_key_exists($variable, $arguments) ) {
$value = $arguments[ $item[1] ];
}
else if ( isset($default) ) {
$value = $default;
}
else {
throw new \InvalidArgumentException("Argument `$variable` is missing within required route parameter(s)");
}
$search[ $item[0] ] = $arguments[ $item[1] ];
$search[ $item[0] ] = $value;
unset($arguments[ $item[1] ]);
}

View File

@ -30,7 +30,8 @@ class Request implements Extension {
$context->pushFunction("cookie", [ $this, 'cookie' ]);
$context->pushFunction("get", [ $this, 'get' ]);
$context->pushFunction("post", [ $this, 'post' ]);
$context->pushFunction("post", [ $this, 'post' ]);
$context->pushFunction("request", [ $this, 'request' ]);
$context->pushFunction("server", [ $this, 'server' ]);
}
public function cookie(? string $variableName = null, $default = null)
@ -48,7 +49,16 @@ class Request implements Extension {
return $variableName === null ? $this->request->getParsedBody() : static::arrayGet($this->request->getParsedBody(), $variableName) ?? $default;
}
<<<<<<< Updated upstream
public function server(? string $variableName = null, $default = null)
=======
public function request(? string $variableName = null)
{
return $this->post($variableName) ?? $this->get($variableName);
}
public function server(? string $variableName = null)
>>>>>>> Stashed changes
{
return $variableName === null ? $this->request->getServerParams() : static::arrayGet($this->request->getServerParams(), $variableName) ?? $default;
}