chore: Типы

This commit is contained in:
origami11@yandex.ru 2025-12-10 17:46:56 +03:00
parent f4d829119b
commit 233749f1ea
4 changed files with 82 additions and 26 deletions

View file

@ -63,7 +63,18 @@ class Collection implements \ArrayAccess
*/
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
{
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 int $default

View file

@ -231,7 +231,7 @@ class Action implements ActionInterface
/**
* Страница по умолчанию
* @param HttpRequest $request
* @return View|string
* @return string|false
*/
public function actionIndex(HttpRequest $request) {
return "";

View file

@ -5,8 +5,7 @@
*/
namespace ctiso;
use Exception;
use ArrayAccess;
use ctiso\Collection;
use ctiso\Session;
@ -69,12 +68,22 @@ class HttpRequest extends Collection
function getInt(string $key, int $default = 0): int
{
return parent::get('data')->getInt($key, $default);
return parent::get('data')->getInt($key, $default);
}
function getNat(string $key, int $default = 1): int
{
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
@ -85,14 +94,6 @@ class HttpRequest extends Collection
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
{
parent::get('data')->set($key, $value);

View file

@ -21,7 +21,7 @@ class Path
*/
public function __construct($path = '')
{
$this->url = parse_url($path);
$this->url = parse_url($path) ?: [];
if (isset($this->url['path'])) {
$path = $this->url['path'];
@ -174,17 +174,17 @@ class Path
*/
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'] . ':/' : '')
. (isset($path['host']) ? ('/'
. (isset($path['user']) ? $path['user'] . (isset($path['pass']) ? ':' . $path['pass'] : '') . '@' : '')
. $path['host']
. (isset($path['port']) ? ':' . $path['port'] : '')) : '')
. $slash
. ($path['path'] ?? '')
. (isset($path['query']) ? '?' . $path['query'] : '')
. (isset($path['fragment']) ? '#' . $path['fragment'] : '');
$port = isset($path['port']) ? ':' . $path['port'] : '';
$host = isset($path['host']) ? ('/' . $user . $path['host'] . $port) : '';
$query = isset($path['query']) ? '?' . $path['query'] : '';
$fragment = isset($path['fragment']) ? '#' . $path['fragment'] : '';
return $scheme . $host . $slash . ($path['path'] ?? '') . $query . $fragment;
}
/**