moitran/php-common-funcs

编写有用的PHP常用函数

v1.0.1 2018-12-18 07:39 UTC

This package is auto-updated.

Last update: 2024-09-18 20:46:03 UTC


README

编写有用的PHP常用函数

Latest Stable Version Latest Unstable Version Build Status codecov License composer.lock

安装

$ composer require moitran/php-common-funcs

它有什么?

  • 数组

    • groupItemsByKeys 根据列表键值将多维数组中的项目分组。
    • isAssocArray 检查数组输入是否为关联数组列出数组类型
    • isNumericArray 检查数组输入是否为数字数组列出数组类型
    • convertLowerUpperCase 将数组中所有字符串值转换为小写或大写。
    • sortBy 根据列表键对多维数组进行排序。
  • 日期时间

    • getCurrentTime 获取当前时间,带有格式参数。
    • getNow 返回当前Unix时间戳。
    • format 接收日期字符串参数,并以指定格式返回。
    • getListRangeDate 返回指定格式下两个日期之间的日期列表。
    • getPreviousDates 返回指定格式下具有参数的天数之前的日期。
    • getPreviousDateRange 返回与日期范围参数相反的先前日期列表。
    • getTimeBetweenTwoDate 返回两个日期之间的秒数(S)、分钟(M)、小时(H)或天数(D)。
    • getAge 通过出生日期返回年龄。
    • niceTime 返回美观的时间格式。

最佳实践(个人看法)

此包中的所有函数都具有静态方法,因此如果我们以这种方式调用函数,将很难编写phpunit测试代码。

use MoiTran\CommonFunctions\DateCommonFunctions;

class A {
    public function doSomeThing() {
        $time = DateCommonFunctions::getCurrentTime('Y/m/d');
        // do some thing
    }
}

解决方案是使用此特征StaticCalling来调用这些静态函数。代码如下所示

use MoiTran\CommonFunctions\DateCommonFunctions;
use MoiTran\CommonFunctions\StaticCalling;

class A {
    use StaticCalling;

    public function doSomeThing() {
        $time = $this->callStatic(DateCommonFunctions::class, DateCommonFunctions::GET_CURRENT_TIME_FUNC, ['Y/m/d']);
        // do some thing
    }
}

许可证

本软件包采用MIT许可证