mekramy/php-util

PHP 实用函数

v1.0.0 2020-04-02 07:29 UTC

This package is auto-updated.

Last update: 2024-09-29 05:29:44 UTC


README

快速切换

如果你需要检查多个条件来设置变量值,可以使用 quickSwitch 而不是深层三元操作符 (?:),if/else 或 switch/case。

use MEkramy\PHPUtil\Helpers;

$res = Helpers::quickSwitch([
    'First Value' => false,
    'Second Value' => function(){
        return false;
    },
    'Third Value' => falsyFunction(),
    'True Option' => true
], 'Default Value');
echo $res; # > "True Option"

$res = Helpers::quickSwitch([
    'First Value' => false,
    'Second Value' => function(){
        return false;
    },
    'Third Value' => falsyFunction(),
], 'Default Value');
echo $res; # > "Default Value"

快速切换简单接受一个关联数组 value => condition。如果任何条件为真或返回真值,则返回该值,否则返回默认值。

将日期转换为波斯日期

你可以传递 string 日期,DateTime 对象,Carbon 实例或 timestamp 来解析为波斯日期。

static function toPersianDate($date = null, ?string $format = 'Y-m-d H:i:s')

注意: 如果你传递 null 作为输入日期,此函数返回当前日期

注意: 如果你传递 null 作为格式参数,此函数返回 \Hekmatinasser\Verta\Verta 对象而不是日期 string

注意: 如果传递了无效的日期,此函数返回 null

use MEkramy\PHPUtil\Helpers;

$gregorian_date = '2019-03-21';
$res = Helpers::toPersianDate($gregorian_date); # > 1398-01-01 00:00:00
$res = Helpers::toPersianDate($gregorian_date, 'Y'); # > 1398
$res = Helpers::toPersianDate($gregorian_date, null); # > \Hekmatinasser\Verta\Verta instance

将波斯日期转换为日期

你可以传递 string 波斯日期或 \Hekmatinasser\Verta\Verta 实例来解析为公历日期。

static function toPersianDate($date = null, ?string $format = 'Y-m-d H:i:s')

注意: 如果你传递 null 作为输入日期,此函数返回当前日期

注意: 如果你传递 null 作为格式参数,此函数返回 \Carbon\Carbon 对象而不是日期 string

注意: 如果传递了无效的日期,此函数返回 null

use MEkramy\PHPUtil\Helpers;

$persian_date = '1398-01-01';
$res = Helpers::toGregorianDate($persian_date); # > 2019-03-21 00:00:00
$res = Helpers::toGregorianDate($persian_date, 'Y'); # > 2019
$res = Helpers::toGregorianDate($persian_date, null); # > \Carbon\Carbon instance

验证值

检查值是否不为空且为有效值(如果传递了允许列表)或返回默认值如果不有效。

static function validateOrDefault($value, ?array $allowed, $default = null)

注意:null 作为允许值传递以忽略检查

use MEkramy\PHPUtil\Helpers;

$res = Helpers::validateOrDefault(null, null, 'default'); # > "default"
$res = Helpers::validateOrDefault("my val", null, 'default'); # > "my val"
$res = Helpers::validateOrDefault("test", ["first valid value", "second"], 'default'); # > "default"

验证数字

检查值是否为数字且在范围(最小/最大)内且有效(允许值)或返回默认值。

public static function validateNumberOrDefault($value, bool $float = false, $min = null, $max = null, ?array $allowed = null, $default = null)

注意: 如果将 $float 设置为 true,则将 $value 解析为 float,否则解析为 int

注意:null 作为允许值传递以忽略检查

注意:null 作为最小/最大值传递以忽略检查

use MEkramy\PHPUtil\Helpers;

$res = Helpers::validateNumberOrDefault(12, false, null, null, null, 0); # > 12
$res = Helpers::validateNumberOrDefault(12, true, 12.001, null, null, 0); # > 0
$res = Helpers::validateNumberOrDefault("12.03", true, 11.99, 12.99, null, 0); # > 12.03
$res = Helpers::validateNumberOrDefault(7, true, null, null, [1, 2, 3, 4], 0); # > 0

将值转换为布尔值

如果值是 1"1"true"true""on""yes" 返回 true,否则返回 false

use MEkramy\PHPUtil\Helpers;

$res = Helpers::asBoolean("1"); # > true
$res = Helpers::asBoolean(true); # > true
$res = Helpers::asBoolean("on"); # > true
$res = Helpers::asBoolean("yes"); # > true

$res = Helpers::asBoolean(null); # > false
$res = Helpers::asBoolean("-"); # > false

从字符串中提取数字

use MEkramy\PHPUtil\Helpers;

$res = Helpers::extractNumbers("0123- 456"); # > "0123456"
$res = Helpers::extractNumbers("1 this is a text 2 contains3 string"); # > "123"
$res = Helpers::extractNumbers("text with no numbers"); # > ""

使用占位符格式化字符串

use MEkramy\PHPUtil\Helpers;

$res = Helpers::formatString("{0} - {1}", ['abc', 'def']); # > "abc - def"
$res = Helpers::formatString("{first} {last}", ['first' => 'John', 'last' => 'Doe']); # > "John Doe"
$res = Helpers::formatString("you search {search} and we found no result for {search}", ['search' => 'dummy']); # > "you search dummy and we found no result for dummy"

打印调试块

此函数将输入变量转换为格式化的 JSON 并使用带有头和尾的 echo 打印到输出。

注意: 你可以指定头部文本、分隔符字符和头部/尾部长度

use MEkramy\PHPUtil\Helpers;

$a = [
    "first_name" => "john",
    "last_name" => "doe",
    "address" => [
        "city" => "Somewhere",
        "street" => null
    ]
];

Helpers::printDebug($a, "john doe info", "*", 75);

# ****************************** JOHN DOE INFO ******************************
# {
#     "first_name": "john",
#     "last_name": "doe",
#     "address": {
#         "city": "Somewhere",
#         "street": null
#     }
# }
# ***************************************************************************

Helpers::printDebug("simple value", "simple", "-", 25);

# -------- SIMPLE ---------
# "simple value"
# -------------------------

Helpers::printDebug(pi());

# ===================== DEBUG ======================
# 3.141592653589793
# ==================================================