PHP 的规范

v0.1.2 2015-08-17 15:24 UTC

This package is not auto-updated.

Last update: 2024-09-18 17:50:36 UTC


README

#Spec 一个简单的 PHP 规范库

Build Status Code Climate

安装

composer require kayladnls/spec

这是什么?

“规范模式是一种特殊的软件设计模式,其中业务规则可以通过使用布尔逻辑将业务规则链接在一起来重新组合。该模式在领域驱动设计(Domain-Driven Design,简称DDD)的上下文中经常被使用。” -- 维基百科

如何使用它?

使用构建器

$all_spec = Kayladnls/Spec/Builder::all(new MustHaveFourLegs(), new MustHaveStripesSpec());

$all_spec->isSatisfiedBy($elephpant) //False 
$all_spec->isSatisfiedBy($zebra)     // True

$any_spec = Kayladnls/Spec/Builder::any($all_spec, new IsLizardSpec());
$any_spec->isSatisfiedBy($iguana) // True

或者,如果您喜欢,您也可以使用函数。

基于函数

$all_spec = Kayladnls/Spec/all([new MustHaveFourLegs(), new MustHaveStripesSpec()]);

Kayladnls/Spec/satisfies($elephpant, $all_spec) //False 
Kayladnls/Spec/satisfies($zebra, $all_spec) //True 

$any_spec = Kayladnls/Spec/any([$all_spec, new IsLizardSpec()]);
Kayladnls/Spec/satisfies($iguana, $any_spec) // True

$none_spec = Kayladnls/Spec/none([new MustHaveFourLegs(), new MustHaveSpotsSpec()]);
Kayladnls/Spec/satisfies($kangaroo, $none_spec) // true
Kayladnls/Spec/satisfies($cheetah, $none_spec) // false

示例

if ($all_spec->isSatisfiedBy($zebra)){
	// Do Some cool Zebra Stuff here. 
}

// Function Based
if (satisfies($iguana, $any_spec)){
	// do some cool lizard-based stuff here. 
}