You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Helper;
|
|
|
|
|
|
|
|
|
|
class Param
|
|
|
|
|
{
|
|
|
|
|
public const EMPTY_VALUES = ['', null];
|
|
|
|
|
|
|
|
|
|
public const ZERO_VALUES = [0, '0', '', null];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* params是否存在key键(若key对应的值存在于emptyValues中,则也认为不存在)
|
|
|
|
|
*/
|
|
|
|
|
public static function has(array $params, $key, array $emptyValues = self::EMPTY_VALUES): bool
|
|
|
|
|
{
|
|
|
|
|
if (isset($params[$key])) {
|
|
|
|
|
foreach ($emptyValues as $value) {
|
|
|
|
|
if ($params[$key] === $value) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function require(array $params, array $requireKeys): array
|
|
|
|
|
{
|
|
|
|
|
$requireParams = [];
|
|
|
|
|
foreach ($requireKeys as $key) {
|
|
|
|
|
$requireParams[$key] = $params[$key] ?? null;
|
|
|
|
|
}
|
|
|
|
|
return $requireParams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function default(array $params, array $defaults): array
|
|
|
|
|
{
|
|
|
|
|
foreach ($defaults as $key => $value) {
|
|
|
|
|
if (!isset($params[$key])) {
|
|
|
|
|
$params[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function remove(array $params, array $removeKeys): array
|
|
|
|
|
{
|
|
|
|
|
foreach ($removeKeys as $key) {
|
|
|
|
|
if (isset($params[$key])) {
|
|
|
|
|
unset($params[$key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $params;
|
|
|
|
|
}
|
|
|
|
|
}
|