├── art
├── 1280x640-source.jpg
├── 1380x575-source.jpg
├── 1280x640-optimized.jpg
├── 1380x575-optimized.jpg
├── sponsor-laravel-idea.png
├── sponsor-material-theme.png
├── sponsor-laravel-idea.pxd
│ ├── metadata.info
│ ├── QuickLook
│ │ ├── Icon.tiff
│ │ └── Thumbnail.tiff
│ └── data
│ │ └── originalImportedContentDocumentInfo
└── sponsor-material-theme.pxd
│ ├── metadata.info
│ ├── QuickLook
│ ├── Icon.tiff
│ └── Thumbnail.tiff
│ └── data
│ ├── 0D6D801E-30F3-46BF-815B-DC27D2EB5BE0
│ └── originalImportedContentDocumentInfo
├── src
├── autoload.php
├── system.php
├── Helpers
│ ├── System
│ │ └── OS.php
│ └── Artisan
│ │ └── BackgroundCommand.php
├── filesystem.php
├── artisan.php
├── date.php
├── json.php
├── xml.php
├── debug.php
├── db.php
├── format.php
├── array.php
└── email.php
├── LICENSE.md
├── composer.json
└── README.md
/art/1280x640-source.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/1280x640-source.jpg
--------------------------------------------------------------------------------
/art/1380x575-source.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/1380x575-source.jpg
--------------------------------------------------------------------------------
/art/1280x640-optimized.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/1280x640-optimized.jpg
--------------------------------------------------------------------------------
/art/1380x575-optimized.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/1380x575-optimized.jpg
--------------------------------------------------------------------------------
/art/sponsor-laravel-idea.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-laravel-idea.png
--------------------------------------------------------------------------------
/art/sponsor-material-theme.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-material-theme.png
--------------------------------------------------------------------------------
/art/sponsor-laravel-idea.pxd/metadata.info:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-laravel-idea.pxd/metadata.info
--------------------------------------------------------------------------------
/art/sponsor-material-theme.pxd/metadata.info:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-material-theme.pxd/metadata.info
--------------------------------------------------------------------------------
/art/sponsor-laravel-idea.pxd/QuickLook/Icon.tiff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-laravel-idea.pxd/QuickLook/Icon.tiff
--------------------------------------------------------------------------------
/art/sponsor-material-theme.pxd/QuickLook/Icon.tiff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-material-theme.pxd/QuickLook/Icon.tiff
--------------------------------------------------------------------------------
/art/sponsor-laravel-idea.pxd/QuickLook/Thumbnail.tiff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-laravel-idea.pxd/QuickLook/Thumbnail.tiff
--------------------------------------------------------------------------------
/art/sponsor-material-theme.pxd/QuickLook/Thumbnail.tiff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-material-theme.pxd/QuickLook/Thumbnail.tiff
--------------------------------------------------------------------------------
/art/sponsor-laravel-idea.pxd/data/originalImportedContentDocumentInfo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-laravel-idea.pxd/data/originalImportedContentDocumentInfo
--------------------------------------------------------------------------------
/art/sponsor-material-theme.pxd/data/0D6D801E-30F3-46BF-815B-DC27D2EB5BE0:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-material-theme.pxd/data/0D6D801E-30F3-46BF-815B-DC27D2EB5BE0
--------------------------------------------------------------------------------
/art/sponsor-material-theme.pxd/data/originalImportedContentDocumentInfo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmitry-ivanov/laravel-helper-functions/HEAD/art/sponsor-material-theme.pxd/data/originalImportedContentDocumentInfo
--------------------------------------------------------------------------------
/src/autoload.php:
--------------------------------------------------------------------------------
1 | files()
7 | ->in(__DIR__)
8 | ->depth(0)
9 | ->name('*.php')
10 | ->notName('autoload.php');
11 |
12 | foreach ($files as $file) {
13 | require_once $file;
14 | }
15 |
--------------------------------------------------------------------------------
/src/system.php:
--------------------------------------------------------------------------------
1 | makePathRelative(realpath($to), realpath($from));
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/artisan.php:
--------------------------------------------------------------------------------
1 | run();
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/date.php:
--------------------------------------------------------------------------------
1 | timezone(Config::get('app.timezone'))
16 | ->toDateTimeString();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/json.php:
--------------------------------------------------------------------------------
1 | map(function (string $line) {
27 | return preg_match('/(#\d+) .*? called at \[(.*?)]/', $line, $matches) || preg_match('/(#\d+) (.*?):/', $line, $matches)
28 | ? "{$matches[1]} {$matches[2]}"
29 | : false;
30 | })
31 | ->filter()
32 | ->implode("\n");
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) Dmitry Ivanov
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/src/db.php:
--------------------------------------------------------------------------------
1 | now;
37 | }
38 | }
39 |
40 | if (!function_exists('db_mysql_variable')) {
41 | /**
42 | * Get value of the specified MySQL variable.
43 | */
44 | function db_mysql_variable(string $name): string|false
45 | {
46 | return DB::selectOne('show variables where variable_name = ?', [$name])->Value ?? false;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "illuminated/helper-functions",
3 | "description": "Laravel-specific and pure PHP Helper Functions.",
4 | "keywords": ["laravel", "helpers", "functions", "background-command", "multiarray", "backtrace", "xml-parser", "xml-to-array", "array-to-xml"],
5 | "license": "MIT",
6 | "support": {
7 | "issues": "https://github.com/dmitry-ivanov/laravel-helper-functions/issues",
8 | "source": "https://github.com/dmitry-ivanov/laravel-helper-functions"
9 | },
10 | "authors": [{
11 | "name": "Dmitry Ivanov",
12 | "email": "dmitry.g.ivanov@gmail.com"
13 | }],
14 | "require": {
15 | "php": "^8.2",
16 | "ext-dom": "*",
17 | "ext-simplexml": "*",
18 | "illuminate/support": "^12.0",
19 | "nesbot/carbon": "^3.8.4",
20 | "symfony/filesystem": "^7.2",
21 | "symfony/finder": "^7.2",
22 | "symfony/process": "^7.2",
23 | "symfony/var-dumper": "^7.2",
24 | "spatie/array-to-xml": "^3.0"
25 | },
26 | "require-dev": {
27 | "phpunit/phpunit": "^11.5.3",
28 | "mockery/mockery": "^1.6.10",
29 | "illuminated/testing-tools": "^12.0"
30 | },
31 | "autoload": {
32 | "files": [
33 | "src/autoload.php"
34 | ],
35 | "psr-4": {
36 | "Illuminated\\Helpers\\": "src/Helpers/"
37 | }
38 | },
39 | "autoload-dev": {
40 | "psr-4": {
41 | "Illuminated\\Helpers\\Tests\\": "tests/"
42 | }
43 | },
44 | "config": {
45 | "sort-packages": true
46 | },
47 | "minimum-stability": "dev",
48 | "prefer-stable": true
49 | }
50 |
--------------------------------------------------------------------------------
/src/format.php:
--------------------------------------------------------------------------------
1 | setMaxItems(-1);
16 | $cloner->setMaxString(-1);
17 |
18 | $cloneVar = $cloner->cloneVar($var);
19 | $cloneVar = $cloneVar->withMaxDepth(50);
20 | $cloneVar = $cloneVar->withMaxItemsPerDepth(-1);
21 | $cloneVar = $cloneVar->withRefHandles(true);
22 |
23 | $dumper = new CliDumper();
24 | $dumper->setIndentPad(' ');
25 | $output = fopen('php://memory', 'r+b');
26 | $dumper->dump($cloneVar, $output);
27 |
28 | return stream_get_contents($output, -1, 0);
29 | }
30 | }
31 |
32 | if (!function_exists('format_bytes')) {
33 | /**
34 | * Format bytes into kilobytes, megabytes, gigabytes or terabytes.
35 | */
36 | function format_bytes(int $bytes, int $precision = 2): string
37 | {
38 | $units = ['B', 'KB', 'MB', 'GB', 'TB'];
39 |
40 | $bytes = max($bytes, 0);
41 | $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
42 | $pow = min($pow, count($units) - 1);
43 | $bytes /= 1024 ** $pow;
44 |
45 | return round($bytes, $precision) . ' ' . $units[$pow];
46 | }
47 | }
48 |
49 | if (!function_exists('format_xml')) {
50 | /**
51 | * Format the given XML string using new lines and indents.
52 | */
53 | function format_xml(string $xml): string
54 | {
55 | $dom = dom_import_simplexml(new SimpleXMLElement($xml))->ownerDocument;
56 |
57 | $dom->formatOutput = true;
58 | $dom->preserveWhiteSpace = false;
59 |
60 | return $dom->saveXML();
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/array.php:
--------------------------------------------------------------------------------
1 | reject(function ($item) use ($except) {
17 | return collect($except)->containsStrict($item);
18 | })
19 | ->toArray();
20 | }
21 | }
22 |
23 | if (!function_exists('multiarray_set')) {
24 | /**
25 | * Set the value for each item of the multidimensional array using "dot" notation.
26 | */
27 | function multiarray_set(array &$multiarray, mixed $key, mixed $value): array
28 | {
29 | foreach ($multiarray as &$array) {
30 | Arr::set($array, $key, $value);
31 | }
32 |
33 | return $multiarray;
34 | }
35 | }
36 |
37 | if (!function_exists('multiarray_sort_by')) {
38 | /**
39 | * Sort the multidimensional array by several fields.
40 | *
41 | * Use either SORT_ASC or SORT_DESC for `sort` arguments.
42 | */
43 | function multiarray_sort_by(array $multiarray, mixed $field1 = null, mixed $sort1 = null, mixed $_ = null): array
44 | {
45 | $arguments = collect(func_get_args());
46 |
47 | // The first argument is always the multiarray
48 | $array = $arguments->shift();
49 |
50 | // Loop through the remaining arguments and if we see a string - it is the field name.
51 | // We should replace it with the "pluck", in order to pass it later to `array_multisort`.
52 | $arguments = $arguments
53 | ->map(function ($item) use ($array) {
54 | return is_string($item) ? Arr::pluck($array, $item) : $item;
55 | })
56 | ->toArray();
57 |
58 | // Add the reference to the multiarray as the final argument
59 | $arguments[] = &$array;
60 |
61 | // Do the sorting with the composed arguments
62 | array_multisort(...$arguments);
63 |
64 | // The multiarray was passed by reference, thus it would be changed
65 | return $array;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/Helpers/Artisan/BackgroundCommand.php:
--------------------------------------------------------------------------------
1 | command = $command;
39 | $this->before = $before;
40 | $this->after = $after;
41 | }
42 |
43 | /**
44 | * Run the command in background.
45 | */
46 | public function run(): void
47 | {
48 | $command = $this->composeCommand();
49 |
50 | $command = is_windows_os()
51 | ? "start /B {$command}"
52 | : "({$command}) > /dev/null 2>&1 &";
53 |
54 | exec($command);
55 | }
56 |
57 | /**
58 | * Compose the command.
59 | */
60 | protected function composeCommand(): string
61 | {
62 | return collect()
63 | ->when($this->before, function (Collection $collection) {
64 | return $collection->push($this->before);
65 | })
66 | ->push("{$this->getPhpExecutable()} {$this->getArtisan()} {$this->command}")
67 | ->when($this->after, function (Collection $collection) {
68 | return $collection->push($this->after);
69 | })
70 | ->implode(' && ');
71 | }
72 |
73 | /**
74 | * Get the path to PHP executable.
75 | */
76 | protected function getPhpExecutable(): string
77 | {
78 | return (new PhpExecutableFinder)->find();
79 | }
80 |
81 | /**
82 | * Get the path to artisan.
83 | */
84 | protected function getArtisan(): string
85 | {
86 | return defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan';
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/src/email.php:
--------------------------------------------------------------------------------
1 | map(function (array $item) {
28 | $name = Arr::get($item, 'name');
29 | $address = Arr::get($item, 'address');
30 |
31 | if (!is_email($address)) {
32 | return false;
33 | }
34 |
35 | return $name ? "{$name} <{$address}>" : $address;
36 | })
37 | ->filter()
38 | ->implode(', ');
39 | }
40 | }
41 |
42 | if (!function_exists('to_swiftmailer_emails')) {
43 | /**
44 | * Convert addresses data to SwiftMailer-suitable format.
45 | *
46 | * @see https://swiftmailer.org/docs/messages.html
47 | */
48 | function to_swiftmailer_emails(array $addresses): array
49 | {
50 | $addresses = !empty($addresses['address']) ? [$addresses] : $addresses;
51 |
52 | return collect($addresses)
53 | ->mapWithKeys(function (array $item, int $key) {
54 | $name = Arr::get($item, 'name');
55 | $address = Arr::get($item, 'address');
56 |
57 | if (!is_email($address)) {
58 | return [];
59 | }
60 |
61 | return $name ? [$address => $name] : [$key => $address];
62 | })
63 | ->filter()
64 | ->toArray();
65 | }
66 | }
67 |
68 | if (!function_exists('to_symfony_emails')) {
69 | /**
70 | * Convert addresses data to Symfony-suitable format.
71 | *
72 | * @see https://symfony.com/doc/current/mailer.html#email-addresses
73 | */
74 | function to_symfony_emails(array $addresses): array
75 | {
76 | $addresses = !empty($addresses['address']) ? [$addresses] : $addresses;
77 |
78 | return collect($addresses)
79 | ->map(function (array $item) {
80 | $name = Arr::get($item, 'name');
81 | $address = Arr::get($item, 'address');
82 |
83 | if (!is_email($address)) {
84 | return false;
85 | }
86 |
87 | return $name ? "{$name} <{$address}>" : $address;
88 | })
89 | ->filter()
90 | ->values()
91 | ->toArray();
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 | # Laravel Helper Functions
4 |
5 | [
](https://buymeacoffee.com/dmitry.ivanov)
6 |
7 | [](https://github.styleci.io/repos/61384075?branch=master)
8 | [](https://github.com/dmitry-ivanov/laravel-helper-functions/actions?query=workflow%3Atests+branch%3Amaster)
9 | [](https://app.codecov.io/gh/dmitry-ivanov/laravel-helper-functions/tree/master)
10 |
11 | 
12 | 
13 | 
14 | 
15 |
16 | Laravel-specific and pure PHP Helper Functions.
17 |
18 | | Laravel | Helper Functions |
19 | |---------|-----------------------------------------------------------------------------|
20 | | 12.x | [12.x](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/12.x) |
21 | | 11.x | [11.x](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/11.x) |
22 | | 10.x | [10.x](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/10.x) |
23 | | 9.x | [9.x](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/9.x) |
24 | | 8.x | [8.x](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/8.x) |
25 | | 7.x | [7.x](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/7.x) |
26 | | 6.x | [6.x](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/6.x) |
27 | | 5.8.* | [5.8.*](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/5.8) |
28 | | 5.7.* | [5.7.*](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/5.7) |
29 | | 5.6.* | [5.6.*](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/5.6) |
30 | | 5.5.* | [5.5.*](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/5.5) |
31 | | 5.4.* | [5.4.*](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/5.4) |
32 | | 5.3.* | [5.3.*](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/5.3) |
33 | | 5.2.* | [5.2.*](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/5.2) |
34 | | 5.1.* | [5.1.*](https://github.com/dmitry-ivanov/laravel-helper-functions/tree/5.1) |
35 |
36 | ## Usage
37 |
38 | 1. Install the package via Composer:
39 |
40 | ```shell script
41 | composer require illuminated/helper-functions
42 | ```
43 |
44 | 2. Use any of the provided helper functions:
45 |
46 | ```php
47 | if (is_windows_os()) {
48 | call_in_background('switch-to-mac');
49 | }
50 | ```
51 |
52 | ## Available functions
53 |
54 | > Feel free to contribute.
55 |
56 | - [Array](#array)
57 | - [array_except_value](#array_except_value)
58 | - [multiarray_set](#multiarray_set)
59 | - [multiarray_sort_by](#multiarray_sort_by)
60 |
61 | - [Artisan](#artisan)
62 | - [call_in_background](#call_in_background)
63 |
64 | - [Database](#database)
65 | - [db_is_sqlite](#db_is_sqlite)
66 | - [db_is_mysql](#db_is_mysql)
67 | - [db_mysql_now](#db_mysql_now)
68 | - [db_mysql_variable](#db_mysql_variable)
69 |
70 | - [Date](#date)
71 | - [to_default_timezone](#to_default_timezone)
72 |
73 | - [Debug](#debug)
74 | - [backtrace_as_string](#backtrace_as_string)
75 | - [minimized_backtrace_as_string](#minimized_backtrace_as_string)
76 |
77 | - [Email](#email)
78 | - [is_email](#is_email)
79 | - [to_rfc2822_email](#to_rfc2822_email)
80 | - [to_swiftmailer_emails](#to_swiftmailer_emails)
81 | - [to_symfony_emails](#to_symfony_emails)
82 |
83 | - [Filesystem](#filesystem)
84 | - [relative_path](#relative_path)
85 |
86 | - [Format](#format)
87 | - [get_dump](#get_dump)
88 | - [format_bytes](#format_bytes)
89 | - [format_xml](#format_xml)
90 |
91 | - [Json](#json)
92 | - [is_json](#is_json)
93 |
94 | - [System](#system)
95 | - [is_windows_os](#is_windows_os)
96 |
97 | - [Xml](#xml)
98 | - [xml_to_array](#xml_to_array)
99 | - [array_to_xml](#array_to_xml)
100 |
101 | ## Array
102 |
103 | #### `array_except_value()`
104 |
105 | Remove the given values from the array:
106 |
107 | ```php
108 | array_except_value(['foo', 'bar', 'baz'], 'baz');
109 |
110 | // ["foo", "bar"]
111 | ```
112 |
113 | ```php
114 | array_except_value(['foo', 'bar', 'baz'], ['bar', 'baz']);
115 |
116 | // ["foo"]
117 | ```
118 |
119 | #### `multiarray_set()`
120 |
121 | Set the value for each item of the multidimensional array using "dot" notation:
122 |
123 | ```php
124 | $array = [
125 | ['name' => 'Mercedes-Benz', 'details' => ['type' => 'SUV']],
126 | ['name' => 'BMW', 'details' => ['type' => 'SUV']],
127 | ['name' => 'Porsche', 'details' => ['type' => 'SUV']],
128 | ];
129 |
130 | multiarray_set($array, 'details.country', 'Germany');
131 |
132 | // [
133 | // ["name" => "Mercedes-Benz", "details" => ["type" => "SUV", "country" => "Germany"]],
134 | // ["name" => "BMW", "details" => ["type" => "SUV", "country" => "Germany"]],
135 | // ["name" => "Porsche", "details" => ["type" => "SUV", "country" => "Germany"]],
136 | // ]
137 | ```
138 |
139 | #### `multiarray_sort_by()`
140 |
141 | Sort the multidimensional array by several fields:
142 |
143 | ```php
144 | $array = [
145 | ['name' => 'Mercedes-Benz', 'model' => 'GLS', 'price' => 120000],
146 | ['name' => 'Mercedes-Benz', 'model' => 'GLE Coupe', 'price' => 110000],
147 | ['name' => 'BMW', 'model' => 'X6', 'price' => 77000],
148 | ['name' => 'Porsche', 'model' => 'Cayenne', 'price' => 117000],
149 | ];
150 |
151 | $sorted = multiarray_sort_by($array, 'name', 'model');
152 |
153 | // [
154 | // ["name" => "BMW", "model" => "X6", "price" => 77000],
155 | // ["name" => "Mercedes-Benz", "model" => "GLE Coupe", "price" => 110000],
156 | // ["name" => "Mercedes-Benz", "model" => "GLS", "price" => 120000],
157 | // ["name" => "Porsche", "model" => "Cayenne", "price" => 117000],
158 | // ]
159 | ```
160 |
161 | Also, you can change the sort order:
162 |
163 | ```php
164 | $array = [
165 | ['name' => 'Mercedes-Benz', 'model' => 'GLS', 'price' => 120000],
166 | ['name' => 'Mercedes-Benz', 'model' => 'GLE Coupe', 'price' => 110000],
167 | ['name' => 'BMW', 'model' => 'X6', 'price' => 77000],
168 | ['name' => 'Porsche', 'model' => 'Cayenne', 'price' => 117000],
169 | ];
170 |
171 | $sorted = multiarray_sort_by($array, 'name', SORT_ASC, 'model', SORT_DESC);
172 |
173 | // [
174 | // ["name" => "BMW", "model" => "X6", "price" => 77000],
175 | // ["name" => "Mercedes-Benz", "model" => "GLS", "price" => 120000],
176 | // ["name" => "Mercedes-Benz", "model" => "GLE Coupe", "price" => 110000],
177 | // ["name" => "Porsche", "model" => "Cayenne", "price" => 117000],
178 | // ]
179 | ```
180 |
181 | ## Artisan
182 |
183 | #### `call_in_background()`
184 |
185 | Call the given artisan console command in background.
186 |
187 | Code execution continues immediately, without waiting for results.
188 |
189 | ```php
190 | call_in_background('report');
191 |
192 | // "php artisan report" would be called in background
193 | ```
194 |
195 | Optional `before` and `after` sub-commands could be used:
196 |
197 | ```php
198 | call_in_background('report:monthly subscriptions', 'sleep 0.3');
199 |
200 | // "sleep 0.3 && php artisan report:monthly subscriptions" would be called in background
201 | ```
202 |
203 | ## Database
204 |
205 | #### `db_is_sqlite()`
206 |
207 | Check whether the default database connection driver is `sqlite` or not:
208 |
209 | ```php
210 | db_is_sqlite();
211 |
212 | // false
213 | ```
214 |
215 | #### `db_is_mysql()`
216 |
217 | Check whether the default database connection driver is `mysql` or not:
218 |
219 | ```php
220 | db_is_mysql();
221 |
222 | // true
223 | ```
224 |
225 | #### `db_mysql_now()`
226 |
227 | Get the current MySQL datetime:
228 |
229 | ```php
230 | db_mysql_now();
231 |
232 | // "2020-05-25 20:09:33"
233 | ```
234 |
235 | #### `db_mysql_variable()`
236 |
237 | Get value of the specified MySQL variable:
238 |
239 | ```php
240 | db_mysql_variable('hostname');
241 |
242 | // "localhost"
243 | ```
244 |
245 | ## Date
246 |
247 | #### `to_default_timezone()`
248 |
249 | Convert the given datetime to the default timezone (see `app.timezone` config):
250 |
251 | ```php
252 | to_default_timezone('2017-02-28T14:05:01Z');
253 |
254 | // "2017-02-28 16:05:01", assuming that `app.timezone` is "Europe/Kiev"
255 | ```
256 |
257 | ## Debug
258 |
259 | #### `backtrace_as_string()`
260 |
261 | Get backtrace without arguments, as a string:
262 |
263 | ```php
264 | $backtrace = backtrace_as_string();
265 |
266 | #0 backtrace_as_string() called at [/htdocs/example/routes/web.php:15]
267 | #1 Illuminate\Routing\Router->{closure}() called at [/htdocs/example/vendor/laravel/framework/src/Illuminate/Routing/Route.php:189]
268 | #2 Illuminate\Foundation\Http\Kernel->handle() called at [/htdocs/example/public/index.php:53]
269 | ```
270 |
271 | #### `minimized_backtrace_as_string()`
272 |
273 | Get minimized backtrace, as a string:
274 |
275 | ```php
276 | $backtrace = minimized_backtrace_as_string();
277 |
278 | #0 /htdocs/example/routes/web.php:15
279 | #1 /htdocs/example/vendor/laravel/framework/src/Illuminate/Routing/Route.php:189
280 | #2 /htdocs/example/public/index.php:53
281 | ```
282 |
283 | ## Email
284 |
285 | #### `is_email()`
286 |
287 | Check whether the given string is an email address or not:
288 |
289 | ```php
290 | is_email('john.doe@example.com');
291 |
292 | // true
293 | ```
294 |
295 | #### `to_rfc2822_email()`
296 |
297 | Convert addresses data to [RFC 2822](http://faqs.org/rfcs/rfc2822.html) string, suitable for PHP [mail()](https://php.net/manual/en/function.mail.php) function:
298 |
299 | ```php
300 | to_rfc2822_email([
301 | ['address' => 'john.doe@example.com', 'name' => 'John Doe'],
302 | ['address' => 'jane.smith@example.com'],
303 | ]);
304 |
305 | // "John Doe , jane.smith@example.com"
306 | ```
307 |
308 | Also, it supports simplified syntax for a single address:
309 |
310 | ```php
311 | to_rfc2822_email(['address' => 'john.doe@example.com', 'name' => 'John Doe']);
312 |
313 | // "John Doe "
314 | ```
315 |
316 | #### `to_swiftmailer_emails()`
317 |
318 | Convert addresses data to [SwiftMailer-suitable format](https://swiftmailer.org/docs/messages.html):
319 |
320 | ```php
321 | to_swiftmailer_emails([
322 | ['address' => 'john.doe@example.com', 'name' => 'John Doe'],
323 | ['address' => 'jane.smith@example.com'],
324 | ]);
325 |
326 | // ["john.doe@example.com" => "John Doe", "jane.smith@example.com"]
327 | ```
328 |
329 | Also, it supports simplified syntax for a single address:
330 |
331 | ```php
332 | to_swiftmailer_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe']);
333 |
334 | // ["john.doe@example.com" => "John Doe"]
335 | ```
336 |
337 | #### `to_symfony_emails()`
338 |
339 | Convert addresses data to [Symfony-suitable format](https://symfony.com/doc/current/mailer.html#email-addresses):
340 |
341 | ```php
342 | to_symfony_emails([
343 | ['address' => 'john.doe@example.com', 'name' => 'John Doe'],
344 | ['address' => 'jane.smith@example.com'],
345 | ]);
346 |
347 | // ["John Doe ", "jane.smith@example.com"]
348 | ```
349 |
350 | Also, it supports simplified syntax for a single address:
351 |
352 | ```php
353 | to_symfony_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe']);
354 |
355 | // ["John Doe "]
356 | ```
357 |
358 | ## Filesystem
359 |
360 | #### `relative_path()`
361 |
362 | Get a relative path for the given folders:
363 |
364 | ```php
365 | relative_path('/var/www/htdocs', '/var/www/htdocs/example');
366 |
367 | // "../"
368 | ```
369 |
370 | You can pass the relative path as a parameter too:
371 |
372 | ```php
373 | relative_path('/var/www/htdocs/example/public/../../', '/var/');
374 |
375 | // "www/htdocs/"
376 | ```
377 |
378 | ## Format
379 |
380 | #### `get_dump()`
381 |
382 | Get a nicely formatted string representation of the variable, using the [Symfony VarDumper Component](https://symfony.com/doc/current/components/var_dumper/introduction.html):
383 |
384 | ```php
385 | $array = [
386 | 'a simple string' => 'Hello!',
387 | 'a float' => 1.0,
388 | 'an integer' => 1,
389 | 'a boolean' => true,
390 | 'an empty array' => [],
391 | ];
392 |
393 | $dump = get_dump($array);
394 |
395 | // array:5 [
396 | // "a simple string" => "Hello!"
397 | // "a float" => 1.0
398 | // "an integer" => 1
399 | // "a boolean" => true
400 | // "an empty array" => []
401 | // ]
402 | ```
403 |
404 | #### `format_bytes()`
405 |
406 | Format bytes into kilobytes, megabytes, gigabytes or terabytes:
407 |
408 | ```php
409 | format_bytes(3333333);
410 |
411 | // "3.18 MB"
412 | ```
413 |
414 | #### `format_xml()`
415 |
416 | Format the given XML string using new lines and indents:
417 |
418 | ```php
419 | format_xml('JohnJaneGo to the shopJohnPaulFinish the reportJaneJeffClean the house');
420 |
421 | //
422 | //
423 | //
424 | // John
425 | // Jane
426 | // Go to the shop
427 | //
428 | //
429 | // John
430 | // Paul
431 | // Finish the report
432 | //
433 | //
434 | // Jane
435 | // Jeff
436 | // Clean the house
437 | //
438 | //
439 | ```
440 |
441 | ## Json
442 |
443 | #### `is_json()`
444 |
445 | Check whether the given value is a valid JSON-encoded string or not:
446 |
447 | ```php
448 | is_json('{"foo":1,"bar":2,"baz":3}');
449 |
450 | // true
451 | ```
452 |
453 | It returns decoded JSON if you pass `true` as a second argument:
454 |
455 | ```php
456 | is_json('{"foo":1,"bar":2,"baz":3}', true);
457 |
458 | // ["foo" => 1, "bar" => 2, "baz" => 3]
459 | ```
460 |
461 | ## System
462 |
463 | #### `is_windows_os()`
464 |
465 | Check whether the operating system is Windows or not:
466 |
467 | ```php
468 | is_windows_os();
469 |
470 | // false
471 | ```
472 |
473 | ## Xml
474 |
475 | #### `xml_to_array()`
476 |
477 | Convert the given XML to array:
478 |
479 | ```php
480 | xml_to_array('
481 |
482 |
483 | Luke Skywalker
484 | Lightsaber
485 |
486 |
487 | Sauron
488 | Evil Eye
489 |
490 |
491 | ');
492 |
493 | // [
494 | // "Good_guy" => [
495 | // "name" => "Luke Skywalker",
496 | // "weapon" => "Lightsaber",
497 | // "@attributes" => [
498 | // "Rating" => "100",
499 | // ],
500 | // ],
501 | // "Bad_guy" => [
502 | // "name" => "Sauron",
503 | // "weapon" => "Evil Eye",
504 | // "@attributes" => [
505 | // "Rating" => "90",
506 | // ],
507 | // ],
508 | // ]
509 | ```
510 |
511 | Alternatively, you can pass an instance of the `SimpleXMLElement` class instead of a string.
512 |
513 | #### `array_to_xml()`
514 |
515 | Convert the given array to XML string:
516 |
517 | ```php
518 | $array = [
519 | 'Good guy' => [
520 | 'name' => 'Luke Skywalker',
521 | 'weapon' => 'Lightsaber',
522 | '@attributes' => [
523 | 'Rating' => '100',
524 | ],
525 | ],
526 | 'Bad guy' => [
527 | 'name' => 'Sauron',
528 | 'weapon' => 'Evil Eye',
529 | '@attributes' => [
530 | 'Rating' => '90',
531 | ],
532 | ]
533 | ];
534 |
535 | $xml = array_to_xml($array, 'Guys');
536 |
537 | //
538 | //
539 | //
540 | // Luke Skywalker
541 | // Lightsaber
542 | //
543 | //
544 | // Sauron
545 | // Evil Eye
546 | //
547 | //
548 | ```
549 |
550 | ## Sponsors
551 |
552 | [](https://laravel-idea.com)
553 | [](https://material-theme.com)
554 |
555 | ## License
556 |
557 | Laravel Helper Functions is open-sourced software licensed under the [MIT license](LICENSE.md).
558 |
559 | [
](https://buymeacoffee.com/dmitry.ivanov)
560 |
--------------------------------------------------------------------------------