Skip to content

Latest commit

 

History

History
65 lines (55 loc) · 3.19 KB

README.md

File metadata and controls

65 lines (55 loc) · 3.19 KB

php-common-funs

Writing useful common functions in php

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

Installation

$ composer require moitran/php-common-funcs

What does it have?

  • Array

    • groupItemsByKeys Group items in multidimensional array by values of list keys argument.
    • isAssocArray Checking array input is assoc array or not List types of array.
    • isNumericArray Checking array input is numeric array or not List types of array.
    • convertLowerUpperCase Convert all string values in array to lower case or upper case.
    • sortBy Sorting multidimensional array by list keys.
  • DateTime

    • getCurrentTime Get current time with format argument
    • getNow Return current Unix timestamp
    • format Receive an date string argument and return in the specified format.
    • getListRangeDate Return list dates between two dates in the specified format.
    • getPreviousDates Return previous date in the specified format with argument is number of previous date.
    • getPreviousDateRange Return list previous range dates opposite with date range argument.
    • getTimeBetweenTwoDate Return number of Seconds (S), Minutes (M), Hours (H) or Days (D) between two dates.
    • getAge Return age by date of birth.
    • niceTime Return nice time format.

Best practice to use (*IMO)

All functions in this package have static method so that if we call functions redirect like this way. It will be so hard to write the phpunit test code.

use MoiTran\CommonFunctions\DateCommonFunctions;

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

The solution for it is using this trait StaticCalling to call those static functions. The code will be like this one

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
    }
}

License

This package is under MIT License