sorokinmedia / yii2-helpers
Yii2 帮助工具
dev-master
2020-01-07 15:10 UTC
Requires
- ext-intl: *
- yiisoft/yii2: >=2.0.30
Requires (Dev)
- phpunit/phpunit: 8.*
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2024-09-08 01:55:26 UTC
README
Sorokin.Media 仓库
该组件包含多个帮助器,可以在项目中使用。
ArrayHelper
方法列表
public static function convertArrayToArrayOfObject(array $array, string $type = 'int') : array {}- 将键值对数组转换为对象数组 {'id', 'name'}public static function costHourValues() : array {}- 为薪酬模块生成时间间隔选择列表
DateHelper
常量列表
const TIME_SECOND_ONE- 一秒钟const TIME_MINUTE_ONE- 一分钟const TIME_HOUR_ONE- 一小时const TIME_HOUR_TWELVE- 12小时const TIME_DAY_ONE- 一天const TIME_DAY_FIFTEEN- 15天const TIME_DAY_THIRTY- 30天const TIME_YEAR_ONE- 一年const WEEKEND_DAYS- 周末天数数组
方法列表
public static function getExtendTime(int $extend_time) : int- 考虑周末后得到的小时数public static function getListOfTimezones(string $locale = '') : array- 时间区域列表public static function getLeftTime(int $time = null)- 将秒数(Unix时间戳)转换为文本(1小时20分钟)public static function getTzOffset(string $timezone) : int- 获取时间区域相对于UTC的偏移量(秒)public static function hoursArray() : array- 为表单中的选择生成时间间隔列表public static function getStartEndMonth() : array- 月份的第一天和最后一天public static function getStartEndPrevMonth() : array- 上个月的第一天和最后一天
PluralHelper
方法列表
public static function convert(int $n, string $type = "hours") : string- 将数字转换为单词,使用正确的词形。支持的选项days- 天months- 月years- 年hours- 时minutes- 分seconds- 秒text- 文本notification- 通知tests- 测试tasks- 任务rubl- 卢布
TextHelper
方法列表
public static function translitString(string $string) : string- 字符串转写public static function autop( $pee, $br = true )- WordPress 标签包装器public static function checkCardNumber(string $str) : bool- 使用Luna方法检查卡号public static function clearText(string $text = null) : string- 清除文本中的空格和标签public static function clearTextAllowedTags(string $text = null) : string- 清除文本中的标签,保留指定的标签public static function trimToLowerText($text) : string- 移除空格并将其转换为小写public static function array2string(array $array) : string- 将数组转换为字符串(用于JSON)public static function getCountLinkPost(string $text) : int- 计算文本中a标签的数量public static function filterResponse(string $response) : string- 将JSON响应转换为字符串(用于API测试)public static function makeUrls(string $text) : string- 在文本中自动添加链接标签public static function removeNbsp(string $text) : string- 清除文本中的连续空格(nbsp)
TestHelper
方法列表
public static function filterResponse(string $response) : string- 将API响应转换为所需形式。用于API测试
StatsHelper
方法列表
public static function makeInt(array $array) : array- 将数组值转换为整数(int)public static function makeFloat(array $array) : array- 将数组值转换为浮点数(float)public static function makeRound(array $array) : array- 将数组中的所有值四舍五入到两位小数public static function makeMinutes(array $array) : array- 将秒转换为分钟public static function date_range(string $from, string $to, string $step = '+1 day', string $output_format = 'd-m-Y' ) : array- 生成指定区间的日期数组
CacheHelper
### 缓存操作
要将API响应封装到缓存中,需要执行以下操作:
-
根据请求参数确定一个唯一的键,例如
"User.$user->id.new-messages" -
在API方法中调用主要功能之前,添加检查缓存存在的步骤
$cache_key = "User.$user->id.new-messages";
if (Yii::$app->cache->exists($cache_key)){
return new ApiAnswerLogFromCache(null, Yii::$app->cache->get($cache_key));
}
- 在主要功能方法发送响应后,添加将响应添加到缓存的步骤
Yii::$app->cache->set($cache_key, $messages, CacheHelper::CACHE_TIME_FIVE_MINUTES);
return new ApiAnswerLogInsertCache(null, $messages);
-
在
CacheHelper中添加一个根据键清理缓存的方法 -
在需要的地方添加清理缓存方法的调用
-
在本地
readme.MD中描述已缓存的方法