sorokinmedia/yii2-helpers

Yii2 帮助工具

安装: 458

依赖项: 8

建议者: 0

安全: 0

星标: 2

关注者: 7

分支: 0

开放性问题: 0

类型:yii2-extension

dev-master 2020-01-07 15:10 UTC

This package is auto-updated.

Last update: 2024-09-08 01:55:26 UTC


README

Total Downloads

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 中描述已缓存的方法