illuminated/helper-functions

Laravel 专用和纯 PHP 辅助函数。


README

Laravel-specific and pure PHP Helper Functions

Laravel 辅助函数

Buy me a coffee

StyleCI Build Status Coverage Status

Packagist Version Packagist Stars Packagist Downloads Packagist License

Laravel 专用和纯 PHP 辅助函数。

用法

  1. 通过 Composer 安装包

    composer require illuminated/helper-functions
  2. 使用提供的任何辅助函数

    if (is_windows_os()) {
        call_in_background('switch-to-mac');
    }

可用函数

欢迎贡献力量。

数组

array_except_value()

从数组中移除指定的值

array_except_value(['foo', 'bar', 'baz'], 'baz');

// ["foo", "bar"]
array_except_value(['foo', 'bar', 'baz'], ['bar', 'baz']);

// ["foo"]

multiarray_set()

使用“点”符号为多维数组中的每个项目设置值

$array = [
    ['name' => 'Mercedes-Benz', 'details' => ['type' => 'SUV']],
    ['name' => 'BMW', 'details' => ['type' => 'SUV']],
    ['name' => 'Porsche', 'details' => ['type' => 'SUV']],
];

multiarray_set($array, 'details.country', 'Germany');

// [
//     ["name" => "Mercedes-Benz", "details" => ["type" => "SUV", "country" => "Germany"]],
//     ["name" => "BMW", "details" => ["type" => "SUV", "country" => "Germany"]],
//     ["name" => "Porsche", "details" => ["type" => "SUV", "country" => "Germany"]],
// ]

multiarray_sort_by()

按多个字段对多维数组进行排序

$array = [
    ['name' => 'Mercedes-Benz', 'model' => 'GLS', 'price' => 120000],
    ['name' => 'Mercedes-Benz', 'model' => 'GLE Coupe', 'price' => 110000],
    ['name' => 'BMW', 'model' => 'X6', 'price' => 77000],
    ['name' => 'Porsche', 'model' => 'Cayenne', 'price' => 117000],
];

$sorted = multiarray_sort_by($array, 'name', 'model');

// [
//     ["name" => "BMW", "model" => "X6", "price" => 77000],
//     ["name" => "Mercedes-Benz", "model" => "GLE Coupe", "price" => 110000],
//     ["name" => "Mercedes-Benz", "model" => "GLS", "price" => 120000],
//     ["name" => "Porsche", "model" => "Cayenne", "price" => 117000],
// ]

还可以更改排序顺序

$array = [
    ['name' => 'Mercedes-Benz', 'model' => 'GLS', 'price' => 120000],
    ['name' => 'Mercedes-Benz', 'model' => 'GLE Coupe', 'price' => 110000],
    ['name' => 'BMW', 'model' => 'X6', 'price' => 77000],
    ['name' => 'Porsche', 'model' => 'Cayenne', 'price' => 117000],
];

$sorted = multiarray_sort_by($array, 'name', SORT_ASC, 'model', SORT_DESC);

// [
//     ["name" => "BMW", "model" => "X6", "price" => 77000],
//     ["name" => "Mercedes-Benz", "model" => "GLS", "price" => 120000],
//     ["name" => "Mercedes-Benz", "model" => "GLE Coupe", "price" => 110000],
//     ["name" => "Porsche", "model" => "Cayenne", "price" => 117000],
// ]

Artisan

call_in_background()

在后台调用给定的 artisan 命令行工具。

代码执行立即继续,无需等待结果。

call_in_background('report');

// "php artisan report" would be called in background

可以使用可选的 beforeafter 子命令

call_in_background('report:monthly subscriptions', 'sleep 0.3');

// "sleep 0.3 && php artisan report:monthly subscriptions" would be called in background

数据库

db_is_sqlite()

检查默认数据库连接驱动程序是否为 sqlite

db_is_sqlite();

// false

db_is_mysql()

检查默认数据库连接驱动程序是否为 mysql

db_is_mysql();

// true

db_mysql_now()

获取当前的 MySQL 日期和时间

db_mysql_now();

// "2020-05-25 20:09:33"

db_mysql_variable()

获取指定的 MySQL 变量的值

db_mysql_variable('hostname');

// "localhost"

日期

to_default_timezone()

将给定的日期时间转换为默认时区(参见 app.timezone 配置)

to_default_timezone('2017-02-28T14:05:01Z');

// "2017-02-28 16:05:01", assuming that `app.timezone` is "Europe/Kiev"

调试

backtrace_as_string()

获取不带参数的回溯,作为字符串

$backtrace = backtrace_as_string();

#0  backtrace_as_string() called at [/htdocs/example/routes/web.php:15]
#1  Illuminate\Routing\Router->{closure}() called at [/htdocs/example/vendor/laravel/framework/src/Illuminate/Routing/Route.php:189]
#2  Illuminate\Foundation\Http\Kernel->handle() called at [/htdocs/example/public/index.php:53]

minimized_backtrace_as_string()

获取最小化的回溯,作为字符串

$backtrace = minimized_backtrace_as_string();

#0 /htdocs/example/routes/web.php:15
#1 /htdocs/example/vendor/laravel/framework/src/Illuminate/Routing/Route.php:189
#2 /htdocs/example/public/index.php:53

电子邮件

is_email()

检查给定的字符串是否为电子邮件地址

is_email('john.doe@example.com');

// true

to_rfc2822_email()

将地址数据转换为 RFC 2822 字符串,适用于 PHP mail() 函数

