widi/filter-specification

Specification pattern for filtering objects in php.

4.0.1 2022-05-02 08:30 UTC

This package is auto-updated.

Last update: 2024-04-27 14:49:00 UTC


README

Code Sample From Test

    $candidate = $this->prophesize(MyCandidateInterface::class);
    $candidate->getValue()->willReturn($value);

    $builderFactory = new BuilderFactory();

    $specificationBuilder = $builderFactory->create();
    $firstSpecification   =
        $specificationBuilder
            ->and(new CandidateIsHigherThanFiveCompositeSpecification())
            ->and(new CandidateIsLowerThanTwentyCompositeSpecification())
            ->or(new CandidateIsDivisableByFive())
            ->build();

    $secondSpecification =
        $specificationBuilder
            ->and(new CandidateIsHigherThanOneHundredCompositeSpecification())
            ->and(new CandidateIsLowerThanTwoHundredCompositeSpecification())
            ->build();

    $compositeSpecification =
        $specificationBuilder
            ->or($firstSpecification)
            ->or($secondSpecification)
            ->build();

    $result = $compositeSpecification->meetsSpecification($candidate->reveal());

    $this->assertEquals($expectedResult, $result, 'Value failed: ' . $value);