PHPackages                             vjik/specification - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. vjik/specification

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

vjik/specification
==================

Implementation of Specification pattern

1.0.0(1y ago)51BSD-3-ClausePHPPHP ^8.2

Since Nov 21Pushed 1y ago1 watchersCompare

[ Source](https://github.com/vjik/specification)[ Packagist](https://packagist.org/packages/vjik/specification)[ RSS](/packages/vjik-specification/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Specification Pattern
=====================

[](#specification-pattern)

[![Latest Stable Version](https://camo.githubusercontent.com/a64e5ab4c668a801cb1c945f9703c88fece3ac77600683f7fa82269950aeda33/68747470733a2f2f706f7365722e707567782e6f72672f766a696b2f73706563696669636174696f6e2f76)](https://packagist.org/packages/vjik/specification)[![Total Downloads](https://camo.githubusercontent.com/7b840eef0c35d5f0892cc83d21f0ad83dd370cc9fd8ec9e49053d7e23a451f38/68747470733a2f2f706f7365722e707567782e6f72672f766a696b2f73706563696669636174696f6e2f646f776e6c6f616473)](https://packagist.org/packages/vjik/specification)[![Build status](https://github.com/vjik/specification/actions/workflows/build.yml/badge.svg)](https://github.com/vjik/specification/actions/workflows/build.yml)[![Coverage Status](https://camo.githubusercontent.com/a8f5d2de5864cc11e2f0423c929ec050ff5ac651fa41ec296546e08551044abb/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f766a696b2f73706563696669636174696f6e2f62616467652e737667)](https://coveralls.io/github/vjik/specification)[![Mutation testing badge](https://camo.githubusercontent.com/7c2c2e1113c224006784e2c1ec93b43bb5a3cc7740dc92f2064214fab9ecec87/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246766a696b25324673706563696669636174696f6e2532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/vjik/specification/master)[![type-coverage](https://camo.githubusercontent.com/431f99367c71ea647ce65a93ccd9822fc67b4e1b26c68758ed76b6ec448b5828/68747470733a2f2f73686570686572642e6465762f6769746875622f766a696b2f73706563696669636174696f6e2f636f7665726167652e737667)](https://shepherd.dev/github/vjik/specification)[![static analysis](https://github.com/vjik/specification/workflows/static%20analysis/badge.svg)](https://github.com/vjik/specification/actions?query=workflow%3A%22static+analysis%22)[![psalm-level](https://camo.githubusercontent.com/bb0e2df70817e1b329910aaca2b239b018f8893685100b49aae79a4297c29037/68747470733a2f2f73686570686572642e6465762f6769746875622f766a696b2f73706563696669636174696f6e2f6c6576656c2e737667)](https://shepherd.dev/github/vjik/specification)

The package provides PHP implementation of "[Specification](https://designpatternsphp.readthedocs.io/en/latest/Behavioral/Specification/README.html)" software design pattern:

- interface `SpecificationInterface` and abstract `BaseSpecification` to create user specifications;
- composite specifications `AndSpecification` and `OrSpecification`;
- negation specification `NotSpecification`.

Requirements
------------

[](#requirements)

- PHP 8.2 or higher.

Installation
------------

[](#installation)

The package could be installed with [composer](https://getcomposer.org/download/):

```
composer require vjik/specification
```

General usage
-------------

[](#general-usage)

You can create your own specifications by inheriting from the `BaseSpecification` class:

```
use Vjik\Specification\BaseSpecification;

/**
 * @template T as User
 * @template-extends BaseSpecification
 */
final class UserIsAdultSpecification extends BaseSpecification
{
    /**
     * @param T $value
     */
    public function isSatisfiedBy(mixed $value): bool
    {
        return $value->age >= 18;
    }
}

$specification = new UserIsAdultSpecification();

// true or false
$specification->isSatisfiedBy($user);

// throws an exception if user is not adult
$specification->satisfiedBy($user);
```

> We recommend use static analysis tools like [Psalm](https://psalm.dev) and [PHPStan](https://phpstan.org)to improve code quality.

### Built-in specifications

[](#built-in-specifications)

You can combine specifications using composite specifications:

```
use Vjik\Specification\AndSpecification;
use Vjik\Specification\OrSpecification;

// User is adult AND active
$isActiveAdultUserSpecification = new AndSpecification([
    new UserIsAdultSpecification(),
    new UserIsActiveSpecification(),
]);

// User is adult OR user with parents
$userHasAccessSpecification = new OrSpecification([
    new UserIsAdultSpecification(),
    new UserWithParentsSpecification(),
]);
```

Also, you can use negation specification:

```
use Vjik\Specification\NotSpecification;

// User is not adult
$userIsNotAdultSpecification = new NotSpecification(
    new UserIsAdultSpecification()
);
```

### Static analysis compatible

[](#static-analysis-compatible)

Static analysis tools like [Psalm](https://psalm.dev) and [PHPStan](https://phpstan.org) helps you to avoid mistakes. For example, Psalm issues:

```
$userIsAdultSpecification = new UserIsAdultSpecification();

// ERROR: InvalidArgument Argument 1 of UserIsAdultSpecification::isSatisfiedBy expects User, but 'test' provided
$userIsAdultSpecification->isSatisfiedBy('test');

// ERROR: InvalidArgument Argument 1 of UserIsAdultSpecification::satisfiedBy expects User, but 'test' provided
$userIsAdultSpecification->satisfiedBy('test');

// ERROR: InvalidArgument Incompatible types found for T (must have only one of User, Post)
$isActiveAdultUserSpecification = new AndSpecification([
    new UserIsAdultSpecification(),
    new PostIsActiveSpecificaion(),
]);
```

Documentation
-------------

[](#documentation)

- [Internals](docs/internals.md)

If you have any questions or problems with this package, use [author telegram chat](https://t.me/predvoditelev_chat)for communication.

License
-------

[](#license)

The `vjik/specification` is free software. It is released under the terms of the BSD License. Please see [`LICENSE`](./LICENSE.md) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

544d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/53e5ee1dedd50f71e4aeeac2929f786cdfb400359d4776e6cd806388d0d5df2c?d=identicon)[vjik](/maintainers/vjik)

---

Top Contributors

[![vjik](https://avatars.githubusercontent.com/u/525501?v=4)](https://github.com/vjik "vjik (4 commits)")

---

Tags

patternphpspecificationspecification-patternspecificationsphpspecificationpattern

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/vjik-specification/health.svg)

```
[![Health](https://phpackages.com/badges/vjik-specification/health.svg)](https://phpackages.com/packages/vjik-specification)
```

###  Alternatives

[rikbruil/specification

A PHP implementation of the Specification-pattern

18284.8k1](/packages/rikbruil-specification)[maximecolin/satisfaction

A PHP implementation of the specification pattern for DDD

3630.2k](/packages/maximecolin-satisfaction)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
