Files
kiri-core/kiri-engine/Abstracts/Input.php
T

112 lines
1.6 KiB
PHP
Raw Normal View History

2020-09-07 15:47:13 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-09-07 15:47:13 +08:00
2021-08-11 01:04:57 +08:00
namespace Kiri\Abstracts;
2020-09-07 15:47:13 +08:00
use Exception;
class Input
{
2020-10-29 18:17:25 +08:00
private array $_argv = [];
2020-09-07 15:47:13 +08:00
2020-10-29 18:17:25 +08:00
private string $_command = '';
2020-09-07 15:47:13 +08:00
/**
* Input constructor.
* @param $argv
2020-09-08 14:28:02 +08:00
* @throws
2020-09-07 15:47:13 +08:00
*/
public function __construct($argv)
{
$this->_argv = $this->resolve($argv);
}
2020-09-07 18:11:36 +08:00
/**
* @return string
*/
2020-12-17 14:09:14 +08:00
public function getCommandName(): string
2020-09-07 18:11:36 +08:00
{
2020-09-08 11:12:41 +08:00
return $this->_command;
2020-09-07 18:11:36 +08:00
}
2020-09-07 15:47:13 +08:00
/**
* @param $key
* @param null $default
2021-08-10 16:40:01 +08:00
* @return mixed
2020-09-07 15:47:13 +08:00
*/
2020-12-17 14:09:14 +08:00
public function get($key, $default = null): mixed
2020-09-07 15:47:13 +08:00
{
return $this->_argv[$key] ?? $default;
}
2021-01-19 18:58:09 +08:00
/**
* @param $key
* @return bool
*/
public function exists($key): bool
{
return isset($this->_argv[$key]);
}
2020-09-07 15:47:13 +08:00
/**
* @param $key
* @param $value
* @return $this
*/
2020-12-17 14:09:14 +08:00
public function set($key, $value): static
2020-09-07 15:47:13 +08:00
{
$this->_argv[$key] = $value;
return $this;
}
/**
* @return false|string
*/
2020-12-17 14:09:14 +08:00
public function toJson(): bool|string
2020-09-07 15:47:13 +08:00
{
return json_encode($this->_argv, JSON_UNESCAPED_UNICODE);
}
/**
* @param $parameters
* @return array
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function resolve($parameters): array
2020-09-07 15:47:13 +08:00
{
$arrays = [];
$parameters = array_slice($parameters, 1);
2021-02-22 12:02:10 +08:00
if (empty($parameters)) {
return $arrays;
}
2020-09-07 18:11:36 +08:00
$this->_command = array_shift($parameters);
2020-09-07 15:47:13 +08:00
foreach ($parameters as $parameter) {
$explode = explode('=', $parameter);
if (count($explode) < 2) {
continue;
}
$arrays[array_shift($explode)] = current($explode);
}
return $arrays;
}
/**
* @return string
*/
2020-12-17 14:09:14 +08:00
public function getCommand(): string
2020-09-07 15:47:13 +08:00
{
return $this->_command;
}
}