to_rfc2822_email([
    ['address' => 'john.doe@example.com', 'name' => 'John Doe'],
    ['address' => 'jane.smith@example.com'],
]);

// "John Doe <john.doe@example.com>, jane.smith@example.com"

同时,它支持单地址的简化语法

to_rfc2822_email(['address' => 'john.doe@example.com', 'name' => 'John Doe']);

// "John Doe <john.doe@example.com>"

to_swiftmailer_emails()

将地址数据转换为 SwiftMailer 适合的格式

to_swiftmailer_emails([
    ['address' => 'john.doe@example.com', 'name' => 'John Doe'],
    ['address' => 'jane.smith@example.com'],
]);

// ["john.doe@example.com" => "John Doe", "jane.smith@example.com"]

同时,它支持单地址的简化语法

to_swiftmailer_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe']);

// ["john.doe@example.com" => "John Doe"]

to_symfony_emails()

将地址数据转换为 Symfony 适合的格式

to_symfony_emails([
    ['address' => 'john.doe@example.com', 'name' => 'John Doe'],
    ['address' => 'jane.smith@example.com'],
]);

// ["John Doe <john.doe@example.com>", "jane.smith@example.com"]

同时,它支持单地址的简化语法

to_symfony_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe']);

// ["John Doe <john.doe@example.com>"]

文件系统

relative_path()

获取给定文件夹的相对路径

relative_path('/var/www/htdocs', '/var/www/htdocs/example');

// "../"

您也可以将相对路径作为参数传递

relative_path('/var/www/htdocs/example/public/../../', '/var/');

// "www/htdocs/"

格式化

get_dump()

使用 Symfony VarDumper 组件 获取变量的美观的字符串表示形式

$array = [
    'a simple string' => 'Hello!',
    'a float' => 1.0,
    'an integer' => 1,
    'a boolean' => true,
    'an empty array' => [],
];

$dump = get_dump($array);

// array:5 [
//     "a simple string" => "Hello!"
//     "a float" => 1.0
//     "an integer" => 1
//     "a boolean" => true
//     "an empty array" => []
// ]

format_bytes()

将字节数格式化为千字节、兆字节、吉字节或太字节

format_bytes(3333333);

// "3.18 MB"

format_xml()

使用新行和缩进格式化给定的 XML 字符串

format_xml('<?xml version="1.0"?><root><task priority="low"><to>John</to><from>Jane</from><title>Go to the shop</title></task><task priority="medium"><to>John</to><from>Paul</from><title>Finish the report</title></task><task priority="high"><to>Jane</to><from>Jeff</from><title>Clean the house</title></task></root>');

// <?xml version="1.0"?>
// <root>
//   <task priority="low">
//     <to>John</to>
//     <from>Jane</from>
//     <title>Go to the shop</title>
//   </task>
//   <task priority="medium">
//     <to>John</to>
//     <from>Paul</from>
//     <title>Finish the report</title>
//   </task>
//   <task priority="high">
//     <to>Jane</to>
//     <from>Jeff</from>
//     <title>Clean the house</title>
//   </task>
// </root>

JSON

is_json()

检查给定的值是否为有效的 JSON 编码字符串

is_json('{"foo":1,"bar":2,"baz":3}');

// true

如果您将 true 作为第二个参数传递,则返回解码的 JSON

is_json('{"foo":1,"bar":2,"baz":3}', true);

// ["foo" => 1, "bar" => 2, "baz" => 3]

系统

is_windows_os()

检查操作系统是否为 Windows

is_windows_os();

// false

XML

xml_to_array()

将给定的 XML 转换为数组

xml_to_array('<?xml version="1.0"?>
<Guys>
    <Good_guy Rating="100">
        <name>Luke Skywalker</name>
        <weapon>Lightsaber</weapon>
    </Good_guy>
    <Bad_guy Rating="90">
        <name>Sauron</name>
        <weapon>Evil Eye</weapon>
    </Bad_guy>
</Guys>
');

// [
//     "Good_guy" => [
//         "name" => "Luke Skywalker",
//         "weapon" => "Lightsaber",
//         "@attributes" => [
//             "Rating" => "100",
//         ],
//     ],
//     "Bad_guy" => [
//         "name" => "Sauron",
//         "weapon" => "Evil Eye",
//         "@attributes" => [
//             "Rating" => "90",
//         ],
//     ],
// ]

或者,您也可以传递一个 SimpleXMLElement 类的实例而不是字符串。

array_to_xml()

将给定的数组转换为XML字符串

$array = [
    'Good guy' => [
        'name' => 'Luke Skywalker',
        'weapon' => 'Lightsaber',
        '@attributes' => [
            'Rating' => '100',
        ],
    ],
    'Bad guy' => [
        'name' => 'Sauron',
        'weapon' => 'Evil Eye',
        '@attributes' => [
            'Rating' => '90',
        ],
    ]
];

$xml = array_to_xml($array, 'Guys');

// <?xml version="1.0" encoding="utf-8"?>
// <Guys>
//    <Good_guy Rating="100">
//        <name>Luke Skywalker</name>
//        <weapon>Lightsaber</weapon>
//    </Good_guy>
//    <Bad_guy Rating="90">
//        <name>Sauron</name>
//        <weapon>Evil Eye</weapon>
//    </Bad_guy>
// </Guys>

赞助商

Laravel Idea
Material Theme UI Plugin

许可证

Laravel Helper Functions 是开源软件,采用 MIT 许可证

买我一杯咖啡