78 lines
2.6 KiB
PHP
78 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Picea\Method;
|
|
|
|
use Picea\Extension\Extension,
|
|
Picea\Extension\ExtensionTrait;
|
|
|
|
use Picea\Compiler\Context;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
class Request implements Extension {
|
|
use ExtensionTrait;
|
|
|
|
public array $tokens;
|
|
|
|
public string $token;
|
|
|
|
public ServerRequestInterface $request;
|
|
|
|
public function __construct(ServerRequestInterface $request, Context $context) {
|
|
$this->request = $request;
|
|
$this->register($context);
|
|
}
|
|
|
|
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string { }
|
|
|
|
public function register(Context $context) : void
|
|
{
|
|
$context->pushFunction("cookie", [ $this, 'cookie' ]);
|
|
$context->pushFunction("get", [ $this, 'get' ]);
|
|
$context->pushFunction("post", [ $this, 'post' ]);
|
|
$context->pushFunction("request", [ $this, 'request' ]);
|
|
$context->pushFunction("server", [ $this, 'server' ]);
|
|
}
|
|
|
|
public function cookie(? string $variableName = null, $default = null)
|
|
{
|
|
return $variableName === null ? $this->request->getCookieParams() : static::arrayGet($this->request->getCookieParams(), $variableName) ?? $default;
|
|
}
|
|
|
|
public function get(? string $variableName = null, $default = null)
|
|
{
|
|
return $variableName === null ? $this->request->getQueryParams() : static::arrayGet($this->request->getQueryParams(), $variableName) ?? $default;
|
|
}
|
|
|
|
public function post(? string $variableName = null, $default = null)
|
|
{
|
|
return $variableName === null ? $this->request->getParsedBody() : static::arrayGet($this->request->getParsedBody(), $variableName) ?? $default;
|
|
}
|
|
|
|
public function request(? string $variableName = null, $default = null)
|
|
{
|
|
return $variableName === null ? array_merge(get(null), post(null)) : $this->post($variableName) ?? $this->get($variableName) ?? $default;
|
|
}
|
|
|
|
public function server(? string $variableName = null, $default = null)
|
|
{
|
|
return $variableName === null ? $this->request->getServerParams() : static::arrayGet($this->request->getServerParams(), $variableName) ?? $default;
|
|
}
|
|
|
|
public static function arrayGet(array $array, string $path, string $delimiter = '.')
|
|
{
|
|
$pathArr = explode($delimiter, $path);
|
|
|
|
if ( isset($array[$pathArr[0]]) ) {
|
|
if ( isset($pathArr[1]) ) {
|
|
return static::arrayGet($array[array_shift($pathArr)], implode($delimiter, $pathArr));
|
|
}
|
|
else {
|
|
return $array[$pathArr[0]];
|
|
}
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
}
|
|
} |