growthops/ext-php

此包包含各种用于php的实用函数

v1.0.5 2023-06-28 01:52 UTC

This package is auto-updated.

Last update: 2024-08-28 04:41:20 UTC


README

此包包含各种用于php的实用函数。

 

安装

composer require growthops/ext-php

 

函数

 

函数: isNull()

此函数返回内置php函数 is_null() 的值

if (! function_exists('isNull')) {
    function isNull(mixed $value)
    {
        return is_null($value);
    }
}

 

函数: notNull()

此函数返回内置php函数 is_null() 的相反值

if (! function_exists('notNull')) {
    function notNull(mixed $value)
    {
        return ! is_null($value);
    }
}

 

函数: randomElements()

此函数从元素数组中返回一个随机元素。如果没有提供数组参数,则函数将使用预定义的数组。

if (! function_exists('randomElements')) {
    function randomElements(int $length, ?array $repository = null)
    {
        if (isNull($repository)) {
            $repository = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9'];
        }

        $randomElements = [];
        $max = count($repository) - 1;

        for ($x = 0; $x < $length; $x++) {
            $randomElements[] = $repository[random_int(0, $max)];
        }

        return $randomElements;
    }
}

 

特性

特性: Composable

此特性允许在模型不存在的情况下实例化模型。它还允许在单个方法中保存并返回模型。

public static function compose(?self $model = null): self
{
    if (notNull($model)) {
        return $model;
    }

    return new self();
}
public function commit(): self
{
    $this->save();

    return $this;
}