zheltikov / php-invariant
1.2.3
2021-08-25 09:39 UTC
Requires
- php: >=7.4
- zheltikov/php-exceptions: ^1.0
Requires (Dev)
Suggests
- zheltikov/php-type-assert: Provides type checking/assertion functionality.
This package is auto-updated.
Last update: 2024-08-25 16:19:53 UTC
README
又一个断言库。
安装
您可以通过composer安装此库
$ composer require zheltikov/php-invariant
使用方法
在此库中要使用的最主要的功能是 invariant
。此函数旨在指示程序员的错误,对于不应该发生的情况,类似于 断言。例如
<?php require_once(__DIR__ . '/../vendor/autoload.php'); use function Zheltikov\Invariant\invariant; $obj = new DateTime(); invariant($obj instanceof DateTime, 'Object must have type DateTime'); $p = 123; invariant($p !== null, 'Value can\'t be null'); $max = 100; invariant($p !== null && $p <= $max, '$p\'s value %d must be <= %d', $p, $max);
如果第一个参数的值评估为true,则程序继续执行;否则,将调用 invariant_violation
函数。该函数要么抛出 InvariantException
异常,要么调用先前通过函数 invariant_callback_register
注册的处理程序。
第一个参数被评估为布尔表达式。第二个参数是一个字符串,可以包含文本和/或由函数 sprintf
理解的可选格式化信息。字符串后面的可选逗号分隔值列表必须与该字符串内部可选格式化信息所期望的类型集合相匹配。
待办事项
- 构建一个函数式风格的断言库
- 为这些函数构建一个面向对象的接口