Skip to content

dmitry-ivanov/laravel-helper-functions

Repository files navigation

Laravel helper functions

StyleCI SensioLabsInsight Build Status Coverage Status

Provides Laravel-specific and pure PHP helper functions.

Requirements

  • PHP >=5.6.4
  • Laravel >=5.2

Usage

  1. Install package through composer:

    composer require illuminated/helper-functions
  2. That's it! Now you can use any of provided helper functions.

Available functions

Array

array_except_value()

Removes the given values from 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']

Artisan

call_in_background()

Calls artisan console command in background, with optional before and after sub-commands:

call_in_background('foo');

// "php artisan foo" would be called in background
call_in_background('foo:bar baz', 'sleep 0.3');

// "sleep 0.3 && php artisan foo:bar baz" would be called in background

Database

db_is_mysql()

Checks if default database connection is mysql or not:

if (db_is_mysql()) {
    // mysql-specific code here
}

db_mysql_now()

Returns database datetime, using mysql connection:

$now = db_mysql_now();

// 2016-06-23 15:23:16

db_mysql_variable()

Returns value of specified mysql variable, or false if variable doesn't exist:

$hostname = db_mysql_variable('hostname');

// localhost

Dump

get_dump()

Returns nicely formatted string representation of the variable, using Symfony VarDumper Component with all of it's benefits:

$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" => []
// ]

Email

is_email()

Checks if specified string is valid email address or not:

$isEmail = is_email('john.doe@example.com');

// true

to_rfc2822_email()

Converts 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()

Converts 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"]

Format

format_bytes()

Formats bytes into kilobytes, megabytes, gigabytes or terabytes, with specified precision:

$formatted = format_bytes(3333333);

// 3.18 MB

Json

is_json()

Checks if specified variable is valid json-encoded string or not:

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

// true

Function can return decoded json, if you pass the second return argument as true:

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

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

Strings

str_lower()

Converts string to lowercase:

$lower = str_lower('TeSt');

// test

str_upper()

Converts string to uppercase:

$upper = str_upper('TeSt');

// TEST