PHP code example of localheinz / specification

1. Go to this page and download the library: Download localheinz/specification library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

localheinz / specification example snippets


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');
    }
}

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

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

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