PHP code example of nicolasjourdan / business-logic-bundle

1. Go to this page and download the library: Download nicolasjourdan/business-logic-bundle 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/ */

    

nicolasjourdan / business-logic-bundle example snippets


// config/bundles.php

return [
    // ...
    NicolasJourdan\BusinessLogicBundle\NicolasJourdanBusinessLogicBundle::class => ['all' => true],
];



namespace Your\Namespace\Specification;

use NicolasJourdan\BusinessLogicBundle\Service\Specification\CompositeSpecification;

class IsDummySpecification extends CompositeSpecification
{
    public function isSatisfiedBy($candidate): bool
    {
        // Your business condition...
    }
}



namespace Your\Namespace\Rule;

use NicolasJourdan\BusinessLogicBundle\Service\Rule\RuleInterface;
use Your\Namespace\Specification\IsDummySpecification;

class DummyRule implements RuleInterface
{
    private const BUSINESS_LOGIC_TAGS = [
        ['rule.user.awesome', 10],
        ['rule.user.another_tag'],
    ];

    /** @var IsDummySpecification */
    private $specification;

    public function __construct(IsDummySpecification $specification)
    {
        $this->specification = $specification;
    }

    public function shouldRun($candidate): bool
    {
        return $this->specification
            ->not()
            ->isSatisfiedBy($candidate);
    }

    public function execute($candidate)
    {
        // Your business rule...
    }
}



namespace Your\Namespace\Service;

use NicolasJourdan\BusinessLogicBundle\Service\Rule\RulesEngine;

class Dummy
{
    /** @var RulesEngine */
    private $rulesEngine;

    public function __construct(RulesEngine $rulesEngine)
    {
        $this->rulesEngine = $rulesEngine;
    }

    public function foo(): void
    {
        $candidate = ... 
        
        // The RulesEngine will execute all the business rules which have to be executed on the candidate.
        $updatedCandidate = $this->rulesEngine->execute($candidate);
        
        //...
    }
}