illuminated/helper-functions
Composer 安装命令:
composer require illuminated/helper-functions
包简介
Laravel-specific and pure PHP Helper Functions.
关键字:
README 文档
README
Laravel Helper Functions
Laravel-specific and pure PHP Helper Functions.
| Laravel | Helper Functions |
|---|---|
| 12.x | 12.x |
| 11.x | 11.x |
| 10.x | 10.x |
| 9.x | 9.x |
| 8.x | 8.x |
| 7.x | 7.x |
| 6.x | 6.x |
| 5.8.* | 5.8.* |
| 5.7.* | 5.7.* |
| 5.6.* | 5.6.* |
| 5.5.* | 5.5.* |
| 5.4.* | 5.4.* |
| 5.3.* | 5.3.* |
| 5.2.* | 5.2.* |
| 5.1.* | 5.1.* |
Usage
-
Install the package via Composer:
composer require illuminated/helper-functions
-
Use any of the provided helper functions:
if (is_windows_os()) { call_in_background('switch-to-mac'); }
Available functions
Feel free to contribute.
Array
array_except_value()
Remove the given values from the array:
array_except_value(['foo', 'bar', 'baz'], 'baz'); // ["foo", "bar"]
array_except_value(['foo', 'bar', 'baz'], ['bar', 'baz']); // ["foo"]
multiarray_set()
Set the value for each item of the multidimensional array using "dot" notation:
$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()
Sort the multidimensional array by several fields:
$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], // ]
Also, you can change the sort order:
$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()
Call the given artisan console command in background.
Code execution continues immediately, without waiting for results.
call_in_background('report'); // "php artisan report" would be called in background
Optional before and after sub-commands could be used:
call_in_background('report:monthly subscriptions', 'sleep 0.3'); // "sleep 0.3 && php artisan report:monthly subscriptions" would be called in background
Database
db_is_sqlite()
Check whether the default database connection driver is sqlite or not:
db_is_sqlite(); // false
db_is_mysql()
Check whether the default database connection driver is mysql or not:
db_is_mysql(); // true
db_mysql_now()
Get the current MySQL datetime:
db_mysql_now(); // "2020-05-25 20:09:33"
db_mysql_variable()
Get value of the specified MySQL variable:
db_mysql_variable('hostname'); // "localhost"
Date
to_default_timezone()
Convert the given datetime to the default timezone (see app.timezone config):
to_default_timezone('2017-02-28T14:05:01Z'); // "2017-02-28 16:05:01", assuming that `app.timezone` is "Europe/Kiev"
Debug
backtrace_as_string()
Get backtrace without arguments, as a 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()
Get minimized backtrace, as a 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()
Check whether the given string is an email address or not:
is_email('john.doe@example.com'); // true
to_rfc2822_email()
Convert addresses data to RFC 2822 string, suitable for PHP mail() function:
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"
Also, it supports simplified syntax for a single address:
to_rfc2822_email(['address' => 'john.doe@example.com', 'name' => 'John Doe']); // "John Doe <john.doe@example.com>"
to_swiftmailer_emails()
Convert addresses data to SwiftMailer-suitable format:
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"]
Also, it supports simplified syntax for a single address:
to_swiftmailer_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe']); // ["john.doe@example.com" => "John Doe"]
to_symfony_emails()
Convert addresses data to Symfony-suitable format:
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"]
Also, it supports simplified syntax for a single address:
to_symfony_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe']); // ["John Doe <john.doe@example.com>"]
Filesystem
relative_path()
Get a relative path for the given folders:
relative_path('/var/www/htdocs', '/var/www/htdocs/example'); // "../"
You can pass the relative path as a parameter too:
relative_path('/var/www/htdocs/example/public/../../', '/var/'); // "www/htdocs/"
Format
get_dump()
Get a nicely formatted string representation of the variable, using the Symfony VarDumper Component:
$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 into kilobytes, megabytes, gigabytes or terabytes:
format_bytes(3333333); // "3.18 MB"
format_xml()
Format the given XML string using new lines and indents:
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()
Check whether the given value is a valid JSON-encoded string or not:
is_json('{"foo":1,"bar":2,"baz":3}'); // true
It returns decoded JSON if you pass true as a second argument:
is_json('{"foo":1,"bar":2,"baz":3}', true); // ["foo" => 1, "bar" => 2, "baz" => 3]
System
is_windows_os()
Check whether the operating system is Windows or not:
is_windows_os(); // false
Xml
xml_to_array()
Convert the given XML to array:
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", // ], // ], // ]
Alternatively, you can pass an instance of the SimpleXMLElement class instead of a string.
array_to_xml()
Convert the given array to XML string:
$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>
Sponsors
License
Laravel Helper Functions is open-sourced software licensed under the MIT license.
illuminated/helper-functions 适用场景与选型建议
illuminated/helper-functions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 593.31k 次下载、GitHub Stars 达 107, 最近一次更新时间为 2016 年 06 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「helpers」 「laravel」 「functions」 「Backtrace」 「xml-parser」 「array-to-xml」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 illuminated/helper-functions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 illuminated/helper-functions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 illuminated/helper-functions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
AMWSCAN (Antimalware Scanner) is a php antimalware/antivirus scanner console script written in php for scan your project. This can work on php projects and a lot of others platform.
PHP polyfill for the Apache Functions. The aim is to provides functions as the PHP way in order to get them available even if PHP is not running as an Apache module.
PhpStratum SQLite: A stored procedure loader and wrapper generator for SQLite
HTML and form generation
Falsy helps you manage half-truths with PHP
A siple class to handle errors in PHP functions
统计信息
- 总下载量: 593.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 107
- 点击次数: 32
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-06-17



