localheinz/specification

此包已被废弃,不再维护。未建议替代包。

提供遵循Eric Evans和Martin Fowler论文的规范。

0.1.0 2017-06-09 02:12 UTC

README

CI Status codecov Latest Stable Version Total Downloads

提供遵循Eric Evans和Martin Fowler论文的规范,也请参阅维基百科:规范模式

安装

运行

$ composer require localheinz/specification

使用

SpecificationInterface

在你的规范中实现Localheinz\Specification\SpecificationInterface

namespace Foo\Bar;

use Localheinz\Specification\SpecificationInterface;

final class IsInstanceOfStdClass implements SpecificationInterface
{
    public function isSatisfiedBy($candidate): bool
    {
        return $candidate instanceof \stdClass;
    }
}

final class HasFooProperty implements SpecificationInterface
{
    public function isSatisfiedBy($candidate): bool
    {
        return property_exists($candidate, 'foo');
    }
}

final class HasBarProperty implements SpecificationInterface
{
    public function isSatisfiedBy($candidate): bool
    {
        return property_exists($candidate, 'bar');
    }
}

AndSpecification

使用Localheinz\Specification\AndSpecification从需要满足的所有规范中组合规范

use Localheinz\Specification\AndSpecification;
use Localheinz\Specification\Test\Fixture;

$specification = new AndSpecification(
    new Fixture\Specification\IsInstanceOfStdClass(),
    new Fixture\Specification\HasFooProperty(),
    new Fixture\Specification\HasBarProperty()
);

$candidate = new \stdClass();

$candidate->foo = 9000;
$candidate->bar = 'Hello, my name is Jane';

$specification->isSatisfiedBy($candidate); // true

OrSpecification

使用Localheinz\Specification\OrSpecification从只需要满足其中一个规范的规范中组合规范

use Localheinz\Specification\OrSpecification;
use Localheinz\Specification\Test\Fixture;

$specification = new OrSpecification(
    new Fixture\Specification\IsInstanceOfStdClass(),
    new Fixture\Specification\HasFooProperty(),
    new Fixture\Specification\HasBarProperty()
);

$candidate = new \stdClass();

$specification->isSatisfiedBy($candidate); // true

NotSpecification

使用Localheinz\Specification\NotSpecification从不应该满足的规范中组合规范

use Localheinz\Specification\NotSpecification;
use Localheinz\Specification\Test\Fixture;

$specification = new NotSpecification(
    new Fixture\Specification\IsInstanceOfStdClass(),
    new Fixture\Specification\HasFooProperty(),
    new Fixture\Specification\HasBarProperty()
);

$candidate = new \SplObjectStorage();

$specification->isSatisfiedBy($candidate); // true

混合匹配

根据需要混合匹配所有规范

use Localheinz\Specification\AndSpecification;
use Localheinz\Specification\NotSpecification;
use Localheinz\Specification\OrSpecification;
use Localheinz\Specification\Test\Fixture;

$specification = new AndSpecification(
    new NotSpecification(new Fixture\Specification\IsInstanceOfStdClass()),
    new OrSpecification(
        new Fixture\Specification\HasFooProperty(),
        new Fixture\Specification\HasBarProperty()
    )
);

class Baz
{
    public $foo;
}

$candidate = new Baz();

$specification->isSatisfiedBy($candidate); // true

变更日志

请参阅CHANGELOG.md

贡献

请参阅CONTRIBUTING.md

行为准则

请参阅CODE_OF_CONDUCT.md

许可证

此包使用MIT许可证许可。