A bundle of helpers for develop, contains method for generate excels , csv, array optimizations and more
composer require sefirosweb/laravel-general-helper
For save file need a run migration for manage who has created the file, only have access own creator,
php artisan migrate
You can change the middleware and prefix path for get the files, need publish config
php artisan vendor:publish --provider="Sefirosweb\LaravelGeneralHelper\LaravelGeneralHelperServiceProvider"
Helpers
- pathTemp
- array_group_by
- array_group_by_multidimensional
- objectToArray
- generateMarks
- createMarks
- char_at
- mergeArrays
- mergeArraysOnSubArray
- query
- excelToArray
- saveCsvInServerAndDownload
- saveCsvInServer
- validateArray
- saveExcelInServer
- saveExcelInServerAndDownload
- br2nl
- eliminar_tildes
Helper class
Obtain and create the path "tmp" if it does not exist, it is created automatically, in this path the temporary files used for this package are stored
$path = pathTemp();
// $path = /var/www/html/storage/tmp
Group an array by "identifier", if the third optional value is true, only get the first element of group
array_group_by(array $array, string $key, bool $onlyFirstValue = false)
$array = [
[
'name' => 'pablo',
'date' => '02'
],
[
'name' => 'pablo',
'date' => '03'
],
[
'name' => 'selena',
'date' => 'XX'
]
];
$arrayAgrouped = array_group_by($array, 'name');
// Returns:
[
'pablo' => [
[
'name' => 'pablo',
'date' => '02',
],
[
'name' => 'pablo',
'date' => '03',
]
],
'selena' => [
[
'name' => 'selena',
'date' => 'XX'
]
]
]
$arrayAgrouped = array_group_by($array, 'name', true);
[
'pablo' =>
[
'name' => 'pablo',
'date' => '02',
],
'selena' =>
[
'name' => 'selena',
'date' => 'XX'
]
]
Same as function "array_group_by" but in this case you can sub-group the array in multiple conditions
array_group_by_multidimensional(array $array, array $union_by, bool $onlyFirstValue = false)
$array = [
[
'name' => 'pablo',
'date' => '02'
],
[
'name' => 'pablo',
'date' => '03'
],
[
'name' => 'selena',
'date' => 'XX'
]
];
$arrayAgrouped = array_group_by_multidimensional($array, ['name','date']);
// Returns:
[
'pablo' => [
'02' => [ // <--- sub levels, you can add all levels of you need
[
'name' => 'pablo',
'date' => '02',
],
],
'03' => [
[
'name' => 'pablo',
'date' => '03',
]
]
],
'selena' => [
'XX' => [
[
'name' => 'selena',
'date' => 'XX'
]
]
]
]
$arrayAgrouped = array_group_by_multidimensional($array, ['name','date'], true);
// Returns:
[
'pablo' => [
'02' =>
[ // Only returns the first item in level of 02
'name' => 'pablo',
'date' => '02',
],
'03' =>
[
'name' => 'pablo',
'date' => '03',
]
],
'selena' => [
'XX' =>
[
'name' => 'selena',
'date' => 'XX'
]
]
]
Convert stdClass object to full array values, it is required for use the array group functions
$var = new stdClass();
$var->field = 5;
$array = objectToArray(object $var);
// $array = [
// 'field' => 5;
// ]
TODO
TODO
TODO
TODO
TODO
TODO
TODO
TODO
TODO
Function for save an structured array data into csv, and returns a object "FileSaved"
saveCsvInServer(array $arrayData, string $fileName, string $delimiter = ';', string $enclosure = '"', bool $latingMode = false, bool $headers = true, bool $utf8_decode = false, bool $enclosureAll = false)
$array = [
[
'name' => 'pablo',
'date' => '02'
],
[
'name' => 'pablo',
'date' => '03'
],
[
'name' => 'selena',
'date' => 'XX'
]
];
$file = saveCsvInServer($array, 'filename');
// Returns a SavedFile model object
$file->id
$file->path
...
Function for save an structured array data into excel (for huge data is recommended CSV, save data in excel have low performance)
$array = [
[
'name' => 'pablo',
'date' => '02'
],
[
'name' => 'pablo',
'date' => '03'
],
[
'name' => 'selena',
'date' => 'XX'
]
];
$file = saveCsvInServer($array, 'filename');
// Returns a SavedFile model object
$file->id
$file->path
...
TODO
Replace <br> into new lines "\n"
$resultString = br2nl('hello<br>world');
// $resultString = 'hello\nworld'
Function for remove the latin characters "á', 'à', 'ä', 'â', 'ª', 'Á', 'À', 'Â', 'Ä'" into "a / A"
$resultString = eliminar_tildes('Camión');
// $resultString = 'Camion'
Store in memory for the current request some data, this can be stored and retrieved from any code of your app, usefull to avoid execute multiple queries and don't want to store in some cache system
// Store data
CacheRequest::set('anyKey', $anyData);
// Retrieve data
$retrieveData = CacheRequest::get('anyKey');
// Hot cache data from function, if key not exists then execute the function to generate them
$retrieveData = CacheRequest::remember('anyKey', function () {
// .. do something
return 'return any data';
});
// Delete cache
CacheRequest::delete('anyKey');
Similar to RequestCache, but ther store data in to Redis cache system:
// Store data
RedisHelper::set('anyKey', $anyData, $timeout);
// Retrieve datacall($function, $key = '', $prod = false, $EX = 86400)
$retrieveData = RedisHelper::get('anyKey');
// Hot cache data from function, if key not exists then execute the function to generate them, for default is not executed in production
$retrieveData = RedisHelper::call(function(){
// .. do something
return 'return any data';
}, 'anyKey', $executeInProduction, $timeout);
// Delete cache
RedisHelper::delete('anyKey')