chore: Типы
This commit is contained in:
parent
f4d829119b
commit
233749f1ea
4 changed files with 82 additions and 26 deletions
|
|
@ -63,7 +63,18 @@ class Collection implements \ArrayAccess
|
||||||
*/
|
*/
|
||||||
public function getInt(string $key, int $default = 0): int
|
public function getInt(string $key, int $default = 0): int
|
||||||
{
|
{
|
||||||
return (int)$this->get($key, $default);
|
$value = $this->get($key);
|
||||||
|
|
||||||
|
// Фильтруем как целое число
|
||||||
|
if (is_numeric($value)) {
|
||||||
|
// Приводим к int, но сначала проверим, что не float с дробной частью
|
||||||
|
$floatVal = (float)$value;
|
||||||
|
if (is_finite($floatVal) && floor($floatVal) === $floatVal) {
|
||||||
|
return (int)$floatVal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -73,9 +84,53 @@ class Collection implements \ArrayAccess
|
||||||
*/
|
*/
|
||||||
public function getString(string $key, string $default = ''): string
|
public function getString(string $key, string $default = ''): string
|
||||||
{
|
{
|
||||||
return (string)$this->get($key, $default);
|
$value = $this->get($key);
|
||||||
|
|
||||||
|
if (is_string($value)) {
|
||||||
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_numeric($value)) {
|
||||||
|
return (string)$value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Получает булево значение
|
||||||
|
* Поддерживает: 1, '1', 'true', 'on', 'yes' → true
|
||||||
|
* Иначе → false
|
||||||
|
*/
|
||||||
|
public function getBool(string $key, bool $default = false): bool
|
||||||
|
{
|
||||||
|
$value = $this->get($key);
|
||||||
|
|
||||||
|
if (is_bool($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_string($value)) {
|
||||||
|
$value = strtolower(trim($value));
|
||||||
|
return in_array($value, ['1', 'true', 'on', 'yes'], true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_numeric($value)) {
|
||||||
|
return (bool)$value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getArray(string $key, array $default = []): array {
|
||||||
|
$result = $this->get($key);
|
||||||
|
if (is_array($result)) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param int $default
|
* @param int $default
|
||||||
|
|
|
||||||
|
|
@ -231,7 +231,7 @@ class Action implements ActionInterface
|
||||||
/**
|
/**
|
||||||
* Страница по умолчанию
|
* Страница по умолчанию
|
||||||
* @param HttpRequest $request
|
* @param HttpRequest $request
|
||||||
* @return View|string
|
* @return string|false
|
||||||
*/
|
*/
|
||||||
public function actionIndex(HttpRequest $request) {
|
public function actionIndex(HttpRequest $request) {
|
||||||
return "";
|
return "";
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,7 @@
|
||||||
*/
|
*/
|
||||||
namespace ctiso;
|
namespace ctiso;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use ArrayAccess;
|
|
||||||
use ctiso\Collection;
|
use ctiso\Collection;
|
||||||
use ctiso\Session;
|
use ctiso\Session;
|
||||||
|
|
||||||
|
|
@ -77,6 +76,16 @@ class HttpRequest extends Collection
|
||||||
return parent::get('data')->getNat($key, $default);
|
return parent::get('data')->getNat($key, $default);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getBool(string $key, bool $default = false): bool
|
||||||
|
{
|
||||||
|
return parent::get('data')->getBool($key, $default);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getArray(string $key, array $default = []): array
|
||||||
|
{
|
||||||
|
return parent::get('data')->getArray($key, $default);
|
||||||
|
}
|
||||||
|
|
||||||
function session(?Session $value = null): ?Session
|
function session(?Session $value = null): ?Session
|
||||||
{
|
{
|
||||||
if ($value) {
|
if ($value) {
|
||||||
|
|
@ -85,14 +94,6 @@ class HttpRequest extends Collection
|
||||||
return $this->_session;
|
return $this->_session;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getArray(string $key, array $default = []): array {
|
|
||||||
$result = parent::get('data')->get($key, $default);
|
|
||||||
if (is_array($result)) {
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
return $default;
|
|
||||||
}
|
|
||||||
|
|
||||||
function set(string $key, mixed $value): void
|
function set(string $key, mixed $value): void
|
||||||
{
|
{
|
||||||
parent::get('data')->set($key, $value);
|
parent::get('data')->set($key, $value);
|
||||||
|
|
|
||||||
22
src/Path.php
22
src/Path.php
|
|
@ -21,7 +21,7 @@ class Path
|
||||||
*/
|
*/
|
||||||
public function __construct($path = '')
|
public function __construct($path = '')
|
||||||
{
|
{
|
||||||
$this->url = parse_url($path);
|
$this->url = parse_url($path) ?: [];
|
||||||
|
|
||||||
if (isset($this->url['path'])) {
|
if (isset($this->url['path'])) {
|
||||||
$path = $this->url['path'];
|
$path = $this->url['path'];
|
||||||
|
|
@ -174,17 +174,17 @@ class Path
|
||||||
*/
|
*/
|
||||||
public static function makeUrl($path): string
|
public static function makeUrl($path): string
|
||||||
{
|
{
|
||||||
$slash = (isset($path['host']) && (strlen($path['path']) > 0) && ($path['path'][0] != '/')) ? '/' : '';
|
$slash = (isset($path['host']) && isset($path['path']) && (strlen($path['path']) > 0) && ($path['path'][0] != '/')) ? '/' : '';
|
||||||
|
$scheme = isset($path['scheme']) ? $path['scheme'] . ':/' : '';
|
||||||
|
$user = isset($path['user']) ? $path['user'] . (isset($path['pass']) ? ':' . $path['pass'] : '') . '@' : '';
|
||||||
|
|
||||||
return (isset($path['scheme']) ? $path['scheme'] . ':/' : '')
|
$port = isset($path['port']) ? ':' . $path['port'] : '';
|
||||||
. (isset($path['host']) ? ('/'
|
$host = isset($path['host']) ? ('/' . $user . $path['host'] . $port) : '';
|
||||||
. (isset($path['user']) ? $path['user'] . (isset($path['pass']) ? ':' . $path['pass'] : '') . '@' : '')
|
|
||||||
. $path['host']
|
$query = isset($path['query']) ? '?' . $path['query'] : '';
|
||||||
. (isset($path['port']) ? ':' . $path['port'] : '')) : '')
|
$fragment = isset($path['fragment']) ? '#' . $path['fragment'] : '';
|
||||||
. $slash
|
|
||||||
. ($path['path'] ?? '')
|
return $scheme . $host . $slash . ($path['path'] ?? '') . $query . $fragment;
|
||||||
. (isset($path['query']) ? '?' . $path['query'] : '')
|
|
||||||
. (isset($path['fragment']) ? '#' . $path['fragment'] : '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue