mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-11 06:28:40 +00:00
udpate cli request
This commit is contained in:
parent
7ed21a1ec0
commit
288e9d62d6
|
|
@ -15,9 +15,12 @@ declare(strict_types=1);
|
||||||
namespace phpOMS\Message\Console;
|
namespace phpOMS\Message\Console;
|
||||||
|
|
||||||
use phpOMS\Localization\Localization;
|
use phpOMS\Localization\Localization;
|
||||||
|
use phpOMS\Message\Http\RequestMethod;
|
||||||
use phpOMS\Message\RequestAbstract;
|
use phpOMS\Message\RequestAbstract;
|
||||||
|
use phpOMS\Router\RouteVerb;
|
||||||
use phpOMS\Uri\Argument;
|
use phpOMS\Uri\Argument;
|
||||||
use phpOMS\Uri\UriInterface;
|
use phpOMS\Uri\UriInterface;
|
||||||
|
use phpOMS\Utils\ArrayUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request class.
|
* Request class.
|
||||||
|
|
@ -45,7 +48,7 @@ final class ConsoleRequest extends RequestAbstract
|
||||||
* @var string
|
* @var string
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
protected string $method;
|
protected string $method = RequestMethod::GET;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OS type.
|
* OS type.
|
||||||
|
|
@ -72,6 +75,75 @@ final class ConsoleRequest extends RequestAbstract
|
||||||
$this->init();
|
$this->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get data.
|
||||||
|
*
|
||||||
|
* @param string $key Data key
|
||||||
|
* @param string $type Return type
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public function getData(string $key = null, string $type = null) : mixed
|
||||||
|
{
|
||||||
|
if ($key === null) {
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$key = '-' . \mb_strtolower($key);
|
||||||
|
|
||||||
|
if ($type === null) {
|
||||||
|
return ArrayUtils::getArg($key, $this->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($type) {
|
||||||
|
case 'int':
|
||||||
|
return (int) ArrayUtils::getArg($key, $this->data);
|
||||||
|
case 'string':
|
||||||
|
return (string) ArrayUtils::getArg($key, $this->data);
|
||||||
|
case 'float':
|
||||||
|
return (float) ArrayUtils::getArg($key, $this->data);
|
||||||
|
case 'bool':
|
||||||
|
return (bool) ArrayUtils::getArg($key, $this->data);
|
||||||
|
default:
|
||||||
|
return ArrayUtils::getArg($key, $this->data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set request data.
|
||||||
|
*
|
||||||
|
* @param string $key Data key
|
||||||
|
* @param mixed $value Value
|
||||||
|
* @param bool $overwrite Overwrite data
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public function setData(string $key, mixed $value, bool $overwrite = false) : bool
|
||||||
|
{
|
||||||
|
$key = '-' . \mb_strtolower($key);
|
||||||
|
|
||||||
|
$pos = -1;
|
||||||
|
if ($overwrite || ($pos = ArrayUtils::hasArg($key, $this->data)) !== -1) {
|
||||||
|
if ($pos === -1) {
|
||||||
|
$this->data[] = $key;
|
||||||
|
$this->data[] = $value;
|
||||||
|
} else {
|
||||||
|
$this->data[$pos] = $key;
|
||||||
|
$this->data[$pos + 1] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->uri->setQuery(\implode(' ', $this->data));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init request.
|
* Init request.
|
||||||
*
|
*
|
||||||
|
|
@ -84,6 +156,7 @@ final class ConsoleRequest extends RequestAbstract
|
||||||
private function init() : void
|
private function init() : void
|
||||||
{
|
{
|
||||||
$this->header->l11n->setLanguage('en');
|
$this->header->l11n->setLanguage('en');
|
||||||
|
$this->data = $this->uri->getQueryArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -188,4 +261,27 @@ final class ConsoleRequest extends RequestAbstract
|
||||||
{
|
{
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get route verb.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public function getRouteVerb() : int
|
||||||
|
{
|
||||||
|
switch ($this->getMethod()) {
|
||||||
|
case RequestMethod::GET:
|
||||||
|
return RouteVerb::GET;
|
||||||
|
case RequestMethod::PUT:
|
||||||
|
return RouteVerb::PUT;
|
||||||
|
case RequestMethod::POST:
|
||||||
|
return RouteVerb::SET;
|
||||||
|
case RequestMethod::DELETE:
|
||||||
|
return RouteVerb::DELETE;
|
||||||
|
default:
|
||||||
|
throw new \Exception();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,7 @@ final class ConsoleResponse extends ResponseAbstract implements RenderableInterf
|
||||||
{
|
{
|
||||||
$render = '';
|
$render = '';
|
||||||
|
|
||||||
foreach ($this->response as $key => $response) {
|
foreach ($this->response as $response) {
|
||||||
$render .= StringUtils::stringify($response);
|
$render .= StringUtils::stringify($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -147,7 +147,7 @@ final class ConsoleResponse extends ResponseAbstract implements RenderableInterf
|
||||||
{
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
foreach ($this->response as $key => $response) {
|
foreach ($this->response as $response) {
|
||||||
if ($response instanceof View) {
|
if ($response instanceof View) {
|
||||||
$result[] = $response->toArray();
|
$result[] = $response->toArray();
|
||||||
} elseif (\is_array($response) || \is_scalar($response)) {
|
} elseif (\is_array($response) || \is_scalar($response)) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user