zeus / anti-guard-clause
一个小型库,可以帮你避免编写大量的if-else语句
v1.0.1
2023-02-27 08:04 UTC
Requires
- php: ^8.2
Requires (Dev)
- orchestra/testbench: ^7.21
- phpunit/phpunit: ^9
README
防守卫子句
我不会告诉你什么是守卫子句,想了解的人可以自行研究,这个库的编写是为了防止守卫子句的形成。
使用composer安装
composer require zeus/anti-guard-clause
使用方法
防守卫子句的示例
让我们创建一个类
年龄类
use Zues\Less\IfInterface; readonly class Age implements IfInterface { public function __construct(private int $age) { } /** * @return mixed */ public function make(): mixed { return 'age must be greater than 18'; } /** * @return bool */ public function isTrue(): bool { return 18<=$this->age; } }
人类类
use Zues\Less\IfInterface; readonly class Man implements IfInterface { public function __construct(private string $gender) { } public function make(): string { return 'gender must be a man'; } /** * @return bool */ public function isTrue(): bool { return $this->gender === 'man'; } }
让我们创建一个默认的else类
use Zues\Less\ElseInterface; readonly class ElseGender implements ElseInterface { /** * @return mixed */ public function make(): string { return 'else condition'; } }
并使用它
$gender = 'man'; $age = 16; $condition = new Condition(); $condition ->if(new Age($age)) ->ifNot(new Man($gender)) ->else(new ElseGender()); echo $condition->getMake(); //get Result