zsolt / guard-clauses

此软件包最新版本(1.0.0)没有可用的许可信息。

PHP的守卫子句

1.0.0 2022-09-28 21:41 UTC

This package is auto-updated.

Last update: 2024-09-29 02:13:35 UTC


README

一个包含守卫子句的简单软件包。
守卫子句是一种软件模式,它通过“快速失败”,检查无效输入并在发现任何问题时立即失败来简化复杂函数。

安装

composer require zsolt/guard-clauses

点赞!⭐

如果您喜欢或正在使用此项目,请给它点赞。谢谢!

使用方法

function sendEmailToCustomer(Customer $customer): void
{
    $email = GuardAgainst::null($customer->getEmail());

    // ...
}

final class Customer
{
    private string $firstName;
    private string $lastName;
    private int $age;
    private string $login;
    private ?string $email;

    function __construct(
        string $firstName,
        string $lastName,
        int $age,
        string $login,
        ?string $email = null
    )
    {
        $this->firstName = GuardAgainst::emptyOrWhiteSpace($firstName);
        $this->lastName = GuardAgainst::emptyOrWhiteSpace($lastName);
        $this->age = GuardAgainst::negative($age);
        $this->login = GuardAgainst::match($login, "admin", "login", "You can not be an admin");
        $this->email = $email;
    }
}

支持的守卫子句

GuardAgainst::null(); // Guard against null values
GuardAgainst::true(); // Guard against true values
GuardAgainst::nullOrTrue(); // Guard against null or true values
GuardAgainst::false(); // Guard against false values
GuardAgainst::nullOrFalse(); // Guard against null or false values
GuardAgainst::negative(); // Guard against numbers less than zero
GuardAgainst::positive(); // Guard against numbers more than zero
GuardAgainst::zero(); // Guard against the number zero
GuardAgainst::negativeOrZero(); // Guard against numbers less than or equal to zero
GuardAgainst::positiveOrZero(); // Guard against numbers more than or equal to zero
GuardAgainst::match(); // Guard against values that loosely match a specified value
GuardAgainst::strictMatch(); // Guard against values that strictly match a specified value
GuardAgainst::range(); // Guard against values that are in the specified range
GuardAgainst::notRange(); // Guard against values that are NOT in the specified range
GuardAgainst::empty(); // Guard against empty values
GuardAgainst::whiteSpace(); // Guard against string that contains only one white space
GuardAgainst::emptyOrWhiteSpace(); // Guard against string that are empty or contains only one white space
GuardAgainst::regex(); // Guard against string that matches the regex pattern
GuardAgainst::count(); // Guard against countable that matches a specific count
GuardAgainst::countOrMore(); // Guard against countable that is MORE or matches a specific count
GuardAgainst::countOrLess(); // Guard against countable that is LESS or matches a specific count
GuardAgainst::arrayHasValue(); // Guard against a value in the array
GuardAgainst::arrayHasNoValue(); // Guard against a value NOT in the array
GuardAgainst::type(); // Guard against the specified type type (gettype)
GuardAgainst::notType(); // Guard against not the specified type (gettype)
GuardAgainst::hasProperty(); // Guard against a property existing on an object
GuardAgainst::hasNoProperty(); // Guard against a property NOT existing on an object
GuardAgainst::expression(); // Guard against the expression returning true