Package Data | |
---|---|
Maintainer Username: | dmitry-ivanov |
Maintainer Contact: | dmitry.g.ivanov@gmail.com (Dmitry Ivanov) |
Package Create Date: | 2016-06-17 |
Package Last Update: | 2024-03-06 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-11 15:05:07 |
Package Statistics | |
---|---|
Total Downloads: | 383,453 |
Monthly Downloads: | 14,165 |
Daily Downloads: | 847 |
Total Stars: | 107 |
Total Watchers: | 4 |
Total Forks: | 17 |
Total Open Issues: | 0 |
Laravel-specific and pure PHP helper functions.
| Laravel | Helper Functions | | ------- | :-------------------------------------------------------------------------: | | 5.1.* | 5.1.* | | 5.2.* | 5.2.* | | 5.3.* | 5.3.* | | 5.4.* | 5.4.* | | 5.5.* | 5.5.* | | 5.6.* | 5.6.* | | 5.7.* | 5.7.* | | 5.8.* | 5.8.* |
Install the package via Composer:
composer require illuminated/helper-functions
Use any of the provided helper functions.
New functions are always adding. Feel free to contribute.
array_except_value()
Remove the given values from the array:
$array = ['foo', 'bar', 'baz'];
$array = array_except_value($array, 'baz');
// ['foo', 'bar']
$array = ['foo', 'bar', 'baz'];
$array = array_except_value($array, ['bar', 'baz']);
// ['foo']
multiarray_set()
Set a 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 few 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],
// ]
You can set required 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],
// ]
call_in_background()
Call artisan console command in the 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 can be set as a second and third parameter:
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()
Check if the default database connection driver is sqlite
or not:
if (db_is_sqlite()) {
// sqlite-specific code here
}
db_is_mysql()
Check if the default database connection driver is mysql
or not:
if (db_is_mysql()) {
// mysql-specific code here
}
db_mysql_now()
Get database datetime, using mysql
connection:
$now = db_mysql_now();
// 2016-06-23 15:23:16
db_mysql_variable()
Get the value of specified mysql
variable, or false
if the variable doesn't exist:
$hostname = db_mysql_variable('hostname');
// localhost
to_default_timezone()
Convert passed datetime string to the default timezone, which is app.timezone
config setting:
$date = to_default_timezone('2017-02-28T14:05:01Z');
// 2017-02-28 16:05:01, assuming timezone is Europe/Kiev
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 if the specified string is a valid email address or not:
$isEmail = is_email('john.doe@example.com');
// true
to_rfc2822_email()
Convert addresses data to RFC 2822 string, suitable for PHP mail() function:
$address = to_rfc2822_email([
['address' => 'john.doe@example.com', 'name' => 'John Doe'],
['address' => 'mary.smith@example.com'],
]);
// John Doe <john.doe@example.com>, mary.smith@example.com
Also supports simplified syntax for single address item:
$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 format, which is suitable for SwiftMailer library:
$addresses = to_swiftmailer_emails([
['address' => 'john.doe@example.com', 'name' => 'John Doe'],
['address' => 'mary.smith@example.com'],
]);
// ["john.doe@example.com" => "John Doe", "mary.smith@example.com"]
Also supports simplified syntax for single address item:
$address = to_swiftmailer_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe']);
// ["john.doe@example.com" => "John Doe"]
relative_path()
Get the relative path for two directories:
$path = relative_path('/var/www/htdocs', '/var/www/htdocs/example')
// '../'
You can pass relative path as a parameter too:
$path = relative_path('/var/www/htdocs/example/public/../../', '/var/')
// 'www/htdocs/'
get_dump()
Get nicely formatted string representation of the variable, using Symfony VarDumper Component:
$array = [
'a simple string' => 'in an array of 5 elements',
'a float' => 1.0,
'an integer' => 1,
'a boolean' => true,
'an empty array' => [],
];
$dump = get_dump($array);
// array:5 [
// "a simple string" => "in an array of 5 elements"
// "a float" => 1.0
// "an integer" => 1
// "a boolean" => true
// "an empty array" => []
// ]
format_bytes()
Format bytes into kilobytes, megabytes, gigabytes or terabytes, with specified precision:
$formatted = format_bytes(3333333);
// 3.18 MB
format_xml()
Format XML string using new lines and indents:
$formatted = 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>
is_json()
Check if specified variable is a valid JSON-encoded string or not:
$isJson = is_json('{"foo":1,"bar":2,"baz":3}');
// true
It can return decoded JSON if you pass the second argument as true
:
$decoded = is_json('{"foo":1,"bar":2,"baz":3}', true);
// ['foo' => 1, 'bar' => 2, 'baz' => 3]
str_lower()
Convert string to lowercase, using mb_strtolower
in UTF-8
encoding:
$lower = str_lower('TeSt');
// test
str_upper()
Convert string to uppercase, using mb_strtoupper
in UTF-8
encoding:
$upper = str_upper('TeSt');
// TEST
is_windows_os()
Check if PHP is running on Windows OS or not:
$isWindowsOs = is_windows_os();
// boolean
xml_to_array()
Convert XML string to the array:
$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 a SimpleXMLElement
object, to get the same results.
array_to_xml()
Convert array to XML string using spatie/array-to-xml package:
$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"?>
// <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>
The MIT License. Please see License File for more information.