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

92 lines
1.2 KiB
PHP
Raw Normal View History

2020-09-07 15:47:13 +08:00
<?php
namespace Snowflake\Abstracts;
use Exception;
class Input
{
private $_argv = [];
private $_command = '';
/**
* Input constructor.
* @param $argv
* @throws Exception
*/
public function __construct($argv)
{
$this->_argv = $this->resolve($argv);
}
/**
* @param $key
* @param null $default
* @return mixed|null
*/
public function get($key, $default = null)
{
return $this->_argv[$key] ?? $default;
}
/**
* @param $key
* @param $value
* @return $this
*/
public function set($key, $value)
{
$this->_argv[$key] = $value;
return $this;
}
/**
* @return false|string
*/
public function toJson()
{
return json_encode($this->_argv, JSON_UNESCAPED_UNICODE);
}
/**
* @param $parameters
* @return array
* @throws Exception
*/
public function resolve($parameters)
{
$arrays = [];
$parameters = array_slice($parameters, 1);
2020-09-07 15:52:42 +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
*/
public function getCommand()
{
return $this->_command;
}
}