1. Go to this page and download the library: Download zitec/rule-engine-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/ */
zitec / rule-engine-bundle example snippets
namespace MyBundle/RuleEngine/Context;
use Zitec/RuleEngineBundle/Service/ContextBase;
class MyFirstContext extends ContextBase
{
protected $dataSource;
// Using a custom name will help you identify the context in built expressions.
public function getContextObjectKey(): string
{
return 'my-name';
}
// The context will need a data source to read from. We will set the data source when we evaluate the expression.
public function setMyDataSource($dataSource)
{
$this->dataSource = $dataSource;
}
// Added a parameter.
public function getMyParameter(): string
{
return $this->dataSource->getTheData();
}
}
namespace MyBundle/RuleEngine/Conditions;
use Zitec/RuleEngineBundle/Conditions/AbstractArrayCondition;
class MyParameter extends AbstractArrayCondition
{
// When we ask the context object for the method for this condition name,
// it should respond with getMyCondition, since the ContextBase does simple snake_case to getCamelCase.
protected $name = 'my_parameter';
// This is the name the users will see in admin pages.
protected $label = 'My Parameter';
// A help text displayed in the condition builder if the user selects this parameter.
protected $description = 'Set conditions for my parameter';
// Here is where we decide on the operators that will be available for this parameter.
protected function getOperatorDefinitions(): array
{
$options = [
['key' => 'one', 'label' => 'Option one'],
['key' => 'two', 'label' => 'Option two'],
];
// Details about the operator definition structure can be found in the Operator definition section.
return [
[
'label' => 'match ANY of the following',
'name' => $this::INTERSECTING,
'fieldType' => 'select',
'fieldOptions' => [
'multiple' => true,
'options' => $options,
],
],
[
'label' => 'match NONE of the following',
'name' => $this::DISJOINT,
// Details about the autocomplete feature in the Autocomplete section of this readme.
'fieldType' => 'autocomplete',
'fieldOptions' => [
'autocomplete' => 'my_autocomplete_key',
],
],
];
}
}
class EmailAddress implements RuleInterface
{
use RuleTrait;
// Your entity's properties and getters/setters follow.
}
class EmailAddressAdmin extends AbstractAdmin
{
use RuleAdminTrait;
protected function configureFormFields(FormMapper $formMapper)
{
// Add the rule admin, using the method from the trait:
$this->addRuleFormElement($formMapper);
// Add the rest of your fields.
}
protected function configureListFields(ListMapper $list)
{
// Add the columns from the rule entity. On dev environments, the generated espression will also be visible.
$this->addRuleListColumns($list);
// Add the rest of your columns and actions.
}
use Doctrine\ORM\EntityRepository;
use MyBundle\RuleEngine\Context\MyFirstContext;
use Zitec\RuleEngineBundle\Service\RuleEvaluator;
class RecipientChooserService
{
/**
* The entity repository for your EmailAddress entity
* @var EntityRepository
*/
protected $emailAddressRepository;
/**
* @var RuleEvaluator
*/
protected $evaluator;
/**
* @var MyFirstContext
*/
protected $context;
public function __construct(
EntityRepository $emailAddressRepository,
RuleEvaluator $evaluator,
MyFirstContext $context
) {
$this->emailAddressRepository = $emailAddressRepository;
$this->evaluator = $evaluator;
$this->context = $context;
}
public function getRecipientAddresses($myDataSource)
{
// Load and filter email addresses.
$this->context->setMyDataSource($myDataSource);
/** @var EmailAddress[] $emailAddresses */
$emailAddresses = $this->emailAddressRepository->findAll();
// Determine the applicable addresses.
$recipients = [];
foreach ($emailAddresses as $entity) {
if ($this->evaluator->evaluate($entity->getRule(), $this->context)) {
$recipients[] = $entity->getEmail();
}
}
return $recipients;
}
}
use MyBundle\Entity\MyEntity;
use Zitec\RuleEngineBundle\Autocomplete\AbstractAutocompleteEntity;
class MyEntityAutocomplete extends AbstractAutocompleteEntity
{
protected function getEntityClass(): string
{
return MyEntity::class;
}
protected function getIdField(): string
{
return 'id'; // This will be the value used in the built expression
}
protected function getTextField(): string
{
return 'name'; // This will be the value displayed to the user.
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.