PHP code example of mekramy / php-util

1. Go to this page and download the library: Download mekramy/php-util library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mekramy / php-util example snippets


use MEkramy\PHPUtil\Helpers;

$res = Helpers::quickSwitch([
    'First Value' => false,
    'Second Value' => function(){
        return false;
    },
    'Third Value' => falsyFunction(),
    'True Option' => true
], 'Default Value');
echo $res; # > "True Option"

$res = Helpers::quickSwitch([
    'First Value' => false,
    'Second Value' => function(){
        return false;
    },
    'Third Value' => falsyFunction(),
], 'Default Value');
echo $res; # > "Default Value"

static function toPersianDate($date = null, ?string $format = 'Y-m-d H:i:s')

use MEkramy\PHPUtil\Helpers;

$gregorian_date = '2019-03-21';
$res = Helpers::toPersianDate($gregorian_date); # > 1398-01-01 00:00:00
$res = Helpers::toPersianDate($gregorian_date, 'Y'); # > 1398
$res = Helpers::toPersianDate($gregorian_date, null); # > \Hekmatinasser\Verta\Verta instance

static function toPersianDate($date = null, ?string $format = 'Y-m-d H:i:s')

use MEkramy\PHPUtil\Helpers;

$persian_date = '1398-01-01';
$res = Helpers::toGregorianDate($persian_date); # > 2019-03-21 00:00:00
$res = Helpers::toGregorianDate($persian_date, 'Y'); # > 2019
$res = Helpers::toGregorianDate($persian_date, null); # > \Carbon\Carbon instance

static function validateOrDefault($value, ?array $allowed, $default = null)

use MEkramy\PHPUtil\Helpers;

$res = Helpers::validateOrDefault(null, null, 'default'); # > "default"
$res = Helpers::validateOrDefault("my val", null, 'default'); # > "my val"
$res = Helpers::validateOrDefault("test", ["first valid value", "second"], 'default'); # > "default"

public static function validateNumberOrDefault($value, bool $float = false, $min = null, $max = null, ?array $allowed = null, $default = null)

use MEkramy\PHPUtil\Helpers;

$res = Helpers::validateNumberOrDefault(12, false, null, null, null, 0); # > 12
$res = Helpers::validateNumberOrDefault(12, true, 12.001, null, null, 0); # > 0
$res = Helpers::validateNumberOrDefault("12.03", true, 11.99, 12.99, null, 0); # > 12.03
$res = Helpers::validateNumberOrDefault(7, true, null, null, [1, 2, 3, 4], 0); # > 0

use MEkramy\PHPUtil\Helpers;

$res = Helpers::asBoolean("1"); # > true
$res = Helpers::asBoolean(true); # > true
$res = Helpers::asBoolean("on"); # > true
$res = Helpers::asBoolean("yes"); # > true

$res = Helpers::asBoolean(null); # > false
$res = Helpers::asBoolean("-"); # > false

use MEkramy\PHPUtil\Helpers;

$res = Helpers::extractNumbers("0123- 456"); # > "0123456"
$res = Helpers::extractNumbers("1 this is a text 2 contains3 string"); # > "123"
$res = Helpers::extractNumbers("text with no numbers"); # > ""

use MEkramy\PHPUtil\Helpers;

$res = Helpers::formatString("{0} - {1}", ['abc', 'def']); # > "abc - def"
$res = Helpers::formatString("{first} {last}", ['first' => 'John', 'last' => 'Doe']); # > "John Doe"
$res = Helpers::formatString("you search {search} and we found no result for {search}", ['search' => 'dummy']); # > "you search dummy and we found no result for dummy"

use MEkramy\PHPUtil\Helpers;

$a = [
    "first_name" => "john",
    "last_name" => "doe",
    "address" => [
        "city" => "Somewhere",
        "street" => null
    ]
];

Helpers::printDebug($a, "john doe info", "*", 75);

# ****************************** JOHN DOE INFO ******************************
# {
#     "first_name": "john",
#     "last_name": "doe",
#     "address": {
#         "city": "Somewhere",
#         "street": null
#     }
# }
# ***************************************************************************

Helpers::printDebug("simple value", "simple", "-", 25);

# -------- SIMPLE ---------
# "simple value"
# -------------------------

Helpers::printDebug(pi());

# ===================== DEBUG ======================
# 3.141592653589793
# ==================================================