sefirosweb / laravel-general-helper
开发辅助工具包,包含生成 Excel、CSV、数组优化等功能
1.5.5
2024-06-10 09:06 UTC
Requires
- barryvdh/laravel-dompdf: ^2.0.1
- phpoffice/phpspreadsheet: ^1.28
README
开发辅助工具包,包含生成 Excel、CSV、数组优化等功能
安装
composer require sefirosweb/laravel-general-helper
为了保存文件,需要运行迁移来管理谁创建了文件,只有创建者可以访问自己的文件。
php artisan migrate
可选配置
您可以根据需要更改中间件和前缀路径来获取文件,需要发布配置。
php artisan vendor:publish --provider="Sefirosweb\LaravelGeneralHelper\LaravelGeneralHelperServiceProvider"
辅助工具
- 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
辅助类
辅助工具
pathTemp
如果不存在,则创建并获取路径 "tmp",在此路径中存储此包使用的临时文件
$path = pathTemp(); // $path = /var/www/html/storage/tmp
array_group_by
根据 "标识符" 对数组进行分组,如果第三个可选值为 true,则只获取组中的第一个元素
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' ] ]
array_group_by_multidimensional
类似于 "array_group_by" 函数,但在此情况下,您可以在多个条件下对数组进行子分组
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' ] ] ]
objectToArray
将 stdClass 对象转换为完整的数组值,这是使用数组分组函数所必需的
$var = new stdClass(); $var->field = 5; $array = objectToArray(object $var); // $array = [ // 'field' => 5; // ]
generateMarks
待办事项
createMarks
待办事项
char_at
待办事项
validateArray
待办事项
mergeArrays
待办事项
mergeArraysOnSubArray
待办事项
query
待办事项
excelToArray
待办事项
saveCsvInServerAndDownload
待办事项
saveCsvInServer
将结构化数组数据保存到 CSV 的函数,并返回一个 "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 ...
saveExcelInServer
将结构化数组数据保存到 Excel 的函数(对于大量数据建议使用 CSV,将数据保存到 Excel 性能较低)
$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 ...
saveExcelInServerAndDownload
待办事项
br2nl
将 <br> 替换为新行 "\n"
$resultString = br2nl('hello<br>world'); // $resultString = 'hello\nworld'
eliminar_tildes
将拉丁字符 "á', 'à', 'ä', 'â', 'ª', 'Á', 'À', 'Â', 'Ä'" 移除并转换为 "a / A"
$resultString = eliminar_tildes('Camión'); // $resultString = 'Camion'
辅助类
RequestCache
在内存中存储 当前请求 的一些数据,这可以在您的应用程序的任何代码中存储和检索,用于避免执行多个查询,而不想将数据存储在某个缓存系统中
// 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');
RedisHelper
类似于 RequestCache,但将数据存储到 Redis 缓存系统中
// 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')