Specification¶
A specification is a formal description of expected behaviour, constraints, or requirements. In software development, the term covers several contexts.
In the specification pattern, a Domain-Driven Design tactical pattern, a specification encapsulates a business rule as an object that can be combined with other specifications using logical operators, like AND, OR, NOT. This allows business rules to be reused, combined, and tested in isolation.
In a broader sense, a specification can refer to an API specificationm such as OpenAPI/Swagger, a language specificationm such as the PHP language specification, a test specification, or an architecture decision record.
<?php
interface Specification
{
public function isSatisfiedBy(mixed $candidate): bool;
}
final class IsAdultUser implements Specification
{
public function isSatisfiedBy(mixed $candidate): bool
{
return $candidate instanceof User && $candidate->age >= 18;
}
}
final class AndSpecification implements Specification
{
public function __construct(
private readonly Specification $left,
private readonly Specification $right,
) {}
public function isSatisfiedBy(mixed $candidate): bool
{
return $this->left->isSatisfiedBy($candidate)
&& $this->right->isSatisfiedBy($candidate);
}
}
?>
See also Specification Pattern (Wikipedia) and Specifications by Eric Evans & Martin Fowler.
Related : Domain Design Driven (DDD), Design Pattern, Business Logic, Validation, Design By Contract (DBC), Swagger, Architectural Decision Record (ADR), Software Bill Of Material (SBOM)
Related packages : happyr/doctrine-specification