PHP code example of kanel / specification2

1. Go to this page and download the library: Download kanel/specification2 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/ */

    

kanel / specification2 example snippets


    class IsProductAvaialble implement SpecificationInterface
    {
        protected $product;
        protected $neededQuantity;
        
        public function __construc(Product $product, $neededQuantity = 1)
        {
            $this->product = $product;
            $this->neededQuantity = $neededQuantity;
        }
        
        public function isValid(): bool
        {
            //here your code logic that uses the data set in the construct and returns a boolean
        }
    }

    class IsProductShipableForCustomer implement SpecificationInterface
    {
        protected $product;
        protected $customer;
        
        public function __construc(Product $product, Customer $customer)
        {
            $this->product = $product;
            $this->customer = $customer;
        }
        
        public function isValid(): bool
        {
            //doing the logic to check if the product is avaialble in the customer's country 
        }
    }

    class IsASpecialProduct implement SpecificationInterface
    {
        protected $product;
        
        public function __construc(Product $product)
        {
            $this->product = $product;
        }
        
        public function isValid(): bool
        {
            //doing the logic to check if the product is special 
        }
    }

    class IsCustomerBlackListed implement SpecificationInterface
    {
        protected $customer;
        
        public function __construc(Customer $customer)
        {
            $this->customer = $customer;
        }
        
        public function isValid(): bool
        {
            //doing the logic to checks if the customer is banned
        }
    }

    $specification = (new Specification())->isSatisfiedBy(new IsProductAvaialble($product));
     
     if ($specification->isValid()) {
        ...
     }

    $specification = (new Specification())->isSatisfiedByAll(
        new IsProductAvaialble($product), 
        new IsProductShipableForCustome($product, $customer)
    );
    
    if ($specification->isValid()) {
        ...
    }
       
    $specification = (new Specification())->isSatisfiedByAny(
        new IsProductAvaialble($product), 
        new IsASpecialProduct($product)
    );
    
    if ($specification->isValid()) {
        ...
    }
       
    $specification = (new Specification())
        ->isSatisfiedBy(new IsProductAvaialble($product))
        ->and(new IsProductShipableForCustomer($product, $customer))
        ...
    ;
    
    if ($specification->isValid()) {
       ...
    }
       
    $specification = (new Specification())
        ->isSatisfiedBy(new IsProductAvaialble($product))
        ->and(new IsProductShipableForCustomer($product, $customer))
        ->andNot(new IsCustomerBlackListed($customer)
        ...
    ;
    
    if ($specification->isValid()) {
       ...
    }
 
    $specification = (new Specification())
       ->isSatisfiedBy(new IsProductAvaialble($product))
       ->or(new IsASpecialProduc($product))
       ...
    ;
                   
    
    if ($specification->isValid()) {
       ...
    }

    $specification = (new Specification())
       ->isSatisfiedBy(new IsProductAvaialble($product))
       ->orNot(new IsASpecialProduc($product))
    ;
                   
    
    if ($specification->isValid()) {
       ...
    }
       
    $specification = (new Specification())
       ->isSatisfiedBy(new IsProductAvaialble($product))
       ->xor(new IsASpecialProduc($product))
    ;
                   
    
    if ($specification->isValid()) {
       ...
    }
       
    $specification = (new Specification())
       ->isSatisfiedBy(new IsProductAvaialble($product))
       ->xorNot(new IsASpecialProduc($product))
    ;
                   
    
    if ($specification->isValid()) {
       ...
    }
             
    $specification = (new Specification())
        ->isSatisfiedBy(new IsProductShipableForCustomer($product, $customer))
        ->and(
            (new Specification())
                ->isSatisfiedBy(new IsProductAvaialble($product))
                ->or(new IsASpecialProduct($product))
        )
    ;
    
    if ($specification->isValid()) {
       ...
    }